4

Consider a very common situation when a project depends on 2 libraries, each transitively bringing a 3rd library of different versions:

Project A:
    Library B:
        Library D: version 1
    Library C:
        Library D: version 2

I can use maven enforcer plugin's dependencyConvergence rule to detect such problems and then directly specify the version of Library D in my Project A's pom in e.g. <dependencyManagement> section.

But this creates another problem: now I have to manually track the version of library D even though my project does not directly use it.
Every time I change the version of either library B or library C, I have to remember to also update the version of library D in my project. It's very easy to forget!

Is there a way to tell maven to just use the newest version of a transitive library?
Something like:

<dependencyManagement>
    ...
    <dependency>
        <groupId>path.to</groupId>
        <artifactId>library-D</artifactId>
        <version>
            <use_newest_one_from_all_transitive_dependencies_please/>
        </version>
    </dependency>

Is there a way to achieve this?

Alexander
  • 2,761
  • 1
  • 28
  • 33
  • 1
    Simple answer to this: No...The algorithm is that way to use the version which is nearest to your project....Usually the maintainer of Library B+C should use the same version (which will not work always) or you have to overrule via dependencyManagement the version of D which should be used. The final question is: Does B work with version 2 or does C wirk with version 1? Both could fail...only checkable via Tests...or if even worse your build will not compile/tests will fail... – khmarbaise May 04 '20 at 10:46
  • If `B` and `C` can function can function(At least the method useful to you) with an older version of `D`, you can `exclude` the transitive dependency `D` from both `B` and `C`, then add the older(yet constant) `D` as a direct dependency in `A`. Use the `exclusion` tag to exclude the transitive from getting into the compile time. – Nipun Thathsara May 12 '20 at 16:32
  • @NipunThathsara If you want to choose a version, just use ``. No exclusions necessary. – J Fabian Meier May 13 '20 at 08:55

2 Answers2

4

No, as khmarbaise already said, this is not possible.

The resolution rule sits deeply in Maven itself. You can check the result of the resolution (as you already mentioned), but you cannot change it.

We decided to fix nearly all the versions of transitive dependencies by using appropriate BOMs (lists of dependencyManagement) that we import in the <dependencyManagement> section. This guarantees that all dependencies come in recent versions. It does not guarantee that everything fits together. But using the newest version as standard would not do that either.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
2

You could just add the dependency and then manage it automatically, as suggested here. This uses the Maven Versions Plugin. Granted, it would take the latest version, not the latest transitive dependency version. Version ranges are also a possibility, but using them is discouraged.

davidfmatheson
  • 3,539
  • 19
  • 27