如何获取依赖项 gradle Java

How get dependencies gradle Java

如何使用 Java 获取当前项目的依赖项? 我在 Java class 中尝试此代码,但结果为空:

class Example implements Plugin<Project> {
    void apply(Project project) {
             project.getConfigurations().getByName("runtime").getAllDependencies();        
        }
    }

感谢 JBirdVegas 的回答。我尝试在 Java:

上写你的例子
List<String> deps = new ArrayList<>();
        Configuration configuration = project.getConfigurations().getByName("compile");
        for (File file : configuration) {
            deps.add(file.toString());
        }

但有错误:

Cannot change dependencies of configuration ':compile' after it has been resolved.

当 运行 gradle 构建时

您只是缺少一个迭代找到的依赖项的步骤

Groovy:

class Example implements Plugin<Project> {
    void apply(Project project) {
        def configuration = project.configurations.getByName('compile')
        configuration.each { File file ->
            println "Found project dependency @ $file.absolutePath"
        }     
    }
}

Java 8:

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;

public class Example implements Plugin<Project> {
    @Override
    public void apply(Project project) {
        Configuration configuration = project.getConfigurations().getByName("compile");
        configuration.forEach(file -> {
            project.getLogger().lifecycle("Found project dependency @ " + file.getAbsolutePath());
        });
    }
}

Java 7:

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;

import java.io.File;

public class Example implements Plugin<Project> {
    @Override
    public void apply(Project project) {
        Configuration configuration = project.getConfigurations().getByName("compile");
        for (File file : configuration) {
            project.getLogger().lifecycle("Found project dependency @ " + file.getAbsolutePath());
        }
    }
}