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?
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With