Scrivio Interview library
InternalsFoundations6 min read · updated 12 November 2026

How does Maven choose a version when two paths in the dependency graph bring different versions of the same artifact?

Maven uses nearest-wins mediation: of all the versions of an artifact in the dependency tree, it picks the one closest to our project, and at equal depth the one declared first. It does not pick the highest version, so an older version can win and break code compiled against a newer one. We take control with dependencyManagement, which overrides the version for transitive dependencies, or by declaring the dependency directly, which always wins because it sits at depth one.

Why this question is asked

Almost every real project pulls the same library in through several paths: our web framework wants one Jackson version, a client library wants another. Only one jar can be on the classpath, so the build tool must pick. Interviewers ask how Maven picks because its rule is simple but surprising, and the surprise causes runtime errors that look like bugs in our code. In this answer, we will recap the dependency tree, see the nearest-wins rule on an example, look at the tie-break, see why the highest version does not always win, and then take control with dependencyManagement and direct declarations.

1A Quick Recap of the Dependency Tree

When we declare a dependency in our POM, it usually brings its own dependencies, which bring theirs. These are called transitive dependencies. Maven reads all their POMs and builds a tree with our project at the root. The depth of a node is how many steps it is from the root: our direct dependencies are at depth one, their dependencies at depth two, and so on.

The same artifact, identified by groupId and artifactId, can appear in several places in the tree with different versions. Since the classpath can hold only one copy of each class, Maven must choose a single version. That choice is called dependency mediation.

2Nearest Wins: The Rule on an Example

Suppose our application depends on library A and library B. A depends on json-lib 2.0 directly, so json-lib 2.0 is at depth two. B depends on C, which depends on json-lib 2.5, so json-lib 2.5 is at depth three.

Shell
$ mvn dependency:tree -Dverbose -Dincludes=org.example:json-lib
com.acme:app:jar:1.0.0
+- org.example:lib-a:jar:1.0:compile
| \- org.example:json-lib:jar:2.0:compile <- depth 2, chosen
\- org.example:lib-b:jar:1.0:compile
\- org.example:lib-c:jar:1.0:compile
\- (org.example:json-lib:jar:2.5:compile - omitted for conflict with 2.0) <- depth 3
Which json-lib version wins
PathVersionDepthResult
app -> lib-a -> json-lib2.02Chosen: nearest
app -> lib-b -> lib-c -> json-lib2.53Omitted for conflict

Maven picks 2.0, the version nearest to the root, even though 2.5 is newer. The reason for this rule is predictability: it depends only on the shape of the tree, so we can always see which version wins and change the outcome by moving a declaration closer to the root. The -Dverbose flag shows the losing versions with the reason they were omitted, which is the quickest way to see mediation at work.

3At Equal Depth, the First Declaration Wins

If two versions are at the same depth, distance cannot decide, so Maven picks the one reached first, in the order the dependencies are declared in our POM. Swapping two lines in the dependencies section can therefore change which version ends up on the classpath.

XML
<dependencies>
<dependency><artifactId>lib-x</artifactId></dependency> <!-- brings json-lib 2.1 at depth 2: chosen -->
<dependency><artifactId>lib-y</artifactId></dependency> <!-- brings json-lib 2.4 at depth 2: omitted -->
</dependencies>
<!-- swap the two lines and json-lib 2.4 wins instead -->

Notice how fragile this is. Someone tidies the POM by sorting dependencies alphabetically, and the application starts failing at runtime with no change in versions at all. That is a strong hint to make important versions explicit instead of relying on the tie-break.

4Why the Highest Version Does Not Always Win

Many developers expect the newest version to win, because newer versions are usually backward compatible. Maven does not assume that, and the result can bite. In our first example, lib-c was compiled against json-lib 2.5 and may call a method that was added in 2.5. At runtime only 2.0 is on the classpath, so when lib-c calls that method, the JVM throws java.lang.NoSuchMethodError. The compiler never complained, because lib-c was compiled long ago against its own version.

So mediation errors usually show up at runtime, in a code path that uses the newer feature, and often only in some tests or in production. That is why we want to find and fix conflicts at build time.

