gpt4 book ai didi

java - 如何获取依赖项 gradle Java

转载 作者:行者123 更新时间:2023-11-30 06:09:10 25 4
gpt4 key购买 nike

如何使用 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 时

最佳答案

你只是错过了一个迭代找到的依赖项的步骤

常规:

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());
}
}
}

关于java - 如何获取依赖项 gradle Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38484957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com