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.
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.
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With