【问题标题】:How can I get a the runtime dependencies along with the coordinates and the JAR file of a Gradle project?如何获取运行时依赖项以及 Gradle 项目的坐标和 JAR 文件?
【发布时间】:2021-12-27 22:44:39
【问题描述】:
【问题讨论】:
标签:
java
gradle
dependencies
gradle-plugin
【解决方案1】:
我编写了这段 sn-p 代码,虽然我不确定这是否正确,但它会产生一些结果。这似乎是完成工作。
import java.util.Collection;
import java.util.function.Function;
import java.util.stream.Stream;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ResolvedDependency;
public class Dependencies {
private static <T> Stream<T> of(T node, Function<T, Collection<T>> childrenFn) {
return Stream.concat(Stream.of(node), childrenFn.apply(node).stream()
.flatMap(n -> of(n, childrenFn)));
}
private static Stream<ResolvedDependency> of(ResolvedDependency node) {
return of(node, ResolvedDependency::getChildren);
}
@SuppressWarnings("CodeBlock2Expr")
public static void doIt(Configuration configuration) {
configuration.getResolvedConfiguration()
.getFirstLevelModuleDependencies()
.stream()
.flatMap(Dependencies::of)
.flatMap(dependency -> {
return dependency.getModuleArtifacts().stream();
})
.distinct()
.forEach(artifact -> {
System.out.println(artifact.getFile());
});
}
}