3

I am experiencing a problem with Maven where the library com.google.guava is being referenced about half a dozen times across multiple layers of transitive dependencies in my project A. Almost all of those dependencies require the age-old version 18.0.

I have written another dependency B that is being used in project A as well. This dependency directly depends on version 28.0 of com.google.guava, but Maven still chooses 18.0 over the much newer version.

Unless I explicitly include the newer version in all projects depending on B, the library will be nonfunctional due to NoMethodErrors and such. If I do so I will probably experience problems with all other libraries because of the new, binary incompatible version of the dependency.

Effectively, I am experiencing group pressure to use the old version of the dependency because everyone else does it.

Why would Maven not choose the newest version instead? What can I do to convince Maven to use the newer version in all projects depending on B?

Is there a way to separate concerns and have all other dependencies actually use 18.0 while my dependency uses 28.0?

If just a slightly larger package size is the prize to pay for more modern libraries across the projects...

Maybe there is a possibility to hard-link a certain dependency version for my library B, so only my library uses the newer version?

Markus Appel
  • 3,138
  • 1
  • 17
  • 46
  • 1
    Is your problem just at compile time or also at runtime? You will have problems having 2 different versions of the library in runtime. If the library supports retroactivity, you should use only the newer one. If not, you might be out of luck. – Guilherme Mussi Jul 02 '19 at 15:43
  • Depending on your project structure you might want to look into packaging **B** as a [fat jar](https://stackoverflow.com/q/16222748/3419894) to see if it would suit your needs. – JonK Jul 02 '19 at 15:49
  • Is v28.0 backwards compatible to v18.0? – Puce Jul 02 '19 at 15:57

1 Answers1

0

In maven you can use <dependencyManagement>

With this tag you can explicitly tell maven which versions of dependencies you want your project to use.

For guava you can do something like:

<dependencyManagement>
    <dependencies>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>28.0</version>
        </dependency>

    ...
    </dependencies>
</dependencyManagement>

Then you can do someghing like:

    <dependencies>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
        </dependency>

    ...
    </dependencies>

This will force maven to use the guava 18 dependency

Martín Zaragoza
  • 1,717
  • 8
  • 19