Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven - transitive dependencies with different versions

  • Let's assume my application needs foo.jar and bar.jar
  • foo.jar needs version 1.0 of c.jar
  • bar.jar needs version 2.0 of c.jar

How does Maven resolve this conflict? Which version of c.jar will be used?

like image 448
Frizz Avatar asked Nov 05 '11 22:11

Frizz


People also ask

How do I override the version of a transitive dependency?

How do you do this if the wrong dependency is a transitive dependency? By taking advantage of Maven's nearest definition logic, developers can override the version of a dependency by declaring it on the root pom. xml file.

How does Maven resolve transitive dependencies?

Transitive Dependencies. Maven avoids the need to discover and specify the libraries that your own dependencies require by including transitive dependencies automatically. This feature is facilitated by reading the project files of your dependencies from the remote repositories specified.

How do I exclude a specific version of a dependency in Maven?

Multiple transitive dependencies can be excluded by using the <exclusion> tag for each of the dependency you want to exclude and placing all these exclusion tags inside the <exclusions> tag in pom. xml. You will need to mention the group id and artifact id of the dependency you wish to exclude in the exclusion tag.

How does Maven handle conflicting dependencies?

Enforcer can help developers solve dependency conflicts in Maven by analyzing all libraries declared in the POM file. The plugin uses a lot of different rules, but we are only interested in one: dependencyConvergence – ensures all dependencies converge to the same version.


1 Answers

It depends on the order of declaration in your effective POM. If foo.jar shows up first you will get version 1.0 of c.jar. If on the other hand bar.jar is declared first it will be version 2.0 of c.jar.

Relevant documentation:

...two dependency versions are at the same depth in the dependency tree, until Maven 2.0.8 it was not defined which one would win, but since Maven 2.0.9 it's the order in the declaration that counts: the first declaration wins

like image 170
Sri Sankaran Avatar answered Sep 24 '22 23:09

Sri Sankaran