如何使用Java获取当前项目的依赖关系?我在Java类中尝试此代码,但结果为空:
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 build时
您只是错过了迭代找到的依赖项的步骤
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());
}
}
}