2016-04-10 22 views
5

wiem, że robi:Czy istnieje sposób, aby programowo wyświetlić listę wszystkich zależności gradle?

gradle dependencies 

wymienia pełną drzewa zależności. Teraz szukam sposobu, aby programowo manipulować tym drzewem zależności, tak aby móc wydrukować tę samą hierarchię, ale w JSON zamiast formatu gradli cli używa teraz w konsoli.

Czy ktoś wie, jakie są świetne klasy, które powinienem użyć, aby to osiągnąć?

EDITED

chciałbym uzyskać (w JSON) niektórzy tak:

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

Jak widać tutaj z tym wiem, który zależność zależy od innych zależności przechodni.

Odpowiedz

10

Hi tym wszystkim jest to, jak skończyło się na archiwizację, co potrzebne i mam nadzieję, że będą przydatne dla pozostali.

Po pierwsze chciałbym podziękować "pczeus" i "Björn Kautler" za ich odpowiedzi, które pomogły mi znaleźć to rozwiązanie.

Tak, to jak mogę rozwiązać mój problem:

Biorąc pod uwagę to 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' 
} 

Jeśli zrobić:

 
gradle -b build.gradle dependencies --configuration=compile 

Dostaniesz to wyjście w konsola:

 
: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) 

Chciałem uzyskać to samo "drzewo zależności", ale w formacie JSON. Więc jest to "zadanie" I stworzył za to, że:

 

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 
    } 
} 

Jeśli zabrakło tego zadania:

 
gradle -b build.gradle printSolvedDepsTreeInJson 

Dostaniesz to:

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

który jest JSON reprezentacja drzewa zależności, którego potrzebowałem. Teraz, gdy przyjrzysz się uważniej, zauważysz, że nie jest to rzeczywista lista zależności zdefiniowanych w pliku build.gradle.To jest drzewo zależności RESOLVED. Oznacza to, że niektóre zależności uległy zmianie.

Na przykład zależność Firstlevel:

 
org.javassist:javassist:3.13.0-GA 

została zmieniona na:

 
org.javassist:javassist:3.20.0-GA 

Jak

 
org.hibernate:hibernate-core:5.1.0.Final 

zależy od:

 
org.javassist:javassist:3.20.0-GA 

który jest wyższy niż wersja:

 
org.javassist:javassist:3.13.0-GA 

I Gradle po domyślny algorytm rozwiązywania konfliktów wybiera zawsze z „najnowszej” wersji.

Właściwie to co:

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

czyli w wyjścia konsoli. 3.13.0-GA został zastąpiony wersją 3.20.0-GA.

To jest problem, ponieważ nie otrzymuję rzeczywistego "drzewa zależności". Dostaję "rozwiązany".

skończyło się na ustalenie tej sytuacji definiując kolejne zadanie:

 
task printDepsTreeInJson { 
    doLast { 
    configurations.compile.incoming.getResolutionResult().getAllDependencies().each { depResult -> 
     println "{\"from\":\"" + depResult.getFrom() + "\"," + "\"requested\":\"" + depResult.getRequested() + "\"}" 
    } 
    } 
} 

Jeśli wykonasz to:

 
gradle -b build.gradle printDepsTreeInJson 

Będziesz teraz uzyskać to:

 
: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"} 

Jest nie jest ostatnim "drzewem zależności" (skonstruowałem je za pomocą javascript), ale właśnie to trzeba wygenerować!

"od" to zależność, która zażądała innej zależności, a "zażądano" jest żądaną zależnością rzeczywistą! :)

Jeśli

 
"from":"project :" 

Oznacza to, że zależność jest "Pierwszy poziom" zależność. (A root)

Wszystkie inne zależności będzie tak:

 
{"from":"org.hibernate:hibernate-core:5.1.0.Final","requested":"org.jboss.logging:jboss-logging:3.3.0.Final"} 

Zauważ, że mam teraz dwa

 
org.javassist:javassist 

Każdy z nich wiąże się z zależnością, że faktycznie o to. Jednym z nich jest „Pierwszy poziom” zależność:

 
{"from":"project :","requested":"org.javassist:javassist:3.13.0-GA"} 

a drugi został poproszony przez Hibernacja:

 
{"from":"org.hibernate:hibernate-core:5.1.0.Final","requested":"org.javassist:javassist:3.20.0-GA"} 

byłem zbyt leniwy, aby wygenerować drzewa zależności w JSON w tym samym zadaniu :) Ale jasne jest, że jest to droga, którą należy wykonać, jeśli z jakiegoś powodu trzeba przeanalizować "oryginalne" (poprzednie/nierozwiązane) drzewo zależności.

To jest zawartość pliku końcowy build.gradle w przypadku, gdy chcesz skopiować & pasty i wypróbować:

 
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 
    } 
} 
2

Najłatwiej będzie prawdopodobnie napisać własny DependencyReportRenderer wdrożenie, a następnie albo skonfigurować istniejący dependencies zadanie go używać, lub zdefiniować nowe zadanie typu DependencyReportTask z własnego renderera skonfigurowany.

+0

Dziękujemy! Przyjrzę się klasie, którą opisałeś. Czy znasz jakieś dobre IDE do debugowania skryptów Gradle? –

+0

Jak skonfigurować zadanie, aby używać określonego Renderowania? –

+0

Używam IntelliJ IDEA. Jest to najlepsze środowisko Java IDE pod słońcem, a także znakomite w pisaniu i debugowaniu skryptów Gradle. Cóż, coś w stylu 'task jsonDeps (type: DependencyReportTask) {renderer new MyOwnRenderer()}'. Ale to jest w mojej głowie, nie próbowałem tego jeszcze. – Vampire

10

Można utworzyć zadanie w pliku Gradle, aby wykonać iterację po wszystkich zależnościach i wygenerować JSON według własnego uznania. Oto przykład zadania, które będą dość wydrukować JSON dla Ciebie:

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 
} 

Z wyjściem próbki:

"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" 
     } 
    } 
] 
+0

Zaktualizowałem odpowiedź, aby pokazać ładny przykład drukowania wszystkich rozwiązanych zależności w formacie JSON. – pczeus

+0

pczeus dziękuję bardzo za tak szczegółową odpowiedź! Potrzebowałbym jednak znać zależności tych zależności, aby móc manipulować drzewem (co jest tym, co mnie interesuje). Proszę ponownie zobaczyć to pytanie. Zmieniłem to, aby lepiej opisać, jaki jest oczekiwany wynik, jaki próbuję uzyskać. Jeszcze raz dziękuję! –

+0

Bez problemu. Jeśli spojrzysz na dokumenty Groovy dla [Konfiguracja] (https://docs.gradle.org/current/javadoc/org/gradle/api/artifacts/ConfigurationContainer.html) Możesz zobaczyć, że aplikacja uzyska wszystkie uprawnienia do wszystkich konfiguracji . Ponadto [resolvedConfiguration] (https://docs.gradle.org/current/javadoc/org/gradle/api/artifacts/ResolvedConfiguration.html) ma interfejs API, aby uzyskać zależności pierwszego poziomu i wszystkie elementy podrzędne. Tak więc, powinieneś być w stanie dostosować zadanie, aby nawigować po drzewie zgodnie z potrzebami. " – pczeus