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.
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.
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.
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.
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.
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 :)
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>
=)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With