Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get Maven to fail when conflicting versions of the same artifact are referenced?

Tags:

I'd like my Maven build to fail if the same artifact is referenced with different versions in my dependency tree. This would seem like a fairly trivial option, but I can't work out how to do it. Any clues?

like image 832
Armand Avatar asked Jul 29 '10 17:07

Armand


People also ask

How do you resolve a version collision of artifacts in Maven?

One way to resolve a version collision is by removing a conflicting transitive dependency from specific artifacts. In our example, we don't want to have the com. google. guava library transitively added from the project-a artifact.

How Maven handles and determines what version of dependency will be used when multiple version of an artifact are encountered?

Dependency mediation Determines what version of a dependency is to be used when multiple versions of an artifact are encountered. If two dependency versions are at the same depth in the dependency tree, the first declared dependency will be used.

How do I exclude a specific version of 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.

What will happen if the Maven version number of POM xml file does not match with the machine used to install?

Maven won't allow any other either. Build will fail if version is not found.


1 Answers

The maven-enforcer-plugin has a dependencyConvergence rule which does what you want. Here's an example from the documentation.

This will cause a build to fail:

  <dependencies>     <dependency>       <groupId>org.slf4j</groupId>       <artifactId>slf4j-jdk14</artifactId>       <version>1.6.1</version>     </dependency>     <dependency>       <groupId>org.slf4j</groupId>       <artifactId>slf4j-nop</artifactId>       <version>1.6.0</version>     </dependency>   </dependencies>   

With this being logged during compilation:

[ERROR] Dependency convergence error for org.slf4j:slf4j-api:1.6.1 paths to dependency are: +-org.myorg:my-project:1.0.0-SNAPSHOT   +-org.slf4j:slf4j-jdk14:1.6.1     +-org.slf4j:slf4j-api:1.6.1 and +-org.myorg:my-project:1.0.0-SNAPSHOT   +-org.slf4j:slf4j-nop:1.6.0     +-org.slf4j:slf4j-api:1.6.0 
like image 153
Craig P. Motlin Avatar answered Sep 17 '22 22:09

Craig P. Motlin