checketts
checketts

Reputation: 14943

How can I get a list of my projects dependencies in a flattened form using Gradle?

I know that Gradle has the excellent dependencies task that lists out all dependencies for a project. However, it returns them in a tree listing.

I would like to get a list of all my dependencies as they are resolved in just a flat list. Similar to how the Maven dependency plugin list goal behaves.

Upvotes: 17

Views: 3562

Answers (4)

amram99
amram99

Reputation: 709

Here's a hack that simplifies things if you are using Android Studio or Intellij. Open the Project view and expand External Libraries. From here, select all Gradle ones and copy/paste to a text file. The output should look like this:

...
Gradle: androidx.localbroadcastmanager:localbroadcastmanager:1.0.0@aar
Gradle: androidx.navigation:navigation-common:2.5.3@aar
Gradle: androidx.navigation:navigation-common-ktx:2.5.3@aar
...

I suspect this corresponds to the compileClasspath Gradle configuration, but I can't be certain.

Upvotes: 0

checketts
checketts

Reputation: 14943

Here is a short task that meets that need:

task('dependenciesList') <<  {
    println "Compile dependencies"
    def selectedDeps = project.configurations.compile.incoming.resolutionResult.allDependencies.collect { dep ->
        "${dep.selected}"
    }
    selectedDeps.unique().sort().each { println it}
}

The third line is the interesting part. You need to get the configuration you care about (compile) then instead of getting dependencies there, the incoming.resolutionResult will provide the resolved values and versions.


<< was removed in Gradle 5. To make the task work in Gradle 5 and later versions, remove << and use doLast { } instead. Also, use runtimeClasspath or compileClasspath for the configuration instead of compile:

task('dependenciesList') {
    doLast {
        println "Compile dependencies"
        def selectedDeps = project.configurations.compileClasspath.incoming.resolutionResult.allDependencies.collect { dep ->
            "${dep.selected}"
        }
        selectedDeps.unique().sort().each { println it}
    }
}

Upvotes: 13

smuk
smuk

Reputation: 351

Without modifying the build, flatten the tree using sed, sort, and uniq as follows:

$ gradle dependencies | sed 's/^.* //' | sort | uniq

Alternatively, with slightly tighter sed matching:

./gradlew dependencies \
  | sed -n 's/.*--- \([^ ]*\).*/\1/p' \
  | grep -v "^project$" \
  | sort \
  | uniq

Upvotes: 14

lirui
lirui

Reputation: 1463

Thanks for the answers already supplied.

Finally I complete it by a more standard way:

project.gradle.addListener(new DependencyResolutionListener() {
    
    @Override
    void beforeResolve(ResolvableDependencies dependencies) {}
    
    @Override
    void afterResolve(ResolvableDependencies dependencies) {
        dependencies.resolutionResult.allComponents.each { select ->
            println "selected component: ${select} " + select.selectionReason
        }
    }
})

Project implementation will also be resolved in this way, and the final selected component version will be resolved correctly.

Upvotes: 1

Related Questions