Search code examples
maventransitive-dependency

Automatically taking the newest transitive dependency in maven


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?


Solution

  • 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.