有没有办法以编程方式列出所有 gradle 依赖项?

Posted

技术标签:

【中文标题】有没有办法以编程方式列出所有 gradle 依赖项?【英文标题】:Is there a way to list all gradle dependencies programmatically? 【发布时间】:2016-07-31 21:34:29 【问题描述】:

我知道这样做:

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" : []
  
]

正如你在这里看到的那样,我知道哪个依赖项依赖于其他依赖项。

【问题讨论】:

【参考方案1】:

大家好,这就是我最终归档所需内容的方式,希望对你们其他人有用。

首先,我要感谢“pczeus”和“Björn Kautler”的回答,他们帮助我找到了这个解决方案。

所以,这就是我解决问题的方法:

鉴于此 build.gradle:

应用插件:'java' 存储库 中心() 依赖 编译'org.javassist:javassist:3.13.0-GA' 编译'org.apache.geronimo.specs:geronimo-jta_1.1_spec:1.1.1' 编译'org.hibernate:hibernate-core:5.1.0.Final' 编译'org.springframework:spring-web:4.2.5.RELEASE'

如果你这样做:

gradle -b build.gradle 依赖 --configuration=compile

您将在控制台中获得以下输出:

: 依赖 -------------------------------------------------- ---------- 根项目 -------------------------------------------------- ---------- compile - 为源集“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:同学: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 | | \--- 公共日志记录:公共日志记录: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 (*) (*) - 省略依赖项(之前列出)

我想要的是获得一个相同的“依赖树”,但采用 JSON 格式。所以这是我为此创建的“任务”:

任务 printSolvedDepsTreeInJson 做最后 def json输出=“[” configuration.compile.resolvedConfiguration.firstLevelModuleDependencies.each dep -> def addToJson addToJson = resolveDep -> json输出 += "\n" jsonOutput += "\"groupId\":\"$resolvedDep.module.id.group\",\"artifactId\":\"$resolvedDep.module.id.name\",\"version\ ":\"$resolvedDep.module.id.version\",\"file\":\"$resolvedDep.getModuleArtifacts()[0].file\"" jsonOutput += ",\"依赖关系\":[" if(resolvedDep.children.size()!=0) 已解决Dep.children.each childResolvedDep -> if(在 childResolvedDep.getParents() && childResolvedDep.getConfiguration() 中的 resolvedDep == 'compile') addToJson(childResolvedDep) if(jsonOutput[-1] == ',') jsonOutput = jsonOutput[0..-2] json输出 += "]," addToJson(dep) if(jsonOutput[-1] == ',') jsonOutput = jsonOutput[0..-2] json输出+=“]” println json输出

如果您运行此任务:

gradle -b build.gradle printSolvedDepsTreeInJson

你会得到这个:

[ "groupId": "org.apache.geronimo.specs", “artifactId”:“geronimo-jta_1.1_spec”, “版本”:“1.1.1”, “文件”:“/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", “依赖”:[] , "groupId": "org.hibernate", “artifactId”:“休眠核心”, “版本”:“5.1.0.Final”, “文件”:“/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 ", “依赖”:[ "groupId": "org.jboss.logging", "artifactId": "jboss-logging", “版本”:“3.3.0.Final”, “文件”:“/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 。罐”, “依赖”:[] , "groupId": "org.hibernate.javax.persistence", “artifactId”:“hibernate-jpa-2.1-api”, “版本”:“1.0.0.Final”, “文件”:“/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", “依赖”:[] , "groupId": "antlr", “artifactId”:“antlr”, “版本”:“2.7.7”, “文件”:“/Users/cgadam/.gradle/caches/modules-2/files-2.1/antlr/antlr/2.7.7/83cd2cd674a217ade95a4bb83a8a14f351f48bd0/antlr-2.7.7.jar”, “依赖”:[] , "groupId": "org.apache.geronimo.specs", “artifactId”:“geronimo-jta_1.1_spec”, “版本”:“1.1.1”, “文件”:“/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", “依赖”:[] , "groupId": "org.jboss", “artifactId”:“jandex”, “版本”:“2.0.0.Final”, “文件”:“/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.jboss/jandex/2.0.0.Final/3e899258936f94649c777193e1be846387ed54b3/jandex-2.0.0.Final.jar”, “依赖”:[] , "groupId": "com.fasterxml", "artifactId": "同学", “版本”:“1.3.0”, “文件”:“/Users/cgadam/.gradle/caches/modules-2/files-2.1/com.fasterxml/classmate/1.3.0/183407ff982e9375f1a1c4a51ed0a9307c598fc7/classmate-1.3.0.jar”, “依赖”:[] , "groupId": "dom4j", “artifactId”:“dom4j”, “版本”:“1.6.1”, “文件”:“/Users/cgadam/.gradle/caches/modules-2/files-2.1/dom4j/dom4j/1.6.1/5d3ccc056b6f056dbf0dddfdf43894b9065a8f94/dom4j-1.6.1.jar”, “依赖”:[ "groupId": "xml-apis", "artifactId": "xml-apis", “版本”:“1.0.b2”, “文件”:“/Users/cgadam/.gradle/caches/modules-2/files-2.1/xml-apis/xml-apis/1.0.b2/3136ca936f64c9d68529f048c2618bd356bf85c9/xml-apis-1.0.b2.jar”, “依赖”:[] ] , "groupId": "org.hibernate.common", "artifactId": "hibernate-commons-annotations", “版本”:“5.0.1.Final”, “文件”:“/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", “依赖”:[ "groupId": "org.jboss.logging", "artifactId": "jboss-logging", “版本”:“3.3.0.Final”, “文件”:“/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 。罐”, “依赖”:[] ] , "groupId": "org.javassist", “artifactId”:“javassist”, “版本”:“3.20.0-GA”, “文件”:“/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.javassist/javassist/3.20.0-GA/a9cbcdfb7e9f86fbc74d3afae65f2248bfbf82a0/javassist-3.20.0-GA.jar”, “依赖”:[] ] , "groupId": "org.springframework", “artifactId”:“弹簧网”, “版本”:“4.2.5.RELEASE”, “文件”:“/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 ", “依赖”:[ "groupId": "org.springframework", "artifactId": "spring-aop", “版本”:“4.2.5.RELEASE”, “文件”:“/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 ", “依赖”:[ "groupId": "org.springframework", "artifactId": "spring-beans", “版本”:“4.2.5.RELEASE”, “文件”:“/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 ", “依赖”:[ "groupId": "org.springframework", “artifactId”:“弹簧芯”, “版本”:“4.2.5.RELEASE”, “文件”:“/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 ", “依赖”:[ "groupId": "commons-logging", "artifactId": "commons-logging", “版本”:“1.2”, “文件”:“/Users/cgadam/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar”, “依赖”:[] ] ] , "groupId": "org.springframework", “artifactId”:“弹簧芯”, “版本”:“4.2.5.RELEASE”, “文件”:“/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 ", “依赖”:[ "groupId": "commons-logging", "artifactId": "commons-logging", “版本”:“1.2”, “文件”:“/Users/cgadam/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar”, “依赖”:[] ] , "groupId": "aopalliance", "artifactId": "aopalliance", “版本”:“1.0”, “文件”:“/Users/cgadam/.gradle/caches/modules-2/files-2.1/aopalliance/aopalliance/1.0/235ba8b489512805ac13a8f9ea77a1ca5ebe3e8/aopalliance-1.0.jar”, “依赖”:[] ] , "groupId": "org.springframework", "artifactId": "spring-beans", “版本”:“4.2.5.RELEASE”, “文件”:“/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 ", “依赖”:[ "groupId": "org.springframework", “artifactId”:“弹簧芯”, “版本”:“4.2.5.RELEASE”, “文件”:“/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 ", “依赖”:[ "groupId": "commons-logging", "artifactId": "commons-logging", “版本”:“1.2”, “文件”:“/Users/cgadam/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar”, “依赖”:[] ] ] , "groupId": "org.springframework", “artifactId”:“弹簧上下文”, “版本”:“4.2.5.RELEASE”, “文件”:“/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 ", “依赖”:[ "groupId": "org.springframework", "artifactId": "spring-aop", “版本”:“4.2.5.RELEASE”, “文件”:“/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 ", “依赖”:[ "groupId": "org.springframework", "artifactId": "spring-beans", “版本”:“4.2.5.RELEASE”, “文件”:“/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 ", “依赖”:[ "groupId": "org.springframework", “artifactId”:“弹簧芯”, “版本”:“4.2.5.RELEASE”, “文件”:“/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 ", “依赖”:[ "groupId": "commons-logging", "artifactId": "commons-logging", “版本”:“1.2”, “文件”:“/Users/cgadam/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar”, “依赖”:[] ] ] , "groupId": "org.springframework", “artifactId”:“弹簧芯”, “版本”:“4.2.5.RELEASE”, “文件”:“/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 ", “依赖”:[ "groupId": "commons-logging", "artifactId": "commons-logging", “版本”:“1.2”, “文件”:“/Users/cgadam/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar”, “依赖”:[] ] , "groupId": "aopalliance", "artifactId": "aopalliance", “版本”:“1.0”, “文件”:“/Users/cgadam/.gradle/caches/modules-2/files-2.1/aopalliance/aopalliance/1.0/235ba8b489512805ac13a8f9ea77a1ca5ebe3e8/aopalliance-1.0.jar”, “依赖”:[] ] , "groupId": "org.springframework", "artifactId": "spring-beans", “版本”:“4.2.5.RELEASE”, “文件”:“/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 ", “依赖”:[ "groupId": "org.springframework", “artifactId”:“弹簧芯”, “版本”:“4.2.5.RELEASE”, “文件”:“/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 ", “依赖”:[ "groupId": "commons-logging", "artifactId": "commons-logging", “版本”:“1.2”, “文件”:“/Users/cgadam/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar”, “依赖”:[] ] ] , "groupId": "org.springframework", “artifactId”:“弹簧芯”, “版本”:“4.2.5.RELEASE”, “文件”:“/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 ", “依赖”:[ "groupId": "commons-logging", "artifactId": "commons-logging", “版本”:“1.2”, “文件”:“/Users/cgadam/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar”, “依赖”:[] ] , "groupId": "org.springframework", “artifactId”:“弹簧表达式”, “版本”:“4.2.5.RELEASE”, “文件”:“/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 ", “依赖”:[ "groupId": "org.springframework", “artifactId”:“弹簧芯”, “版本”:“4.2.5.RELEASE”, “文件”:“/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 ", “依赖”:[ "groupId": "commons-logging", "artifactId": "commons-logging", “版本”:“1.2”, “文件”:“/Users/cgadam/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar”, “依赖”:[] ] ] ] , "groupId": "org.springframework", “artifactId”:“弹簧芯”, “版本”:“4.2.5.RELEASE”, “文件”:“/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 ", “依赖”:[ "groupId": "commons-logging", "artifactId": "commons-logging", “版本”:“1.2”, “文件”:“/Users/cgadam/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar”, “依赖”:[] ] ] , "groupId": "org.javassist", “artifactId”:“javassist”, “版本”:“3.20.0-GA”, “文件”:“/Users/cgadam/.gradle/caches/modules-2/files-2.1/org.javassist/javassist/3.20.0-GA/a9cbcdfb7e9f86fbc74d3afae65f2248bfbf82a0/javassist-3.20.0-GA.jar”, “依赖”:[] ]

这是我需要的依赖树的 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 默认的冲突解决算法总是选择“最新”版本。

其实是这样的:

+--- org.javassist:javassist:3.13.0-GA -> 3.20.0-GA

在控制台输出中的意思。 3.13.0-GA 被 3.20.0-GA 版本覆盖。

现在,这是一个问题,因为我没有得到实际的“依赖树”。我得到了“已解决”的问题。

我最终通过定义另一个 TASK 来解决这种情况:

任务 printDepsTreeInJson 做最后 configuration.compile.incoming.getResolutionResult().getAllDependencies().each depResult -> println "\"from\":\"" + depResult.getFrom() + "\"," + "\"requested\":\"" + depResult.getRequested() + "\""

如果你执行这个:

gradle -b build.gradle printDepsTreeInJson

你现在会得到这个:

:printDepsTreeInJson “来自”:“项目:”,“请求”:“org.javassist:javassist:3.13.0-GA” “来自”:“项目:”,“请求”:“org.apache.geronimo.specs:geronimo-jta_1.1_spec:1.1.1” "from":"project :","re​​quested":"org.hibernate:hibernate-core:5.1.0.Final" "来自":"org.hibernate:hibernate-core:5.1.0.Final","re​​quested":"org.jboss.logging:jboss-logging:3.3.0.Final" "来自":"org.hibernate:hibernate-core:5.1.0.Final","re​​quested":"org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final" "来自":"org.hibernate:hibernate-core:5.1.0.Final","re​​quested":"org.javassist:javassist:3.20.0-GA" "来自":"org.hibernate:hibernate-core:5.1.0.Final","re​​quested":"antlr:antlr:2.7.7" "来自":"org.hibernate:hibernate-core:5.1.0.Final","re​​quested":"org.apache.geronimo.specs:geronimo-jta_1.1_spec:1.1.1" "来自":"org.hibernate:hibernate-core:5.1.0.Final","re​​quested":"org.jboss:jandex:2.0.0.Final" "来自":"org.hibernate:hibernate-core:5.1.0.Final","re​​quested":"com.fasterxml:classmate:1.3.0" "来自":"org.hibernate:hibernate-core:5.1.0.Final","re​​quested":"dom4j:dom4j:1.6.1" "来自":"dom4j:dom4j:1.6.1","请求":"xml-apis:xml-apis:1.0.b2" "来自":"org.hibernate:hibernate-core:5.1.0.Final","re​​quested":"org.hibernate.common:hibernate-commons-annotations:5.0.1.Final" "来自":"org.hibernate.common:hibernate-commons-annotations:5.0.1.Final","re​​quested":"org.jboss.logging:jboss-logging:3.3.0.Final" “来自”:“项目:”,“请求”:“org.springframework:spring-web:4.2.5.RELEASE” "来自":"org.springframework:spring-web:4.2.5.RELEASE","re​​quested":"org.springframework:spring-aop:4.2.5.RELEASE" "来自":"org.springframework:spring-aop:4.2.5.RELEASE","re​​quested":"aopalliance:aopalliance:1.0" "来自":"org.springframework:spring-aop:4.2.5.RELEASE","re​​quested":"org.springframework:spring-beans:4.2.5.RELEASE" "来自":"org.springframework:spring-beans:4.2.5.RELEASE","re​​quested":"org.springframework:spring-core:4.2.5.RELEASE" "来自":"org.springframework:spring-core:4.2.5.RELEASE","re​​quested":"commons-logging:commons-logging:1.2" "来自":"org.springframework:spring-aop:4.2.5.RELEASE","re​​quested":"org.springframework:spring-core:4.2.5.RELEASE" "来自":"org.springframework:spring-web:4.2.5.RELEASE","re​​quested":"org.springframework:spring-beans:4.2.5.RELEASE" "来自":"org.springframework:spring-web:4.2.5.RELEASE","re​​quested":"org.springframework:spring-context:4.2.5.RELEASE" "来自":"org.springframework:spring-context:4.2.5.RELEASE","re​​quested":"org.springframework:spring-aop:4.2.5.RELEASE" "来自":"org.springframework:spring-context:4.2.5.RELEASE","re​​quested":"org.springframework:spring-beans:4.2.5.RELEASE" "来自":"org.springframework:spring-context:4.2.5.RELEASE","re​​quested":"org.springframework:spring-core:4.2.5.RELEASE" "来自":"org.springframework:spring-context:4.2.5.RELEASE","re​​quested":"org.springframework:spring-expression:4.2.5.RELEASE" "来自":"org.springframework:spring-expression:4.2.5.RELEASE","re​​quested":"org.springframework:spring-core:4.2.5.RELEASE" "来自":"org.springframework:spring-web:4.2.5.RELEASE","re​​quested":"org.springframework:spring-core:4.2.5.RELEASE"

这不是最终的“依赖树”(我最终使用 javascript 构建它),但它是您真正需要生成它的!

“from”是请求另一个依赖项的依赖项,“requested”是被请求的实际依赖项! :)