5Taking Control With dependencyManagement and Direct Declarations

The dependencyManagement section lets us say 'whenever json-lib appears anywhere in the tree, use this version'. It does not add the dependency; it only fixes its version, and it overrides mediation for transitive occurrences. This is exactly how BOMs such as spring-boot-dependencies work: they are long dependencyManagement sections that we import.

XML
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>json-lib</artifactId>
<version>2.5</version> <!-- every path now resolves to 2.5 -->
</dependency>
</dependencies>
</dependencyManagement>
$ mvn dependency:tree -Dincludes=org.example:json-lib
# \- org.example:json-lib:jar:2.5:compile (version managed from 2.0)
Step throughHow Maven settles a version
  1. Is the artifact in dependencyManagement?
    YesUse the managed versionNoMediate between the versions found
  2. Is one version declared directly?
    YesDirect declaration wins (depth 1)NoPick the nearest in the tree
  3. Tie at the same depth?
    YesFirst declared winsNoDone

3 steps in order. Play them, or step through them one at a time.

The other way is to declare json-lib directly in our dependencies. A direct dependency is at depth one, which is always the nearest, so it always wins. The difference is intent: a direct declaration says our code uses the library, while dependencyManagement only says which version to use if anyone needs it. When our code does not use the library itself, dependencyManagement is the cleaner choice.

To find conflicts before they reach runtime, the Maven Enforcer plugin has rules such as dependencyConvergence, which fails the build when two different versions of an artifact meet in the tree, and requireUpperBoundDeps, which fails when the chosen version is lower than one that some dependency asked for.

6Follow-up questions

How does this differ from Gradle's default?

Gradle's default conflict resolution picks the highest version among all the versions requested in the graph, regardless of depth. In our example, Gradle would choose json-lib 2.5. That avoids the 'older version wins' surprise, but it can silently upgrade a library past what our direct dependencies were tested with. Both tools let us take control: Gradle with constraints, platforms and strict versions, Maven with dependencyManagement and BOMs.

Does dependencyManagement also change the version of my direct dependencies?

If a direct dependency declares no version, it takes the managed one, which is how starters work without version numbers under the Spring Boot parent. If the direct dependency declares its own version, that explicit version is used for it. For transitive occurrences, the managed version always applies.

When would you use an exclusion instead?

When we do not want the transitive dependency at all, for example a logging implementation we replace, or a library that duplicates classes already provided elsewhere. An exclusion removes that path from the tree. If we only want a different version, dependencyManagement is better, because an exclusion on one path does nothing about other paths.

7Conclusion

In this answer, we saw that Maven mediates version conflicts with nearest wins, using declaration order to break ties at the same depth. It does not pick the highest version, so an older version can win and cause NoSuchMethodError at runtime. We take control with dependencyManagement, which overrides the version wherever the artifact appears, or with a direct declaration, which always wins at depth one, and we catch conflicts early with the Enforcer rules.

What to say in the interviewAbout 80 seconds aloud

Open with

Maven builds a tree of all dependencies with my project at the root. When the same artifact shows up with different versions, it uses what is called nearest-wins mediation: the version at the smallest depth wins.

Then, if they want more

If two versions are at the same depth, the one declared first in the POM wins, so even reordering dependencies can change the result. The important consequence is that Maven does not pick the highest version.

If library A brings json-lib 2.0 at depth two, and a deeper library was compiled against 2.5, Maven still picks 2.0, and when that deeper library calls a method added in 2.5 we get NoSuchMethodError at runtime. I would check this with mvn dependency:tree -Dverbose, which shows the omitted versions and why.

To take control, I put the version in dependencyManagement, which overrides it for every transitive occurrence without adding the dependency, and that is exactly what BOMs like Spring Boot's do. Or, if my code uses the library directly, I declare it as a direct dependency, which is at depth one and always wins.

I would also add the Enforcer rules dependencyConvergence or requireUpperBoundDeps so conflicts fail the build instead of production. Gradle is different by default: it picks the highest requested version regardless of depth.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Apache Maven guides
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How does Maven choose a version when two paths in the dependency graph bring different...": https://scrivio-interview-library.netlify.app/java/maven/8083e042e4/