Michal Kordas Michal Kordas - 3 days ago 5
Groovy Question

List original declared scopes for dependency in Gradle

I'd like to programmatically list all scopes where given dependency was declared.

For example having in

build.gradle
:

dependencies {
compile 'junit:junit:4.12'
runtime "commons-io:commons-io:2.5"
}


I'd like to have information that
commons-io:commons-io:2.5
has
runtime
scope.

However, if I create the following task:

task runtimeDeps << {
configurations.findByName('runtime').allDependencies.each { println it }
}


I'm getting output:

DefaultExternalModuleDependency{group='commons-io', name='commons-io', version='2.5', configuration='default'}
DefaultExternalModuleDependency{group='junit', name='junit', version='4.12', configuration='default'}


Which is too much, as
junit
was declared with
compile
. How can I acquire information about the original scope of dependency?

Answer

Instead of allDependencies use dependencies:

task runtimeDeps << {
    configurations.findByName('runtime').dependencies.each { println it }
}

allDependencies returns dependencies also for scope that runtime extends, namely compile.

Comments