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
}
来源:https://stackoverflow.com/questions/30287826/how-to-get-the-information-of-transitive-dependencies-in-a-gradle-task