Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do newer versions of dependencies override older versions in maven projects?

Tags:

java

maven

If I have a maven project which has explicit dependencies on A and B version 2.0 and A has transitive dependency on B version 1.0. Does newer version of B override older version? I used maven depencdy:resolve goal and it looks like older version of B is not resolved. What if A is incompatible with newer version of B? Or if A depends on B version 2.0 and my project has explicit dependency on B version 1.0 after running dependency:resolve goal I don't see newer version of B then. So how do these dependencies get resolved then?

And when I use resolve goal it shows dependencies. But what phase this dependencies will be used in? Compile, test, runtime?

like image 356
user1745356 Avatar asked Nov 03 '13 14:11

user1745356


People also ask

Does Maven override dependency version?

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 decide which version of a dependency to use?

Maven picks the "nearest definition". That is, it uses the version of the closest dependency to your project in the tree of dependencies. You can always guarantee a version by declaring it explicitly in your project's POM.

What happens if you don't specify the version of Maven dependency?

Each maven dependency defined in the pom must have a version either directly or indirectly for example, through dependencyManagement or parent. That being said, if the version is not given, then the version provided in the dependencyManagement or the parent pom will be used.

How do dependencies work in Maven?

Dependencies are external JAR files (Java libraries) that your project uses. If the dependencies are not found in the local Maven repository, Maven downloads them from a central Maven repository and puts them in your local repository. The local repository is just a directory on your computer's hard disk.


2 Answers

The version that is closer to the root of your dependency tree will be preferred. If both conflicting versions have the same depth in the tree, then the first one (starting from the top of the tree) wins.

Is that a completely stupid rule? Yes it is. The only advantage it has is that you can always force a specific version of a dependency by declaring it as a direct dependency of your project.

So in your case, B:2.0 will be used, since it's declared as a direct dependency. If A doesn't work fine with B:2.0, then, well, either use B:1.0 in your code, or choose another library that does the same thing as A but doesn't cause a conflict.

like image 113
JB Nizet Avatar answered Oct 06 '22 10:10

JB Nizet


I maven do not choose the newer version of an artifact when multiple versions are referenced in the dependency tree. Looking at http://maven.apache.org/plugins/maven-dependency-plugin/examples/resolving-conflicts-using-the-dependency-tree.html, it seems it chooses the nearest definition from the root of the tree. That means that the version in the main POM will be preferred to the one in transitive dependencies. So if instead, you had your project depends on B v1.0 and A had a transitive dependency on B v2.0, then your project will have chosen B v1.0.

like image 31
Frederic Lachasse Avatar answered Oct 06 '22 11:10

Frederic Lachasse