gradle dependencies and dependencyInsight

A dependency pulls in others, and the graph that comes out is not the one the build script wrote. Two commands print it: gradle dependencies prints the whole graph of one configuration, and dependencyInsight answers the one question a graph raises, which is why a module has the version it has.

The project

settings.gradle

rootProject.name = 'shop'

build.gradle

plugins { id 'java' } repositories { mavenLocal() } dependencies { testImplementation 'org.junit.jupiter:junit-jupiter:5.14.3' testRuntimeOnly 'org.junit.platform:junit-platform-launcher:1.14.3' } tasks.named('test') { useJUnitPlatform() }

src/test/java/com/example/SmokeTest.java

package com.example; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; class SmokeTest { @Test void runs() { assertTrue(true); } }

gradle dependencies

The command needs a configuration: testCompileClasspath is the one the test sources are compiled against, and testRuntimeClasspath is the one the tests run with.

gradle -q dependencies --configuration testCompileClasspath --console=plain --offline --no-daemon

------------------------------------------------------------
Root project 'shop'
------------------------------------------------------------

testCompileClasspath - Compile classpath for source set 'test'.
\--- org.junit.jupiter:junit-jupiter:5.14.3
     +--- org.junit.jupiter:junit-jupiter-api:5.14.3
     | +--- org.opentest4j:opentest4j:1.3.0
     | +--- org.junit.platform:junit-platform-commons:1.14.3
     | | \--- org.apiguardian:apiguardian-api:1.1.2
     | \--- org.apiguardian:apiguardian-api:1.1.2
     \--- org.junit.jupiter:junit-jupiter-params:5.14.3
          +--- org.junit.jupiter:junit-jupiter-api:5.14.3 (*)
          \--- org.apiguardian:apiguardian-api:1.1.2

(*) - Indicates repeated occurrences of a transitive dependency subtree. Gradle expands transitive dependency subtrees only once per project; repeat occurrences only display the root of the subtree, followed by this annotation.

A web-based, searchable dependency report is available by adding the --scan option.

One declared dependency, six modules in the graph. The tree is the reason the resolved graph is interesting: junit-jupiter is an aggregate that pulls junit-jupiter-api and junit-jupiter-params, and each of them pulls its own dependencies. The (*) means a subtree already printed above.

dependencyInsight

gradle -q dependencyInsight --dependency junit-jupiter-api --configuration testCompileClasspath --console=plain --offline --no-daemon

org.junit.jupiter:junit-jupiter-api:5.14.3
  Variant compile:
    | Attribute Name | Provided | Requested |
    |--------------------------------|----------|--------------|
    | org.gradle.status | release | |
    | org.gradle.category | library | library |
    | org.gradle.libraryelements | jar | classes |
    | org.gradle.usage | java-api | java-api |
    | org.gradle.dependency.bundling | | external |
    | org.gradle.jvm.environment | | standard-jvm |
    | org.gradle.jvm.version | | 21 |

org.junit.jupiter:junit-jupiter-api:5.14.3
+--- org.junit.jupiter:junit-jupiter:5.14.3
| \--- testCompileClasspath
\--- org.junit.jupiter:junit-jupiter-params:5.14.3
     \--- org.junit.jupiter:junit-jupiter:5.14.3 (*)

(*) - Indicates repeated occurrences of a transitive dependency subtree. Gradle expands transitive dependency subtrees only once per project; repeat occurrences only display the root of the subtree, followed by this annotation.

A web-based, searchable dependency report is available by adding the --scan option.

The report answers three questions about one module. Which version won and through which variant it was selected. Which paths in the graph ask for it, with the arrow that shows the request that brought it in. And, when two versions meet, the selection reason: here every request asked for 5.14.3, so there was nothing to resolve. The next lesson makes the two versions differ and the reason appear.

Related

Article author: Arthur Isaev