如果

“来自”:“项目:”

这意味着依赖是“第一级”依赖。 (一个根)

所有其他依赖项都是这样的:

"来自":"org.hibernate:hibernate-core:5.1.0.Final","re​​quested":"org.jboss.logging:jboss-logging:3.3.0.Final"

请注意,我现在确实有两个

org.javassist:javassist

每个都与实际请求它的依赖项相关联。 一个是“一级”依赖:

“来自”:“项目:”,“请求”:“org.javassist:javassist:3.13.0-GA”

另一个是休眠请求的:

"来自":"org.hibernate:hibernate-core:5.1.0.Final","re​​quested":"org.javassist:javassist:3.20.0-GA"

我懒得在同一个任务中生成 JSON 中的依赖树 :) 但是,很明显,如果您出于某种原因需要解析“原始”(以前/未解决) 依赖树。

这是最终的 build.gradle 文件的内容,以防您想复制粘贴并尝试一下:

应用插件:'java' 存储库 中心() 依赖 编译'org.javassist:javassist:3.13.0-GA' 编译'org.apache.geronimo.specs:geronimo-jta_1.1_spec:1.1.1' 编译'org.hibernate:hibernate-core:5.1.0.Final' 编译'org.springframework:spring-web:4.2.5.RELEASE' 任务 printDepsTreeInJson 做最后 configuration.compile.incoming.getResolutionResult().getAllDependencies().each depResult -> println "\"from\":\"" + depResult.getFrom() + "\"," + "\"requested\":\"" + depResult.getRequested() + "\"" 任务 printSolvedDepsTreeInJson 做最后 def json输出=“[” configuration.compile.resolvedConfiguration.firstLevelModuleDependencies.each dep -> def addToJson addToJson = resolveDep -> json输出 += "\n" jsonOutput += "\"groupId\":\"$resolvedDep.module.id.group\",\"artifactId\":\"$resolvedDep.module.id.name\",\"version\ ":\"$resolvedDep.module.id.version\",\"file\":\"$resolvedDep.getModuleArtifacts()[0].file\"" jsonOutput += ",\"依赖关系\":[" if(resolvedDep.children.size()!=0) 已解决Dep.children.each childResolvedDep -> if(在 childResolvedDep.getParents() && childResolvedDep.getConfiguration() 中的 resolvedDep == 'compile') addToJson(childResolvedDep) if(jsonOutput[-1] == ',') jsonOutput = jsonOutput[0..-2] json输出 += "]," addToJson(dep) if(jsonOutput[-1] == ',') jsonOutput = jsonOutput[0..-2] json输出+=“]” println json输出

