List Gradle dependencies for all subprojects
Asked Answered
H

2

24

I can query the dependency tree for a Gradle project with ./gradlew -q dependencies.

I can also run the query for the service subproject with ./gradlew service:dependencies.

How can I list the dependencies automatically for all subprojects from the command line without modifying the build.gradle file?

Thank you in advance.

Hypocaust answered 22/1, 2021 at 20:24 Comment(0)
C
43

I believe there’s no built-in way in Gradle to achieve this (without adapting the build configuration) – unless you manually list the dependencies task for all subprojects as in:

./gradlew sub1:dependencies sub2:dependencies sub1:subsub:dependencies

However, if you need this feature often enough, then you could create a shell alias for it. Example in bash (e.g., put this in your ~/.bashrc file):

alias gradle-all-deps='./gradlew dependencies $(./gradlew -q projects \
    | grep -Fe ---\ Project \
    | sed -Ee "s/^.+--- Project '"'([^']+)'/\1:dependencies/"'")'

Then simply call gradle-all-deps from the root project directory.

Criminal answered 28/1, 2021 at 21:10 Comment(4)
this looks great! The sed syntax is incomplete I am afraid, because in some cases the prefix is \--- instead of +---Hypocaust
Thanks! It should already work for all subprojects: the + is a quantifier here, not a literal. But admittely I had missed the root project; I have updated my answer for that :-)Criminal
This is also answer How to list dependencies of a single Gradle submodule. Thank you.Dareen
With some tweaks to get consistent results across invocations, and to help find problems when they pop up: alias gradle-all-deps='./gradlew --no-parallel --stacktrace dependencies $(./gradlew -q projects \ | grep -Fe ---\ Project \ | sed -Ee "s/^.+--- Project '"'([^']+)'/\1:dependencies/"'" | sort)'Noaccount
A
32

Add following task in your root project's build.gradle file

subprojects {
    task allDeps(type: DependencyReportTask) {}
}

And execute the following command from the root project

./gradlew allDeps 

This will list dependencies of all subprojects.

Further if you want to narrow down the dependency graph on the basis of their type (compileClasspath, testRuntime ... ), please refer this

Alburnum answered 26/1, 2022 at 6:37 Comment(2)
elegant solution .. worked flawlessly in gradle 7.2.. not sure why this has less votes.. simple and effective.. just added this in root gradle file and listed all sub projects dependencies.. awesome..Spitter
@Spitter to be fair, the question did specify without modifying the build.gradle fileHibachi

© 2022 - 2024 — McMap. All rights reserved.