Sub topic 14
Transitive Dependencies and Version Conflicts
When you add one library to your project, you often get more than one. This chapter explains why that happens, what goes wrong when two libraries want different versions of the same thing, and how to 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 that one of your dependencies needs. The build tool downloads it for you automatically.
For example, say your app uses Library A, and Library A uses Library C. You never mention C, but it still ends up on your classpath. The classpath is the list of places Java looks for classes.
What a Version Conflict Is
A version conflict happens when two paths in the tree ask for different versions of the same library. Only one version can be on the classpath, so the build tool must pick one.
| Tool | Default rule | Winner in the diagram |
|---|---|---|
| Maven | "Nearest wins": the version closest to your project in the tree. If two are equally close, the one declared first wins. | C 1.0 (one step away from A) |
| Gradle | "Highest wins": the newest version requested anywhere. | C 2.0 |
Neither rule is always right. If Library D calls a method that exists only in C 2.0, Maven's choice can crash at runtime with a NoSuchMethodError.
Seeing the Tree
You can't fix a conflict you can't see. Both tools print a dependency tree, a report that shows every library and who brought it in.
mvn dependency:tree
./gradlew dependencies --configuration runtimeClasspathIn Gradle's output, an arrow such as 1.0 -> 2.0 means version 1.0 was requested but 2.0 was chosen. In Maven, add -Dverbose to see entries marked "omitted for conflict".
Fixing a Conflict
Exclude the unwanted library
An exclusion tells the tool not to bring in one transitive dependency. In Maven:
<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>The same idea in Gradle:
dependencies {
implementation("com.example:some-library:2.0.0") {
exclude(group = "commons-logging", module = "commons-logging")
}
}Declare the version directly
Under Maven's rule, a dependency you list yourself sits at the top of the tree, so its version always wins. Adding Library C 2.0 as a direct dependency settles the conflict.
Exercise
Your Maven project depends on Library X, which needs Guava 30. It also depends on Library Y, which depends on Library Z, which needs Guava 33. Which Guava version does Maven pick, and how could you make it use 33?
Show answer
Maven picks Guava 30, because it is two steps away (X then Guava) while Guava 33 is three steps away (Y, Z, then Guava). To use 33, declare Guava 33 as a direct dependency in pom.xml, then check the result with mvn dependency:tree.
Exercise
In Gradle's dependency report you see "commons-io:commons-io:2.11.0 -> 2.16.1". What does this mean?
Show answer
Some library asked for commons-io 2.11.0, but Gradle chose 2.16.1 because another path requested that higher version.