Conflict resolution

Two dependencies of one build can ask for two versions of the same module. Gradle picks one, and its default rule is not the one Maven uses: the highest version wins, whatever the distance in the graph. The rule is printed with the graph, so a build never leaves you guessing.

Two versions on one graph

settings.gradle

rootProject.name = 'shop'

build.gradle

plugins { id 'java' } repositories { mavenLocal() } dependencies { testImplementation 'org.junit.jupiter:junit-jupiter:5.14.3' testImplementation 'org.junit.jupiter:junit-jupiter-api:5.11.0' 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.assertEquals; import org.junit.jupiter.api.Test; class SmokeTest { @Test void adds() { assertEquals(4, 2 + 2); } }

The build asks for junit-jupiter-api 5.11.0 directly, and the aggregate junit-jupiter 5.14.3 asks for 5.14.3. Both versions are in the local repository, so Gradle has a real choice to make:

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 |
   Selection reasons:
      - By conflict resolution: between versions 5.14.3 and 5.11.0

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 (*)

org.junit.jupiter:junit-jupiter-api:5.11.0 -> 5.14.3
\--- testCompileClasspath

(*) - 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 selection reason

Two lines of that report matter. Selection reasons: By conflict resolution: between versions 5.14.3 and 5.11.0 says that a rule picked one of two candidates instead of a request. And org.junit.jupiter:junit-jupiter-api:5.11.0 -> 5.14.3 says which version the build asked for and which one it got. The winner is the highest version, and the whole graph is then resolved against it.

The rules

The default of the highest version is a good one for a library that follows semantic versioning, and a build that needs the opposite writes the reason down. Either way the dependencyInsight report is where the answer is.

Related

Article author: Arthur Isaev