Can new versions of dependencies override old versions in maven projects?

If I have a maven project that has explicit dependencies on A and B version 2.0 and A , has a transitive dependency on B version 1.0 . Does newer version B mean older version? I used maven depencdy: the goal of the solution, and it looks like an older version of B is not allowed. What if A is incompatible with the newer version of B ? Or, if A depends on B version 2.0 , and my project has an explicit dependency on B version 1.0 after running the dependency: resolve the target I do not see a newer version of B. So how will these dependencies be resolved then?

And when I use the goal of the solution, it shows the dependencies. But in what phase will these dependencies be used? Compile, test, runtime?

+6
source share
2 answers

A 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, the first wins (starting at the top of the tree).

Is this an absolutely stupid rule? Yes. Its only advantage is that you can always force a certain version of the dependency to be declared by declaring it as a direct dependency of your project.

So, in your case, B: 2.0 is used, since it is declared as a direct dependency. If A does not work fine with B: 2.0, then, well, either use B: 1.0 in your code, or select another library that does the same as A, but does not cause a conflict.

+9
source

I maven does not select a newer version of the 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 like it selects the closest definition from the root of the tree. This means that a version in the main POM will be preferable to a version in transitive dependencies. So, if instead you had your project dependent on B v1.0, and A had a transitive dependency on B v2.0, then your project would choose B v1.0.

+1
source

Source: https://habr.com/ru/post/957287/


All Articles