Java › Build Tools: Maven & Gradle
How does Maven's dependency resolution algorithm handle transitive dependency version conflicts, and what is the "nearest definition wins" rule?
AdvancedAnswer
When multiple dependencies (directly or transitively) require different versions of the same library, Maven resolves the conflict using "nearest definition wins": the version declared at the shallowest depth in the dependency tree (closest to your project's own pom.xml) takes precedence, with ties broken by declaration order -- this can silently select an unexpected or incompatible version, so Maven provides `mvn dependency:tree` to visualize the actual resolved tree and `<dependencyManagement>` or explicit `<exclusions>` to override the automatic resolution when the default nearest-wins choice is wrong for your project.
<!-- Forcing a specific version regardless of transitive resolution -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.2</version> <!-- forces this version project-wide -->
</dependency>
</dependencies>
</dependencyManagement>
# Diagnose the actual resolved dependency tree
mvn dependency:tree -DverboseReal-world example A team debugging a mysterious NoSuchMethodError in production traces it to Maven's nearest-wins rule silently selecting an older, incompatible version of a transitive dependency over the newer version the team actually needed, resolved by explicitly pinning the correct version via dependencyManagement.