有没有办法以编程方式列出所有 gradle 依赖项?
Is there a way to list all gradle dependencies programmatically?
我知道这样做:
gradle dependencies
列出完整的依赖关系树。现在,我正在寻找一种以编程方式操作该依赖项树的方法,以便我可以打印相同的层次结构,但使用 JSON 而不是 gradle cli 现在在控制台中使用的格式。
我应该使用哪些 groovy 类 来实现?
已编辑
我想获得(在 JSON 中)这样的一些:
"dependencies" : [
{
"groupId" : "com.something",
"artifactId" : "somethingArtifact",
"version" : "1.0",
"dependencies" : [
"groupId" : "com.leaf",
"artifactId" : "standaloneArtifact",
"version" : "2.0",
]
},
{
"groupId" : "com.leaf",
"artifactId" : "anotherStandaloneArtifact",
"version" : "1.0",
"dependencies" : []
}
]
正如你在这里看到的那样,我知道哪个依赖项传递地依赖于其他哪些依赖项。
最简单的方法可能是编写您自己的 DependencyReportRenderer
实现,然后配置现有的 dependencies
任务以使用它,或者定义一个类型为 DependencyReportTask
的新任务配置了自己的渲染器。
您可以在 Gradle 文件中创建一个任务来迭代所有依赖项并生成您认为合适的 JSON。这是一个示例任务,可以为您打印 JSON:
task printDependencies << {
def json = '"dependencies": ['
configurations.runtime.resolvedConfiguration.resolvedArtifacts.each { artifact ->
def id = artifact.moduleVersion.id
// println "group: ${id.group}, name: ${id.name}, version: ${id.version}"
json += JsonOutput.toJson(id)
}
json += "]"
json = JsonOutput.prettyPrint(json)
println json
}
样本输出:
"dependencies": [
{
"group": "com.fasterxml.jackson.core",
"version": "2.6.5",
"name": "jackson-core",
"module": {
"group": "com.fasterxml.jackson.core",
"name": "jackson-core"
}
}{
"group": "org.springframework",
"version": "4.2.5.RELEASE",
"name": "spring-aop",
"module": {
"group": "org.springframework",
"name": "spring-aop"
}
}
]
大家好,这就是我最终归档我需要的东西的方式,希望对你们其他人有用。
首先,我要感谢 "pczeus" 和 "Björn Kautler" 的回答,帮助我找到了这个解决方案。
所以,这就是我解决问题的方法:
鉴于此 build.gradle:
apply plugin:'java'
repositories {
jcenter()
}
dependencies {
compile 'org.javassist:javassist:3.13.0-GA'
compile 'org.apache.geronimo.specs:geronimo-jta_1.1_spec:1.1.1'
compile 'org.hibernate:hibernate-core:5.1.0.Final'
compile 'org.springframework:spring-web:4.2.5.RELEASE'
}
如果你这样做:
gradle -b build.gradle dependencies --configuration=compile
您将在控制台中获得此输出:
:dependencies
------------------------------------------------------------
Root project
------------------------------------------------------------
compile - Compile classpath for source set 'main'.
+--- org.javassist:javassist:3.13.0-GA -> 3.20.0-GA
+--- org.apache.geronimo.specs:geronimo-jta_1.1_spec:1.1.1
+--- org.hibernate:hibernate-core:5.1.0.Final
| +--- org.jboss.logging:jboss-logging:3.3.0.Final
| +--- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final
| +--- org.javassist:javassist:3.20.0-GA
| +--- antlr:antlr:2.7.7
| +--- org.apache.geronimo.specs:geronimo-jta_1.1_spec:1.1.1
| +--- org.jboss:jandex:2.0.0.Final
| +--- com.fasterxml:classmate:1.3.0
| +--- dom4j:dom4j:1.6.1
| | \--- xml-apis:xml-apis:1.0.b2
| \--- org.hibernate.common:hibernate-commons-annotations:5.0.1.Final
| \--- org.jboss.logging:jboss-logging:3.3.0.Final
\--- org.springframework:spring-web:4.2.5.RELEASE
+--- org.springframework:spring-aop:4.2.5.RELEASE
| +--- aopalliance:aopalliance:1.0
| +--- org.springframework:spring-beans:4.2.5.RELEASE
| | \--- org.springframework:spring-core:4.2.5.RELEASE
| | \--- commons-logging:commons-logging:1.2
| \--- org.springframework:spring-core:4.2.5.RELEASE (*)
+--- org.springframework:spring-beans:4.2.5.RELEASE (*)
+--- org.springframework:spring-context:4.2.5.RELEASE
| +--- org.springframework:spring-aop:4.2.5.RELEASE (*)
| +--- org.springframework:spring-beans:4.2.5.RELEASE (*)
| +--- org.springframework:spring-core:4.2.5.RELEASE (*)
| \--- org.springframework:spring-expression:4.2.5.RELEASE
| \--- org.springframework:spring-core:4.2.5.RELEASE (*)
\--- org.springframework:spring-core:4.2.5.RELEASE (*)
(*) - dependencies omitted (listed previously)
我想要的是获得相同的 "dependency tree" 但格式为 JSON。所以这是我为此创建的 "task":
task printSolvedDepsTreeInJson {
doLast {
def jsonOutput = "["
configurations.compile.resolvedConfiguration.firstLevelModuleDependencies.each { dep ->
def addToJson
addToJson = { resolvedDep ->
jsonOutput += "\n{"
jsonOutput += "\"groupId\":\"${resolvedDep.module.id.group}\",\"artifactId\":\"${resolvedDep.module.id.name}\",\"version\":\"${resolvedDep.module.id.version}\",\"file\":\"${resolvedDep.getModuleArtifacts()[0].file}\""
jsonOutput += ",\"dependencies\":["
if(resolvedDep.children.size()!=0){
resolvedDep.children.each { childResolvedDep ->
if(resolvedDep in childResolvedDep.getParents() && childResolvedDep.getConfiguration() == 'compile'){
addToJson(childResolvedDep)
}
}
if(jsonOutput[-1] == ','){
jsonOutput = jsonOutput[0..-2]
}
}
jsonOutput += "]},"
}
addToJson(dep)
}
if(jsonOutput[-1] == ','){
jsonOutput = jsonOutput[0..-2]
}
jsonOutput += "]"
println jsonOutput
}
}
如果你运行这个任务:
gradle -b build.gradle printSolvedDepsTreeInJson
你会得到这个:
[
{
"groupId": "org.apache.geronimo.specs",
"artifactId": "geronimo-jta_1.1_spec",
"version": "1.1.1",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.apache.geronimo.specs/geronimo-jta_1.1_spec/1.1.1/aabab3165b8ea936b9360abbf448459c0d04a5a4/geronimo-jta_1.1_spec-1.1.1.jar",
"dependencies": []
},
{
"groupId": "org.hibernate",
"artifactId": "hibernate-core",
"version": "5.1.0.Final",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.hibernate/hibernate-core/5.1.0.Final/1b5ac619df76cfd67222ca7cddcee6b0a5db8d0c/hibernate-core-5.1.0.Final.jar",
"dependencies": [
{
"groupId": "org.jboss.logging",
"artifactId": "jboss-logging",
"version": "3.3.0.Final",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.jboss.logging/jboss-logging/3.3.0.Final/3616bb87707910296e2c195dc016287080bba5af/jboss-logging-3.3.0.Final.jar",
"dependencies": []
},
{
"groupId": "org.hibernate.javax.persistence",
"artifactId": "hibernate-jpa-2.1-api",
"version": "1.0.0.Final",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.hibernate.javax.persistence/hibernate-jpa-2.1-api/1.0.0.Final/5e731d961297e5a07290bfaf3db1fbc8bbbf405a/hibernate-jpa-2.1-api-1.0.0.Final.jar",
"dependencies": []
},
{
"groupId": "antlr",
"artifactId": "antlr",
"version": "2.7.7",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/antlr/antlr/2.7.7/83cd2cd674a217ade95a4bb83a8a14f351f48bd0/antlr-2.7.7.jar",
"dependencies": []
},
{
"groupId": "org.apache.geronimo.specs",
"artifactId": "geronimo-jta_1.1_spec",
"version": "1.1.1",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.apache.geronimo.specs/geronimo-jta_1.1_spec/1.1.1/aabab3165b8ea936b9360abbf448459c0d04a5a4/geronimo-jta_1.1_spec-1.1.1.jar",
"dependencies": []
},
{
"groupId": "org.jboss",
"artifactId": "jandex",
"version": "2.0.0.Final",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.jboss/jandex/2.0.0.Final/3e899258936f94649c777193e1be846387ed54b3/jandex-2.0.0.Final.jar",
"dependencies": []
},
{
"groupId": "com.fasterxml",
"artifactId": "classmate",
"version": "1.3.0",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/com.fasterxml/classmate/1.3.0/183407ff982e9375f1a1c4a51ed0a9307c598fc7/classmate-1.3.0.jar",
"dependencies": []
},
{
"groupId": "dom4j",
"artifactId": "dom4j",
"version": "1.6.1",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/dom4j/dom4j/1.6.1/5d3ccc056b6f056dbf0dddfdf43894b9065a8f94/dom4j-1.6.1.jar",
"dependencies": [
{
"groupId": "xml-apis",
"artifactId": "xml-apis",
"version": "1.0.b2",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/xml-apis/xml-apis/1.0.b2/3136ca936f64c9d68529f048c2618bd356bf85c9/xml-apis-1.0.b2.jar",
"dependencies": []
}]
},
{
"groupId": "org.hibernate.common",
"artifactId": "hibernate-commons-annotations",
"version": "5.0.1.Final",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.hibernate.common/hibernate-commons-annotations/5.0.1.Final/71e1cff3fcb20d3b3af4f3363c3ddb24d33c6879/hibernate-commons-annotations-5.0.1.Final.jar",
"dependencies": [
{
"groupId": "org.jboss.logging",
"artifactId": "jboss-logging",
"version": "3.3.0.Final",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.jboss.logging/jboss-logging/3.3.0.Final/3616bb87707910296e2c195dc016287080bba5af/jboss-logging-3.3.0.Final.jar",
"dependencies": []
}]
},
{
"groupId": "org.javassist",
"artifactId": "javassist",
"version": "3.20.0-GA",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.javassist/javassist/3.20.0-GA/a9cbcdfb7e9f86fbc74d3afae65f2248bfbf82a0/javassist-3.20.0-GA.jar",
"dependencies": []
}]
},
{
"groupId": "org.springframework",
"artifactId": "spring-web",
"version": "4.2.5.RELEASE",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-web/4.2.5.RELEASE/49cd2430884b77172aa81e3fc33ef668ea1dab30/spring-web-4.2.5.RELEASE.jar",
"dependencies": [
{
"groupId": "org.springframework",
"artifactId": "spring-aop",
"version": "4.2.5.RELEASE",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-aop/4.2.5.RELEASE/858d6c70909b3ce7e07b59fc936f8ccfcd81c0aa/spring-aop-4.2.5.RELEASE.jar",
"dependencies": [
{
"groupId": "org.springframework",
"artifactId": "spring-beans",
"version": "4.2.5.RELEASE",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-beans/4.2.5.RELEASE/fa992ae40f6fc47117282164e0433b71da385e94/spring-beans-4.2.5.RELEASE.jar",
"dependencies": [
{
"groupId": "org.springframework",
"artifactId": "spring-core",
"version": "4.2.5.RELEASE",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/4.2.5.RELEASE/251207b29f0f38f61e3495a2f7fb053cf1bfe8c/spring-core-4.2.5.RELEASE.jar",
"dependencies": [
{
"groupId": "commons-logging",
"artifactId": "commons-logging",
"version": "1.2",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar",
"dependencies": []
}]
}]
},
{
"groupId": "org.springframework",
"artifactId": "spring-core",
"version": "4.2.5.RELEASE",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/4.2.5.RELEASE/251207b29f0f38f61e3495a2f7fb053cf1bfe8c/spring-core-4.2.5.RELEASE.jar",
"dependencies": [
{
"groupId": "commons-logging",
"artifactId": "commons-logging",
"version": "1.2",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar",
"dependencies": []
}]
},
{
"groupId": "aopalliance",
"artifactId": "aopalliance",
"version": "1.0",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/aopalliance/aopalliance/1.0/235ba8b489512805ac13a8f9ea77a1ca5ebe3e8/aopalliance-1.0.jar",
"dependencies": []
}]
},
{
"groupId": "org.springframework",
"artifactId": "spring-beans",
"version": "4.2.5.RELEASE",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-beans/4.2.5.RELEASE/fa992ae40f6fc47117282164e0433b71da385e94/spring-beans-4.2.5.RELEASE.jar",
"dependencies": [
{
"groupId": "org.springframework",
"artifactId": "spring-core",
"version": "4.2.5.RELEASE",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/4.2.5.RELEASE/251207b29f0f38f61e3495a2f7fb053cf1bfe8c/spring-core-4.2.5.RELEASE.jar",
"dependencies": [
{
"groupId": "commons-logging",
"artifactId": "commons-logging",
"version": "1.2",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar",
"dependencies": []
}]
}]
},
{
"groupId": "org.springframework",
"artifactId": "spring-context",
"version": "4.2.5.RELEASE",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-context/4.2.5.RELEASE/a75e18322c7b362fe1daa26a245ae672ec0f3138/spring-context-4.2.5.RELEASE.jar",
"dependencies": [
{
"groupId": "org.springframework",
"artifactId": "spring-aop",
"version": "4.2.5.RELEASE",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-aop/4.2.5.RELEASE/858d6c70909b3ce7e07b59fc936f8ccfcd81c0aa/spring-aop-4.2.5.RELEASE.jar",
"dependencies": [
{
"groupId": "org.springframework",
"artifactId": "spring-beans",
"version": "4.2.5.RELEASE",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-beans/4.2.5.RELEASE/fa992ae40f6fc47117282164e0433b71da385e94/spring-beans-4.2.5.RELEASE.jar",
"dependencies": [
{
"groupId": "org.springframework",
"artifactId": "spring-core",
"version": "4.2.5.RELEASE",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/4.2.5.RELEASE/251207b29f0f38f61e3495a2f7fb053cf1bfe8c/spring-core-4.2.5.RELEASE.jar",
"dependencies": [
{
"groupId": "commons-logging",
"artifactId": "commons-logging",
"version": "1.2",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar",
"dependencies": []
}]
}]
},
{
"groupId": "org.springframework",
"artifactId": "spring-core",
"version": "4.2.5.RELEASE",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/4.2.5.RELEASE/251207b29f0f38f61e3495a2f7fb053cf1bfe8c/spring-core-4.2.5.RELEASE.jar",
"dependencies": [
{
"groupId": "commons-logging",
"artifactId": "commons-logging",
"version": "1.2",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar",
"dependencies": []
}]
},
{
"groupId": "aopalliance",
"artifactId": "aopalliance",
"version": "1.0",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/aopalliance/aopalliance/1.0/235ba8b489512805ac13a8f9ea77a1ca5ebe3e8/aopalliance-1.0.jar",
"dependencies": []
}]
},
{
"groupId": "org.springframework",
"artifactId": "spring-beans",
"version": "4.2.5.RELEASE",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-beans/4.2.5.RELEASE/fa992ae40f6fc47117282164e0433b71da385e94/spring-beans-4.2.5.RELEASE.jar",
"dependencies": [
{
"groupId": "org.springframework",
"artifactId": "spring-core",
"version": "4.2.5.RELEASE",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/4.2.5.RELEASE/251207b29f0f38f61e3495a2f7fb053cf1bfe8c/spring-core-4.2.5.RELEASE.jar",
"dependencies": [
{
"groupId": "commons-logging",
"artifactId": "commons-logging",
"version": "1.2",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar",
"dependencies": []
}]
}]
},
{
"groupId": "org.springframework",
"artifactId": "spring-core",
"version": "4.2.5.RELEASE",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/4.2.5.RELEASE/251207b29f0f38f61e3495a2f7fb053cf1bfe8c/spring-core-4.2.5.RELEASE.jar",
"dependencies": [
{
"groupId": "commons-logging",
"artifactId": "commons-logging",
"version": "1.2",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar",
"dependencies": []
}]
},
{
"groupId": "org.springframework",
"artifactId": "spring-expression",
"version": "4.2.5.RELEASE",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-expression/4.2.5.RELEASE/a42bdfb833d0be6c18429aea3fb0fba81f85c6e8/spring-expression-4.2.5.RELEASE.jar",
"dependencies": [
{
"groupId": "org.springframework",
"artifactId": "spring-core",
"version": "4.2.5.RELEASE",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/4.2.5.RELEASE/251207b29f0f38f61e3495a2f7fb053cf1bfe8c/spring-core-4.2.5.RELEASE.jar",
"dependencies": [
{
"groupId": "commons-logging",
"artifactId": "commons-logging",
"version": "1.2",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar",
"dependencies": []
}]
}]
}]
},
{
"groupId": "org.springframework",
"artifactId": "spring-core",
"version": "4.2.5.RELEASE",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/4.2.5.RELEASE/251207b29f0f38f61e3495a2f7fb053cf1bfe8c/spring-core-4.2.5.RELEASE.jar",
"dependencies": [
{
"groupId": "commons-logging",
"artifactId": "commons-logging",
"version": "1.2",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar",
"dependencies": []
}]
}]
},
{
"groupId": "org.javassist",
"artifactId": "javassist",
"version": "3.20.0-GA",
"file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.javassist/javassist/3.20.0-GA/a9cbcdfb7e9f86fbc74d3afae65f2248bfbf82a0/javassist-3.20.0-GA.jar",
"dependencies": []
}
]
这是我需要的依赖关系树的 JSON 表示。
现在,如果您仔细观察,您会注意到这不是我们在 build.gradle 中定义的实际依赖项列表。这是已解决的依赖树。这意味着某些依赖项已更改。
例如firstLevel依赖:
org.javassist:javassist:3.13.0-GA
已更改为:
org.javassist:javassist:3.20.0-GA
作为
org.hibernate:hibernate-core:5.1.0.Final
取决于:
org.javassist:javassist:3.20.0-GA
高于以下版本:
org.javassist:javassist:3.13.0-GA
并且 Gradle 默认情况下冲突解决算法始终选择 "latest" 版本。
其实是这样的:
+--- org.javassist:javassist:3.13.0-GA -> 3.20.0-GA
表示在控制台输出。 3.13.0-GA 被 3.20.0-GA 版本覆盖。
现在,这是一个问题,因为我没有得到实际的 "dependency tree"。我要 "resolved" 一个。
我最终通过定义另一个任务解决了这个问题:
task printDepsTreeInJson {
doLast {
configurations.compile.incoming.getResolutionResult().getAllDependencies().each { depResult ->
println "{\"from\":\"" + depResult.getFrom() + "\"," + "\"requested\":\"" + depResult.getRequested() + "\"}"
}
}
}
如果你执行这个:
gradle -b build.gradle printDepsTreeInJson
你现在会得到这个:
:printDepsTreeInJson
{"from":"project :","requested":"org.javassist:javassist:3.13.0-GA"}
{"from":"project :","requested":"org.apache.geronimo.specs:geronimo-jta_1.1_spec:1.1.1"}
{"from":"project :","requested":"org.hibernate:hibernate-core:5.1.0.Final"}
{"from":"org.hibernate:hibernate-core:5.1.0.Final","requested":"org.jboss.logging:jboss-logging:3.3.0.Final"}
{"from":"org.hibernate:hibernate-core:5.1.0.Final","requested":"org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final"}
{"from":"org.hibernate:hibernate-core:5.1.0.Final","requested":"org.javassist:javassist:3.20.0-GA"}
{"from":"org.hibernate:hibernate-core:5.1.0.Final","requested":"antlr:antlr:2.7.7"}
{"from":"org.hibernate:hibernate-core:5.1.0.Final","requested":"org.apache.geronimo.specs:geronimo-jta_1.1_spec:1.1.1"}
{"from":"org.hibernate:hibernate-core:5.1.0.Final","requested":"org.jboss:jandex:2.0.0.Final"}
{"from":"org.hibernate:hibernate-core:5.1.0.Final","requested":"com.fasterxml:classmate:1.3.0"}
{"from":"org.hibernate:hibernate-core:5.1.0.Final","requested":"dom4j:dom4j:1.6.1"}
{"from":"dom4j:dom4j:1.6.1","requested":"xml-apis:xml-apis:1.0.b2"}
{"from":"org.hibernate:hibernate-core:5.1.0.Final","requested":"org.hibernate.common:hibernate-commons-annotations:5.0.1.Final"}
{"from":"org.hibernate.common:hibernate-commons-annotations:5.0.1.Final","requested":"org.jboss.logging:jboss-logging:3.3.0.Final"}
{"from":"project :","requested":"org.springframework:spring-web:4.2.5.RELEASE"}
{"from":"org.springframework:spring-web:4.2.5.RELEASE","requested":"org.springframework:spring-aop:4.2.5.RELEASE"}
{"from":"org.springframework:spring-aop:4.2.5.RELEASE","requested":"aopalliance:aopalliance:1.0"}
{"from":"org.springframework:spring-aop:4.2.5.RELEASE","requested":"org.springframework:spring-beans:4.2.5.RELEASE"}
{"from":"org.springframework:spring-beans:4.2.5.RELEASE","requested":"org.springframework:spring-core:4.2.5.RELEASE"}
{"from":"org.springframework:spring-core:4.2.5.RELEASE","requested":"commons-logging:commons-logging:1.2"}
{"from":"org.springframework:spring-aop:4.2.5.RELEASE","requested":"org.springframework:spring-core:4.2.5.RELEASE"}
{"from":"org.springframework:spring-web:4.2.5.RELEASE","requested":"org.springframework:spring-beans:4.2.5.RELEASE"}
{"from":"org.springframework:spring-web:4.2.5.RELEASE","requested":"org.springframework:spring-context:4.2.5.RELEASE"}
{"from":"org.springframework:spring-context:4.2.5.RELEASE","requested":"org.springframework:spring-aop:4.2.5.RELEASE"}
{"from":"org.springframework:spring-context:4.2.5.RELEASE","requested":"org.springframework:spring-beans:4.2.5.RELEASE"}
{"from":"org.springframework:spring-context:4.2.5.RELEASE","requested":"org.springframework:spring-core:4.2.5.RELEASE"}
{"from":"org.springframework:spring-context:4.2.5.RELEASE","requested":"org.springframework:spring-expression:4.2.5.RELEASE"}
{"from":"org.springframework:spring-expression:4.2.5.RELEASE","requested":"org.springframework:spring-core:4.2.5.RELEASE"}
{"from":"org.springframework:spring-web:4.2.5.RELEASE","requested":"org.springframework:spring-core:4.2.5.RELEASE"}
这不是最终的 "dependency tree"(我最终使用 javascript 构建它)但它是您实际需要生成它的东西!
"from" 是请求另一个依赖项的依赖项,"requested" 是请求的实际依赖项! :)
如果
"from":"project :"
这意味着该依赖项是 "first level" 依赖项。 (一个根)
所有其他依赖项将是这样的:
{"from":"org.hibernate:hibernate-core:5.1.0.Final","requested":"org.jboss.logging:jboss-logging:3.3.0.Final"}
请注意,我现在有两个
org.javassist:javassist
每一个都关联到实际请求它的依赖项。
一个是 "first level" 依赖项:
{"from":"project :","requested":"org.javassist:javassist:3.13.0-GA"}
另一个是hibernate请求的:
{"from":"org.hibernate:hibernate-core:5.1.0.Final","requested":"org.javassist:javassist:3.20.0-GA"}
我懒得在同一个任务中生成 JSON 中的依赖树 :) 但是,很明显,如果您出于某种原因需要解析 "original"(former/not已解决)依赖树。
这是最终build.gradle文件的内容,如果您想复制粘贴并试用:
apply plugin:'java'
repositories {
jcenter()
}
dependencies {
compile 'org.javassist:javassist:3.13.0-GA'
compile 'org.apache.geronimo.specs:geronimo-jta_1.1_spec:1.1.1'
compile 'org.hibernate:hibernate-core:5.1.0.Final'
compile 'org.springframework:spring-web:4.2.5.RELEASE'
}
task printDepsTreeInJson {
doLast {
configurations.compile.incoming.getResolutionResult().getAllDependencies().each { depResult ->
println "{\"from\":\"" + depResult.getFrom() + "\"," + "\"requested\":\"" + depResult.getRequested() + "\"}"
}
}
}
task printSolvedDepsTreeInJson {
doLast {
def jsonOutput = "["
configurations.compile.resolvedConfiguration.firstLevelModuleDependencies.each { dep ->
def addToJson
addToJson = { resolvedDep ->
jsonOutput += "\n{"
jsonOutput += "\"groupId\":\"${resolvedDep.module.id.group}\",\"artifactId\":\"${resolvedDep.module.id.name}\",\"version\":\"${resolvedDep.module.id.version}\",\"file\":\"${resolvedDep.getModuleArtifacts()[0].file}\""
jsonOutput += ",\"dependencies\":["
if(resolvedDep.children.size()!=0){
resolvedDep.children.each { childResolvedDep ->
if(resolvedDep in childResolvedDep.getParents() && childResolvedDep.getConfiguration() == 'compile'){
addToJson(childResolvedDep)
}
}
if(jsonOutput[-1] == ','){
jsonOutput = jsonOutput[0..-2]
}
}
jsonOutput += "]},"
}
addToJson(dep)
}
if(jsonOutput[-1] == ','){
jsonOutput = jsonOutput[0..-2]
}
jsonOutput += "]"
println jsonOutput
}
}
我知道这样做:
gradle dependencies
列出完整的依赖关系树。现在,我正在寻找一种以编程方式操作该依赖项树的方法,以便我可以打印相同的层次结构,但使用 JSON 而不是 gradle cli 现在在控制台中使用的格式。
我应该使用哪些 groovy 类 来实现?
已编辑
我想获得(在 JSON 中)这样的一些:
"dependencies" : [
{
"groupId" : "com.something",
"artifactId" : "somethingArtifact",
"version" : "1.0",
"dependencies" : [
"groupId" : "com.leaf",
"artifactId" : "standaloneArtifact",
"version" : "2.0",
]
},
{
"groupId" : "com.leaf",
"artifactId" : "anotherStandaloneArtifact",
"version" : "1.0",
"dependencies" : []
}
]
正如你在这里看到的那样,我知道哪个依赖项传递地依赖于其他哪些依赖项。
最简单的方法可能是编写您自己的 DependencyReportRenderer
实现,然后配置现有的 dependencies
任务以使用它,或者定义一个类型为 DependencyReportTask
的新任务配置了自己的渲染器。
您可以在 Gradle 文件中创建一个任务来迭代所有依赖项并生成您认为合适的 JSON。这是一个示例任务,可以为您打印 JSON:
task printDependencies << {
def json = '"dependencies": ['
configurations.runtime.resolvedConfiguration.resolvedArtifacts.each { artifact ->
def id = artifact.moduleVersion.id
// println "group: ${id.group}, name: ${id.name}, version: ${id.version}"
json += JsonOutput.toJson(id)
}
json += "]"
json = JsonOutput.prettyPrint(json)
println json
}
样本输出:
"dependencies": [
{
"group": "com.fasterxml.jackson.core",
"version": "2.6.5",
"name": "jackson-core",
"module": {
"group": "com.fasterxml.jackson.core",
"name": "jackson-core"
}
}{
"group": "org.springframework",
"version": "4.2.5.RELEASE",
"name": "spring-aop",
"module": {
"group": "org.springframework",
"name": "spring-aop"
}
}
]
大家好,这就是我最终归档我需要的东西的方式,希望对你们其他人有用。
首先,我要感谢 "pczeus" 和 "Björn Kautler" 的回答,帮助我找到了这个解决方案。
所以,这就是我解决问题的方法:
鉴于此 build.gradle:
apply plugin:'java' repositories { jcenter() } dependencies { compile 'org.javassist:javassist:3.13.0-GA' compile 'org.apache.geronimo.specs:geronimo-jta_1.1_spec:1.1.1' compile 'org.hibernate:hibernate-core:5.1.0.Final' compile 'org.springframework:spring-web:4.2.5.RELEASE' }
如果你这样做:
gradle -b build.gradle dependencies --configuration=compile
您将在控制台中获得此输出:
:dependencies ------------------------------------------------------------ Root project ------------------------------------------------------------ compile - Compile classpath for source set 'main'. +--- org.javassist:javassist:3.13.0-GA -> 3.20.0-GA +--- org.apache.geronimo.specs:geronimo-jta_1.1_spec:1.1.1 +--- org.hibernate:hibernate-core:5.1.0.Final | +--- org.jboss.logging:jboss-logging:3.3.0.Final | +--- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final | +--- org.javassist:javassist:3.20.0-GA | +--- antlr:antlr:2.7.7 | +--- org.apache.geronimo.specs:geronimo-jta_1.1_spec:1.1.1 | +--- org.jboss:jandex:2.0.0.Final | +--- com.fasterxml:classmate:1.3.0 | +--- dom4j:dom4j:1.6.1 | | \--- xml-apis:xml-apis:1.0.b2 | \--- org.hibernate.common:hibernate-commons-annotations:5.0.1.Final | \--- org.jboss.logging:jboss-logging:3.3.0.Final \--- org.springframework:spring-web:4.2.5.RELEASE +--- org.springframework:spring-aop:4.2.5.RELEASE | +--- aopalliance:aopalliance:1.0 | +--- org.springframework:spring-beans:4.2.5.RELEASE | | \--- org.springframework:spring-core:4.2.5.RELEASE | | \--- commons-logging:commons-logging:1.2 | \--- org.springframework:spring-core:4.2.5.RELEASE (*) +--- org.springframework:spring-beans:4.2.5.RELEASE (*) +--- org.springframework:spring-context:4.2.5.RELEASE | +--- org.springframework:spring-aop:4.2.5.RELEASE (*) | +--- org.springframework:spring-beans:4.2.5.RELEASE (*) | +--- org.springframework:spring-core:4.2.5.RELEASE (*) | \--- org.springframework:spring-expression:4.2.5.RELEASE | \--- org.springframework:spring-core:4.2.5.RELEASE (*) \--- org.springframework:spring-core:4.2.5.RELEASE (*) (*) - dependencies omitted (listed previously)
我想要的是获得相同的 "dependency tree" 但格式为 JSON。所以这是我为此创建的 "task":
task printSolvedDepsTreeInJson { doLast { def jsonOutput = "[" configurations.compile.resolvedConfiguration.firstLevelModuleDependencies.each { dep -> def addToJson addToJson = { resolvedDep -> jsonOutput += "\n{" jsonOutput += "\"groupId\":\"${resolvedDep.module.id.group}\",\"artifactId\":\"${resolvedDep.module.id.name}\",\"version\":\"${resolvedDep.module.id.version}\",\"file\":\"${resolvedDep.getModuleArtifacts()[0].file}\"" jsonOutput += ",\"dependencies\":[" if(resolvedDep.children.size()!=0){ resolvedDep.children.each { childResolvedDep -> if(resolvedDep in childResolvedDep.getParents() && childResolvedDep.getConfiguration() == 'compile'){ addToJson(childResolvedDep) } } if(jsonOutput[-1] == ','){ jsonOutput = jsonOutput[0..-2] } } jsonOutput += "]}," } addToJson(dep) } if(jsonOutput[-1] == ','){ jsonOutput = jsonOutput[0..-2] } jsonOutput += "]" println jsonOutput } }
如果你运行这个任务:
gradle -b build.gradle printSolvedDepsTreeInJson
你会得到这个:
[ { "groupId": "org.apache.geronimo.specs", "artifactId": "geronimo-jta_1.1_spec", "version": "1.1.1", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.apache.geronimo.specs/geronimo-jta_1.1_spec/1.1.1/aabab3165b8ea936b9360abbf448459c0d04a5a4/geronimo-jta_1.1_spec-1.1.1.jar", "dependencies": [] }, { "groupId": "org.hibernate", "artifactId": "hibernate-core", "version": "5.1.0.Final", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.hibernate/hibernate-core/5.1.0.Final/1b5ac619df76cfd67222ca7cddcee6b0a5db8d0c/hibernate-core-5.1.0.Final.jar", "dependencies": [ { "groupId": "org.jboss.logging", "artifactId": "jboss-logging", "version": "3.3.0.Final", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.jboss.logging/jboss-logging/3.3.0.Final/3616bb87707910296e2c195dc016287080bba5af/jboss-logging-3.3.0.Final.jar", "dependencies": [] }, { "groupId": "org.hibernate.javax.persistence", "artifactId": "hibernate-jpa-2.1-api", "version": "1.0.0.Final", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.hibernate.javax.persistence/hibernate-jpa-2.1-api/1.0.0.Final/5e731d961297e5a07290bfaf3db1fbc8bbbf405a/hibernate-jpa-2.1-api-1.0.0.Final.jar", "dependencies": [] }, { "groupId": "antlr", "artifactId": "antlr", "version": "2.7.7", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/antlr/antlr/2.7.7/83cd2cd674a217ade95a4bb83a8a14f351f48bd0/antlr-2.7.7.jar", "dependencies": [] }, { "groupId": "org.apache.geronimo.specs", "artifactId": "geronimo-jta_1.1_spec", "version": "1.1.1", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.apache.geronimo.specs/geronimo-jta_1.1_spec/1.1.1/aabab3165b8ea936b9360abbf448459c0d04a5a4/geronimo-jta_1.1_spec-1.1.1.jar", "dependencies": [] }, { "groupId": "org.jboss", "artifactId": "jandex", "version": "2.0.0.Final", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.jboss/jandex/2.0.0.Final/3e899258936f94649c777193e1be846387ed54b3/jandex-2.0.0.Final.jar", "dependencies": [] }, { "groupId": "com.fasterxml", "artifactId": "classmate", "version": "1.3.0", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/com.fasterxml/classmate/1.3.0/183407ff982e9375f1a1c4a51ed0a9307c598fc7/classmate-1.3.0.jar", "dependencies": [] }, { "groupId": "dom4j", "artifactId": "dom4j", "version": "1.6.1", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/dom4j/dom4j/1.6.1/5d3ccc056b6f056dbf0dddfdf43894b9065a8f94/dom4j-1.6.1.jar", "dependencies": [ { "groupId": "xml-apis", "artifactId": "xml-apis", "version": "1.0.b2", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/xml-apis/xml-apis/1.0.b2/3136ca936f64c9d68529f048c2618bd356bf85c9/xml-apis-1.0.b2.jar", "dependencies": [] }] }, { "groupId": "org.hibernate.common", "artifactId": "hibernate-commons-annotations", "version": "5.0.1.Final", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.hibernate.common/hibernate-commons-annotations/5.0.1.Final/71e1cff3fcb20d3b3af4f3363c3ddb24d33c6879/hibernate-commons-annotations-5.0.1.Final.jar", "dependencies": [ { "groupId": "org.jboss.logging", "artifactId": "jboss-logging", "version": "3.3.0.Final", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.jboss.logging/jboss-logging/3.3.0.Final/3616bb87707910296e2c195dc016287080bba5af/jboss-logging-3.3.0.Final.jar", "dependencies": [] }] }, { "groupId": "org.javassist", "artifactId": "javassist", "version": "3.20.0-GA", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.javassist/javassist/3.20.0-GA/a9cbcdfb7e9f86fbc74d3afae65f2248bfbf82a0/javassist-3.20.0-GA.jar", "dependencies": [] }] }, { "groupId": "org.springframework", "artifactId": "spring-web", "version": "4.2.5.RELEASE", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-web/4.2.5.RELEASE/49cd2430884b77172aa81e3fc33ef668ea1dab30/spring-web-4.2.5.RELEASE.jar", "dependencies": [ { "groupId": "org.springframework", "artifactId": "spring-aop", "version": "4.2.5.RELEASE", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-aop/4.2.5.RELEASE/858d6c70909b3ce7e07b59fc936f8ccfcd81c0aa/spring-aop-4.2.5.RELEASE.jar", "dependencies": [ { "groupId": "org.springframework", "artifactId": "spring-beans", "version": "4.2.5.RELEASE", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-beans/4.2.5.RELEASE/fa992ae40f6fc47117282164e0433b71da385e94/spring-beans-4.2.5.RELEASE.jar", "dependencies": [ { "groupId": "org.springframework", "artifactId": "spring-core", "version": "4.2.5.RELEASE", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/4.2.5.RELEASE/251207b29f0f38f61e3495a2f7fb053cf1bfe8c/spring-core-4.2.5.RELEASE.jar", "dependencies": [ { "groupId": "commons-logging", "artifactId": "commons-logging", "version": "1.2", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar", "dependencies": [] }] }] }, { "groupId": "org.springframework", "artifactId": "spring-core", "version": "4.2.5.RELEASE", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/4.2.5.RELEASE/251207b29f0f38f61e3495a2f7fb053cf1bfe8c/spring-core-4.2.5.RELEASE.jar", "dependencies": [ { "groupId": "commons-logging", "artifactId": "commons-logging", "version": "1.2", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar", "dependencies": [] }] }, { "groupId": "aopalliance", "artifactId": "aopalliance", "version": "1.0", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/aopalliance/aopalliance/1.0/235ba8b489512805ac13a8f9ea77a1ca5ebe3e8/aopalliance-1.0.jar", "dependencies": [] }] }, { "groupId": "org.springframework", "artifactId": "spring-beans", "version": "4.2.5.RELEASE", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-beans/4.2.5.RELEASE/fa992ae40f6fc47117282164e0433b71da385e94/spring-beans-4.2.5.RELEASE.jar", "dependencies": [ { "groupId": "org.springframework", "artifactId": "spring-core", "version": "4.2.5.RELEASE", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/4.2.5.RELEASE/251207b29f0f38f61e3495a2f7fb053cf1bfe8c/spring-core-4.2.5.RELEASE.jar", "dependencies": [ { "groupId": "commons-logging", "artifactId": "commons-logging", "version": "1.2", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar", "dependencies": [] }] }] }, { "groupId": "org.springframework", "artifactId": "spring-context", "version": "4.2.5.RELEASE", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-context/4.2.5.RELEASE/a75e18322c7b362fe1daa26a245ae672ec0f3138/spring-context-4.2.5.RELEASE.jar", "dependencies": [ { "groupId": "org.springframework", "artifactId": "spring-aop", "version": "4.2.5.RELEASE", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-aop/4.2.5.RELEASE/858d6c70909b3ce7e07b59fc936f8ccfcd81c0aa/spring-aop-4.2.5.RELEASE.jar", "dependencies": [ { "groupId": "org.springframework", "artifactId": "spring-beans", "version": "4.2.5.RELEASE", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-beans/4.2.5.RELEASE/fa992ae40f6fc47117282164e0433b71da385e94/spring-beans-4.2.5.RELEASE.jar", "dependencies": [ { "groupId": "org.springframework", "artifactId": "spring-core", "version": "4.2.5.RELEASE", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/4.2.5.RELEASE/251207b29f0f38f61e3495a2f7fb053cf1bfe8c/spring-core-4.2.5.RELEASE.jar", "dependencies": [ { "groupId": "commons-logging", "artifactId": "commons-logging", "version": "1.2", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar", "dependencies": [] }] }] }, { "groupId": "org.springframework", "artifactId": "spring-core", "version": "4.2.5.RELEASE", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/4.2.5.RELEASE/251207b29f0f38f61e3495a2f7fb053cf1bfe8c/spring-core-4.2.5.RELEASE.jar", "dependencies": [ { "groupId": "commons-logging", "artifactId": "commons-logging", "version": "1.2", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar", "dependencies": [] }] }, { "groupId": "aopalliance", "artifactId": "aopalliance", "version": "1.0", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/aopalliance/aopalliance/1.0/235ba8b489512805ac13a8f9ea77a1ca5ebe3e8/aopalliance-1.0.jar", "dependencies": [] }] }, { "groupId": "org.springframework", "artifactId": "spring-beans", "version": "4.2.5.RELEASE", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-beans/4.2.5.RELEASE/fa992ae40f6fc47117282164e0433b71da385e94/spring-beans-4.2.5.RELEASE.jar", "dependencies": [ { "groupId": "org.springframework", "artifactId": "spring-core", "version": "4.2.5.RELEASE", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/4.2.5.RELEASE/251207b29f0f38f61e3495a2f7fb053cf1bfe8c/spring-core-4.2.5.RELEASE.jar", "dependencies": [ { "groupId": "commons-logging", "artifactId": "commons-logging", "version": "1.2", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar", "dependencies": [] }] }] }, { "groupId": "org.springframework", "artifactId": "spring-core", "version": "4.2.5.RELEASE", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/4.2.5.RELEASE/251207b29f0f38f61e3495a2f7fb053cf1bfe8c/spring-core-4.2.5.RELEASE.jar", "dependencies": [ { "groupId": "commons-logging", "artifactId": "commons-logging", "version": "1.2", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar", "dependencies": [] }] }, { "groupId": "org.springframework", "artifactId": "spring-expression", "version": "4.2.5.RELEASE", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-expression/4.2.5.RELEASE/a42bdfb833d0be6c18429aea3fb0fba81f85c6e8/spring-expression-4.2.5.RELEASE.jar", "dependencies": [ { "groupId": "org.springframework", "artifactId": "spring-core", "version": "4.2.5.RELEASE", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/4.2.5.RELEASE/251207b29f0f38f61e3495a2f7fb053cf1bfe8c/spring-core-4.2.5.RELEASE.jar", "dependencies": [ { "groupId": "commons-logging", "artifactId": "commons-logging", "version": "1.2", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar", "dependencies": [] }] }] }] }, { "groupId": "org.springframework", "artifactId": "spring-core", "version": "4.2.5.RELEASE", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/4.2.5.RELEASE/251207b29f0f38f61e3495a2f7fb053cf1bfe8c/spring-core-4.2.5.RELEASE.jar", "dependencies": [ { "groupId": "commons-logging", "artifactId": "commons-logging", "version": "1.2", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar", "dependencies": [] }] }] }, { "groupId": "org.javassist", "artifactId": "javassist", "version": "3.20.0-GA", "file": "/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.javassist/javassist/3.20.0-GA/a9cbcdfb7e9f86fbc74d3afae65f2248bfbf82a0/javassist-3.20.0-GA.jar", "dependencies": [] } ]
这是我需要的依赖关系树的 JSON 表示。 现在,如果您仔细观察,您会注意到这不是我们在 build.gradle 中定义的实际依赖项列表。这是已解决的依赖树。这意味着某些依赖项已更改。
例如firstLevel依赖:
org.javassist:javassist:3.13.0-GA
已更改为:
org.javassist:javassist:3.20.0-GA
作为
org.hibernate:hibernate-core:5.1.0.Final
取决于:
org.javassist:javassist:3.20.0-GA
高于以下版本:
org.javassist:javassist:3.13.0-GA
并且 Gradle 默认情况下冲突解决算法始终选择 "latest" 版本。
其实是这样的:
+--- org.javassist:javassist:3.13.0-GA -> 3.20.0-GA
表示在控制台输出。 3.13.0-GA 被 3.20.0-GA 版本覆盖。
现在,这是一个问题,因为我没有得到实际的 "dependency tree"。我要 "resolved" 一个。
我最终通过定义另一个任务解决了这个问题:
task printDepsTreeInJson { doLast { configurations.compile.incoming.getResolutionResult().getAllDependencies().each { depResult -> println "{\"from\":\"" + depResult.getFrom() + "\"," + "\"requested\":\"" + depResult.getRequested() + "\"}" } } }
如果你执行这个:
gradle -b build.gradle printDepsTreeInJson
你现在会得到这个:
:printDepsTreeInJson {"from":"project :","requested":"org.javassist:javassist:3.13.0-GA"} {"from":"project :","requested":"org.apache.geronimo.specs:geronimo-jta_1.1_spec:1.1.1"} {"from":"project :","requested":"org.hibernate:hibernate-core:5.1.0.Final"} {"from":"org.hibernate:hibernate-core:5.1.0.Final","requested":"org.jboss.logging:jboss-logging:3.3.0.Final"} {"from":"org.hibernate:hibernate-core:5.1.0.Final","requested":"org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final"} {"from":"org.hibernate:hibernate-core:5.1.0.Final","requested":"org.javassist:javassist:3.20.0-GA"} {"from":"org.hibernate:hibernate-core:5.1.0.Final","requested":"antlr:antlr:2.7.7"} {"from":"org.hibernate:hibernate-core:5.1.0.Final","requested":"org.apache.geronimo.specs:geronimo-jta_1.1_spec:1.1.1"} {"from":"org.hibernate:hibernate-core:5.1.0.Final","requested":"org.jboss:jandex:2.0.0.Final"} {"from":"org.hibernate:hibernate-core:5.1.0.Final","requested":"com.fasterxml:classmate:1.3.0"} {"from":"org.hibernate:hibernate-core:5.1.0.Final","requested":"dom4j:dom4j:1.6.1"} {"from":"dom4j:dom4j:1.6.1","requested":"xml-apis:xml-apis:1.0.b2"} {"from":"org.hibernate:hibernate-core:5.1.0.Final","requested":"org.hibernate.common:hibernate-commons-annotations:5.0.1.Final"} {"from":"org.hibernate.common:hibernate-commons-annotations:5.0.1.Final","requested":"org.jboss.logging:jboss-logging:3.3.0.Final"} {"from":"project :","requested":"org.springframework:spring-web:4.2.5.RELEASE"} {"from":"org.springframework:spring-web:4.2.5.RELEASE","requested":"org.springframework:spring-aop:4.2.5.RELEASE"} {"from":"org.springframework:spring-aop:4.2.5.RELEASE","requested":"aopalliance:aopalliance:1.0"} {"from":"org.springframework:spring-aop:4.2.5.RELEASE","requested":"org.springframework:spring-beans:4.2.5.RELEASE"} {"from":"org.springframework:spring-beans:4.2.5.RELEASE","requested":"org.springframework:spring-core:4.2.5.RELEASE"} {"from":"org.springframework:spring-core:4.2.5.RELEASE","requested":"commons-logging:commons-logging:1.2"} {"from":"org.springframework:spring-aop:4.2.5.RELEASE","requested":"org.springframework:spring-core:4.2.5.RELEASE"} {"from":"org.springframework:spring-web:4.2.5.RELEASE","requested":"org.springframework:spring-beans:4.2.5.RELEASE"} {"from":"org.springframework:spring-web:4.2.5.RELEASE","requested":"org.springframework:spring-context:4.2.5.RELEASE"} {"from":"org.springframework:spring-context:4.2.5.RELEASE","requested":"org.springframework:spring-aop:4.2.5.RELEASE"} {"from":"org.springframework:spring-context:4.2.5.RELEASE","requested":"org.springframework:spring-beans:4.2.5.RELEASE"} {"from":"org.springframework:spring-context:4.2.5.RELEASE","requested":"org.springframework:spring-core:4.2.5.RELEASE"} {"from":"org.springframework:spring-context:4.2.5.RELEASE","requested":"org.springframework:spring-expression:4.2.5.RELEASE"} {"from":"org.springframework:spring-expression:4.2.5.RELEASE","requested":"org.springframework:spring-core:4.2.5.RELEASE"} {"from":"org.springframework:spring-web:4.2.5.RELEASE","requested":"org.springframework:spring-core:4.2.5.RELEASE"}
这不是最终的 "dependency tree"(我最终使用 javascript 构建它)但它是您实际需要生成它的东西!
"from" 是请求另一个依赖项的依赖项,"requested" 是请求的实际依赖项! :)
如果
"from":"project :"
这意味着该依赖项是 "first level" 依赖项。 (一个根)
所有其他依赖项将是这样的:
{"from":"org.hibernate:hibernate-core:5.1.0.Final","requested":"org.jboss.logging:jboss-logging:3.3.0.Final"}
请注意,我现在有两个
org.javassist:javassist
每一个都关联到实际请求它的依赖项。 一个是 "first level" 依赖项:
{"from":"project :","requested":"org.javassist:javassist:3.13.0-GA"}
另一个是hibernate请求的:
{"from":"org.hibernate:hibernate-core:5.1.0.Final","requested":"org.javassist:javassist:3.20.0-GA"}
我懒得在同一个任务中生成 JSON 中的依赖树 :) 但是,很明显,如果您出于某种原因需要解析 "original"(former/not已解决)依赖树。
这是最终build.gradle文件的内容,如果您想复制粘贴并试用:
apply plugin:'java' repositories { jcenter() } dependencies { compile 'org.javassist:javassist:3.13.0-GA' compile 'org.apache.geronimo.specs:geronimo-jta_1.1_spec:1.1.1' compile 'org.hibernate:hibernate-core:5.1.0.Final' compile 'org.springframework:spring-web:4.2.5.RELEASE' } task printDepsTreeInJson { doLast { configurations.compile.incoming.getResolutionResult().getAllDependencies().each { depResult -> println "{\"from\":\"" + depResult.getFrom() + "\"," + "\"requested\":\"" + depResult.getRequested() + "\"}" } } } task printSolvedDepsTreeInJson { doLast { def jsonOutput = "[" configurations.compile.resolvedConfiguration.firstLevelModuleDependencies.each { dep -> def addToJson addToJson = { resolvedDep -> jsonOutput += "\n{" jsonOutput += "\"groupId\":\"${resolvedDep.module.id.group}\",\"artifactId\":\"${resolvedDep.module.id.name}\",\"version\":\"${resolvedDep.module.id.version}\",\"file\":\"${resolvedDep.getModuleArtifacts()[0].file}\"" jsonOutput += ",\"dependencies\":[" if(resolvedDep.children.size()!=0){ resolvedDep.children.each { childResolvedDep -> if(resolvedDep in childResolvedDep.getParents() && childResolvedDep.getConfiguration() == 'compile'){ addToJson(childResolvedDep) } } if(jsonOutput[-1] == ','){ jsonOutput = jsonOutput[0..-2] } } jsonOutput += "]}," } addToJson(dep) } if(jsonOutput[-1] == ','){ jsonOutput = jsonOutput[0..-2] } jsonOutput += "]" println jsonOutput } }