Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the information of transitive dependencies in a gradle task?

I want to get information of all dependencies (including transitive ones) in a gradle task.

I tried the code:

class MyGradlePlugin implements Plugin<Project> {

  void apply(Project project) {
    project.afterEvaluate {
      println "  Project:" + project.name
      project.configurations.each { conf ->
        println "    Configuration: ${conf.name}"
        conf.allDependencies.each { dep ->
          println "      ${dep.group}:${dep.name}:${dep.version}"
        }
      }
    }
  }

}

But it only prints the declared ones, no transitive ones.

That means, if my dependencies is:

dependencies {
    compile "com.google.guava:guava:18.0"
    compile 'org.codehaus.groovy:groovy-all:2.3.11'
    testCompile 'junit:junit:4.11'
}

It only prints these 3 dependencies, but the org.hamcrest:hamcrest-core:1.3 which is the transitive dependency of junit:junit:4.11 is not displayed.

How to modify the code to let it show org.hamcrest:hamcrest-core:1.3 as well?

PS: I know gradle dependencies task will show everything I want, but I need to get the dependencies information manually and print it in my own format.

like image 905
Freewind Avatar asked May 17 '15 14:05

Freewind


People also ask

How do you handle transitive dependencies in Gradle?

A variant of a component can have dependencies on other modules to work properly, so-called transitive dependencies. Releases of a module hosted on a repository can provide metadata to declare those transitive dependencies. By default, Gradle resolves transitive dependencies automatically.

Does Gradle support transitive dependencies?

Gradle automatically resolves those additional modules, so called transitive dependencies. If needed, you can customize the behavior the handling of transitive dependencies to your project's requirements. Projects with tens or hundreds of declared dependencies can easily suffer from dependency hell.


1 Answers

Finally, I figure it out with follow task

class Dep {
  String group
  String name
  String version
  String extention
  String classifier

  Dep(String group, String name, String version, String extension, String classifier) {
    this.group = group
    this.name = name
    this.version = version
    this.extention = extension
    this.classifier = classifier
  }
}

task collectAllDeps {
  def deps = []
  configurations.each {
  conf ->
    if (conf.isCanBeResolved()) {
      conf.getResolvedConfiguration().getResolvedArtifacts().each {
        at ->
           def dep = at.getModuleVersion().getId()
           println at.getFile().getAbsolutePath()
           //          dep = dep1.getComponentIdentifier()
           println "$dep.group:$dep.name:$dep.version"
           deps.add(new Dep(dep.group, dep.name, dep.version, at.extension, at.classifier))
      }
    }
  }
  def json = groovy.json.JsonOutput.toJson(deps)
  json = groovy.json.JsonOutput.prettyPrint(json)
  new File("deps.json") << json
}
like image 96
cosyman Avatar answered Oct 19 '22 07:10

cosyman