Sub topic 14
Transitive Dependencies and Version Conflicts
In earlier chapters you added dependencies such as JUnit and Guava with a single line each. The libraries you add often need other libraries in order to work, and those libraries can need more. This chapter explains how Maven and Gradle handle that chain, what happens when two libraries ask for different versions of the same thing, and how you can look at the result and fix it.
What a Transitive Dependency Is
A direct dependency is a library you list yourself in pom.xml or build.gradle.kts. A transitive dependency is a library your build pulls in because one of your direct dependencies needs it. The word "transitive" means "passed along."
Every library published to Maven Central ships with its own POM file, which lists the library's own dependencies. When your build tool downloads a library, it reads that POM and downloads whatever it lists. Then it reads those libraries' POMs, and keeps going until nothing new turns up. The full result is called the dependency graph. A graph here just means a set of items joined by "needs" arrows.
This saves you a lot of work. You add Guava with one line, and the build tool finds and downloads the small helper libraries Guava needs, with no extra lines from you.
Seeing the Whole Tree
You cannot fix what you cannot see, so both tools give you a report of the dependency tree. You saw these commands earlier in the tutorial:
mvn dependency:tree
./gradlew dependencies --configuration runtimeClasspathMaven prints each library with the libraries it brought in indented beneath it. Gradle prints a similar tree for one configuration, which is a named group of dependencies such as runtimeClasspath.
When the tree is long, you can narrow it to one library:
mvn dependency:tree -Dincludes=commons-logging:commons-logging
./gradlew dependencyInsight --dependency commons-logging --configuration runtimeClasspathThe Maven -Dincludes filter shows only the paths that lead to the library you name. The Gradle dependencyInsight task shows which version was picked for one library and which dependencies asked for it.
How a Version Conflict Happens
A version conflict happens when two paths in the graph ask for different versions of the same library. A Java program can only load one copy of a class with a given name, so the build tool has to pick one version.
Here is a made-up example. Your app uses two libraries, A and B. Both need a library called C, but they want different versions.
Maven and Gradle use different rules to settle this conflict, so the same set of dependencies can give a different result in each tool.
Maven's Rule: Nearest Wins
Maven uses nearest definition. It counts how many steps each version sits from your project in the tree and picks the closest one. If two versions are the same distance away, the one whose path appears first in your pom.xml wins.
In the diagram, both copies of C are two steps away. If you declared A before B, Maven picks C 1.0, even though 2.0 is newer. Library B may then fail at runtime with an error such as NoSuchMethodError, because it calls a method that only exists in C 2.0.
Gradle's Rule: Highest Version Wins
Gradle picks the highest version it finds anywhere in the graph. In the same example, Gradle picks C 2.0. The tree report shows the change with an arrow:
+--- com.example:library-b:1.0
| \--- com.example:library-c:2.0
\--- com.example:library-a:1.0
\--- com.example:library-c:1.0 -> 2.0The 1.0 -> 2.0 means "library A asked for 1.0, but Gradle chose 2.0."
| Situation | Maven picks | Gradle picks |
|---|---|---|
| Same depth, A declared first | C 1.0 (first declared) | C 2.0 (highest) |
| You declare C 1.5 directly | C 1.5 (nearest) | C 2.0 (highest) |
| C pinned with a managed version or constraint | The pinned version | The pinned version, if no other request is higher than a strict pin |
Fixing Conflicts in Maven
Pin the version with dependencyManagement
The <dependencyManagement> section sets the version Maven must use for a library wherever it shows up in the tree. It does not add the library on its own. It only controls the version.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>library-c</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
</dependencyManagement>Put this inside <project>, next to your normal <dependencies> section. After this change, both A and B get C 2.0.
Exclude a library you do not want
Sometimes you want a transitive library gone completely, for example a logging library that clashes with the one you chose. You saw this in the tutorial plan:
<dependency>
<groupId>com.example</groupId>
<artifactId>some-library</artifactId>
<version>2.0.0</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>An exclusion only cuts the path through some-library. If another dependency also brings in commons-logging, you need to exclude it there too.
Fixing Conflicts in Gradle
Exclude a library
The Kotlin DSL puts the exclusion in a block after the dependency:
dependencies {
implementation("com.example:some-library:2.0.0") {
exclude(group = "commons-logging", module = "commons-logging")
}
}Gradle uses the word module where Maven uses artifactId.
Set a version with a constraint
A dependency constraint tells Gradle which version of a library to use if anything in the graph asks for it. Like Maven's dependencyManagement, it does not add the library by itself.
dependencies {
implementation("com.example:library-a:1.0")
implementation("com.example:library-b:1.0")
constraints {
implementation("com.example:library-c:2.0") {
because("library-b needs methods added in 2.0")
}
}
}The because text shows up in dependencyInsight output, so teammates can see why the rule exists.
A plain constraint still takes part in "highest wins." If you must force a lower version, mark it strict:
dependencies {
constraints {
implementation("com.example:library-c") {
version {
strictly("1.5")
}
}
}
}Use strictly rarely. If a library really needs a newer version, forcing an older one moves the failure from build time to runtime.
A Safe Workflow
- Run the tree report and find the library in question.
- Read which paths ask for which versions.
- Choose one fix: pin the version, or exclude the library.
- Run the tree report again to confirm the result.
- Run the full build with tests,
./mvnw clean verifyor./gradlew build.
Exercise
Your Maven project declares library A first and library B second. A needs C 1.0, and B needs C 2.0, both at the same depth. Which version of C does Maven choose, which does Gradle choose, and how would you make Maven use 2.0?
Show answer
Maven chooses C 1.0, because both versions are equally near and A is declared first. Gradle chooses C 2.0, because it picks the highest version. To make Maven use 2.0, add C version 2.0 to the dependencyManagement section of the pom.xml (or declare C 2.0 as a direct dependency so it is nearest), then run mvn dependency:tree to confirm.
Exercise
A Gradle tree report shows "org.slf4j:slf4j-api:1.7.36 -> 2.0.13". What does the arrow mean, and which command shows which dependency asked for 2.0.13?
Show answer
Some dependency asked for 1.7.36, but Gradle resolved the conflict by choosing the higher version, 2.0.13. Run ./gradlew dependencyInsight --dependency slf4j-api --configuration runtimeClasspath to see every request for that library and why 2.0.13 was selected.