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
- Highest version wins. Distance does not matter, so a transitive dependency five levels down can raise a version at the top. Maven does the opposite: the nearest definition wins.
- A constraint says what is acceptable. constraints { testImplementation('org.junit.jupiter:junit-jupiter-api:5.14.3') } states a version without adding a dependency.
- A strict version refuses to move. version { strictly '5.14.3' } fails the build instead of resolving to another version.
- A forced version overrides the rule. resolutionStrategy { force 'org.junit.jupiter:junit-jupiter-api:5.14.3' } in the configuration, or force = true on a dependency.
- Rich versions pick a variant. Gradle also reads 5.14.+ and the [5.0,6.0) range syntax, and a dynamic version is resolved at build time, which the lock files of a large build usually replace.
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
- Platforms and constraints: the BOM that makes the whole family agree.
- gradle dependencies and dependencyInsight: the two reports this lesson reads.
- Dependency conflicts in Maven: the same two versions, and the other rule.
Автор статьи: Arthur Isaev