Gradle6:以编程方式获取实现依赖列表
Gradle 6: get the list of implementation dependencies programmatically
我有一个 build.gradle
项目的 java 文件,看起来像这样:
group "com.example"
dependencies {
implementation project(':project:project-base')
}
如何以编程方式获取实现依赖项列表?
我希望是这样的:
configurations.compile.dark.magick.moreMagicDependencies()
到 return 一组要筛选的依赖项,但我可能遗漏了一些重要的东西。
谢谢!
我认为您可以像下面的代码一样使用 resolvedConfiguration
。注意,"implementation" 不能直接解析(try..)。
但是你可以使用例如。 compileClasspath
(如果适合您的情况);还有一些扩展 "implementation" 的其他配置(参见 Java Library plugin.. 中的图表)。
像这样调用下面的代码gradle :mymodule:dumpDependencies
apply plugin: 'java-library'
dependencies {
implementation 'org.springframework:spring-web:5.2.1.RELEASE'
}
task dumpDependencies {
doLast {
def resolved = configurations.compileClasspath.resolvedConfiguration
println "files=$resolved.files"
resolved.resolvedArtifacts.each {
artifact-> println "artifact=$artifact"
}
}
}
编辑
另请注意 compile
已弃用,如果您使用 implementation ..
,则 compile
依赖项将为空(您当然可以使用 gradle :module1:dependencies
检查)。
为了扩展 Daniele 的回答,这个 returns 正是我要找的:
configurations.compileClasspath.incoming.dependencies.findAll {
it.group && it.name && it.getProperties().get("dependencyProject")
}.collect {
[group: it.group, name: it.name]
}
我有一个 build.gradle
项目的 java 文件,看起来像这样:
group "com.example"
dependencies {
implementation project(':project:project-base')
}
如何以编程方式获取实现依赖项列表?
我希望是这样的:
configurations.compile.dark.magick.moreMagicDependencies()
到 return 一组要筛选的依赖项,但我可能遗漏了一些重要的东西。
谢谢!
我认为您可以像下面的代码一样使用 resolvedConfiguration
。注意,"implementation" 不能直接解析(try..)。
但是你可以使用例如。 compileClasspath
(如果适合您的情况);还有一些扩展 "implementation" 的其他配置(参见 Java Library plugin.. 中的图表)。
像这样调用下面的代码gradle :mymodule:dumpDependencies
apply plugin: 'java-library'
dependencies {
implementation 'org.springframework:spring-web:5.2.1.RELEASE'
}
task dumpDependencies {
doLast {
def resolved = configurations.compileClasspath.resolvedConfiguration
println "files=$resolved.files"
resolved.resolvedArtifacts.each {
artifact-> println "artifact=$artifact"
}
}
}
编辑
另请注意 compile
已弃用,如果您使用 implementation ..
,则 compile
依赖项将为空(您当然可以使用 gradle :module1:dependencies
检查)。
为了扩展 Daniele 的回答,这个 returns 正是我要找的:
configurations.compileClasspath.incoming.dependencies.findAll {
it.group && it.name && it.getProperties().get("dependencyProject")
}.collect {
[group: it.group, name: it.name]
}