18

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.

informatik01
  • 16,038
  • 10
  • 74
  • 104
erdos
  • 3,135
  • 2
  • 16
  • 27

2 Answers2

39

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.

Chriki
  • 15,638
  • 3
  • 51
  • 66
  • 1
    this looks great! The sed syntax is incomplete I am afraid, because in some cases the prefix is \--- instead of +--- – erdos Jan 29 '21 at 09:59
  • 1
    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 :-) – Chriki Jan 29 '21 at 10:53
  • This is also answer *How to list dependencies of a single Gradle submodule*. Thank you. – jspetrak Dec 30 '22 at 10:13
20

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

Vijender Kumar
  • 1,285
  • 2
  • 17
  • 26
  • 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.. – kaluva Oct 29 '22 at 08:11
  • @kaluva to be fair, the question did specify **without modifying the build.gradle file** – Edd May 15 '23 at 10:37