【讨论】:

【参考方案2】:

您可以在 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"
        
    
]

【讨论】:

我更新了答案,展示了一个以 JSON 格式打印所有已解析依赖项的好例子。 pczeus 非常感谢您提供如此详细的回答!但是,我需要知道这些依赖项的依赖项,以便我可以操纵树(这是我感兴趣的事情)。请再看一遍问题。我已经对其进行了编辑,以更好地描述我想要获得的预期结果。再次感谢! 没问题。如果您查看 Configuration 的 Groovy 文档,您可以看到 api 甚至获取所有配置的 allDependencies。此外,resolvedConfiguration 有一个 API 来获取第一级依赖项和所有子项。因此,您应该能够根据需要调整任务以导航树。' 顺便说一句。我展示的示例是显示所有依赖项的“扁平化”依赖关系树,无需导航树。如果您想在 JSON 中嵌套子依赖项,那么我之前的评论应该可以帮助您指明正确的方向。 pczeus,我猜这比创建我自己的记者要简单得多。您指出的 API 有 getChildren() 方法:)【参考方案3】:

最简单的方法可能是编写自己的 DependencyReportRenderer 实现,然后配置现有的 dependencies 任务以使用它,或者定义一个 DependencyReportTask 类型的新任务并配置您自己的渲染器。

【讨论】:

谢谢!我会看看你所描述的课程。你知道有什么好的 IDE 可以实际调试 Gradle 脚本吗? 如何配置任务以使用特定的渲染? 我使用 IntelliJ IDEA。它是世界上最好的 Java IDE,在编写和调试 Gradle 脚本方面也非常出色。好吧,就像task jsonDeps(type: DependencyReportTask) renderer new MyOwnRenderer() 。但那是我的想法,还没有尝试过。 您有机会对此进行扩展吗? 在什么范围内?实际上,我正在美国旅行,手头没有电脑,所以我可能无法扩展太多,但是除了实际实施之外,您还需要什么?我认为您应该拥有所需的所有信息。

以上是关于有没有办法以编程方式列出所有 gradle 依赖项?的主要内容,如果未能解决你的问题,请参考以下文章

如何以编程方式列出 C++ 或 Python 中的 DLL 依赖项?

如何以编程方式在C ++或Python中列出DLL的依赖项?

有没有办法列出 pip 依赖项/要求?

Gradle - 获取依赖项的 URL

Gradle 没有以正确的方式寻找依赖关系

Gradle 依赖项未列出本地 maven jar 的依赖项