有没有办法以编程方式列出所有gradle依赖项?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了有没有办法以编程方式列出所有gradle依赖项?相关的知识,希望对你有一定的参考价值。
我知道这样做:
gradle dependencies
列出完整的依赖关系树。现在,我正在寻找一种以编程方式操作依赖关系树的方法,这样我就可以打印相同的层次结构但是使用JSON而不是gradle cli现在在控制台中使用的格式。
有谁知道我应该使用哪些groovy类来实现这一目标?
EDITED
我想获得(在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" : []
}
]
正如你在这里看到的那样,我知道哪个依赖项依赖于哪些其他依赖项可传递。
答案
大家好,我最终归档了我需要的东西,希望对你们其他人有用。
首先,我要感谢“pczeus”和“BjörnKautler”的答案,这些答案帮助我找到了这个解决方案。
所以,这就是我解决问题的方法:
鉴于此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)
我想要的是以JSON格式获得相同的“依赖树”。所以这是我为此做的“任务”:
task printSolvedDepsTreeInJson { doLast { def jsonOutput = "[" configurations.compile.resolvedConfiguration.firstLevelModuleDependencies.each { dep -> def addToJson addToJson = { resolvedDep -> jsonOutput += " {" 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": []以上是关于有没有办法以编程方式列出所有gradle依赖项?的主要内容,如果未能解决你的问题,请参考以下文章
如何以编程方式列出 C++ 或 Python 中的 DLL 依赖项?