如何获取渐进依赖关系的信息? [英] How to get the information of transitive dependencies in a gradle task?

查看:195
本文介绍了如何获取渐进依赖关系的信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在毕业生任务中获取所有依赖关系的信息(包括传递性的信息)。



我尝试过代码:

  class MyGradlePlugin implements Plugin< Project> ; {

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

}

但它只打印已声明的,不传递的。 >

这意味着如果我的依赖关系是:

 依赖关系{
compilecom.google.guava:guava:18.0
compile'org.codehaus.groovy:groovy-all:2.3.11'
testCompile 'junit:junit:4.11'
}

它只打印这3个依赖关系, org.hamcrest:hamcrest-core:1.3 这是不显示 junit:junit:4.11 的传递依赖关系。



如何修改代码让它显示 org.hamcrest:hamcrest-core:1.3 / p>

PS:我知道 gradle依赖性任务将显示我想要的一切,但是我需要手动获取依赖关系信息,以我自己的格式打印。

解决方案

最后,我通过跟随任务

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

Dep(String group,String name,String版本,字符串扩展名,String分类器){
this.group = group
this.name = name
this.version = version
this.extention = extension
this .classifier = classifier
}
}

任务collectAllDeps<< {

def deps = []
configured.each {
conf - >
conf.getResolvedConfiguration()。getResolvedArtifacts()。每个{
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
}


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.

解决方案

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

这篇关于如何获取渐进依赖关系的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