Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven - Multiple artifact versions in dependency management

Is there a valid use case for having two different versions of the same artifact in the dependency management section of your POM?

I'm looking at a project whose root POM contains such an example. The project itself has multiple modules using different versions of this "duplicate dependency". Some of the modules thus have to specify the artifact's version explicitly among their dependencies so as to distinguish between the two.

If the POM files of the modules have to specify the version anyway, what is the reason for having the duplication in the parent's dependency management? The dependency would get looked up properly even if it was removed from dependency management, so why would you duplicate it there in the first place?

I'm just trying to figure out if this is good practice or if there's a better solution to such a situation.

like image 694
Miro Avatar asked Aug 30 '13 14:08

Miro


People also ask

Which dependency lets Maven distinguish between multiple artifacts that are generated from the same project?

Maven can automatically bring in these artifacts, also called transitive dependencies. Version collision happens when multiple dependencies link to the same artifact, but use different versions. As a result, there may be errors in our applications both in the compilation phase and also at runtime.

How do I exclude a specific version of a dependency in Maven?

Multiple transitive dependencies can be excluded by using the <exclusion> tag for each of the dependency you want to exclude and placing all these exclusion tags inside the <exclusions> tag in pom. xml. You will need to mention the group id and artifact id of the dependency you wish to exclude in the exclusion tag.

Does Maven override dependency version?

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.

What is the use of dependencyManagement in Maven?

Dependency management in Maven allows teams to manage dependencies for multi-module projects and applications. These can consist of hundreds or even thousands of modules. Using Maven can help teams define, create, and maintain reproducible builds.


2 Answers

Actually, if you are using Maven3+, you'll get a warning like this:

[WARNING]
[WARNING] Some problems were encountered while building the effective model for groupId:artifactId:jar:1.0-SNAPSHOT
[WARNING] 'dependencyManagement.dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: junit:junit:jar -> version 3.8.1 vs 3.0 @ line 15, column 18
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]

Maven will pick up the first, so the second will never be used. Just remove it to prevent more confusion :)

like image 75
Robert Scholte Avatar answered Sep 29 '22 13:09

Robert Scholte


Try to cheat maven. Type dots at the end of each group. It works for me.

<dependency>
        <groupId>my.group</groupId>
        <artifactId>myartifact</artifactId>
        <version>1</version>
</dependency>
<dependency>
        <groupId>my.group.</groupId>
        <artifactId>myartifact</artifactId>
        <version>2</version>
</dependency>
<dependency>
        <groupId>my.group..</groupId>
        <artifactId>myartifact</artifactId>
        <version>3</version>
</dependency>

=)

like image 23
Dmitri Avatar answered Sep 29 '22 11:09

Dmitri