Wednesday, March 15, 2017

Disable Maven dependency mediation

Leave a Comment

As I don't trust Maven to pick the "best" version for me in dependency mediation, I would like to fail the build if there is a version conflict that is not explicitly fixed in the pom (usually by using dependencyManagement).

The dependencyConvergence rule of the enforcer plugin seemed to be what I was looking for, but unfortunately, it cannot handle *-exludes (https://issues.apache.org/jira/browse/MENFORCER-195), so I cannot really use it.

Is there any other way to stop Maven from applying the "nearest dependency wins rule" but make the owner of the pom decide which version to use?

3 Answers

Answers 1

You can use Maven Enforcer Plugin and fail the build if dependencyConvergence is violated. Give it a shot and see if it does what you want.

Update: Sorry, I just noticed that you tried and are having a problem with excludes. Have you tried to build the plugin with the pull request from the linked ticket and see if it fixes the problem for you?

Answers 2

You can define explicitly which version Maven should use by using dependency version ranges.

Basically you just need to surround your version with round brackets (exclusive quantifier) or square brackets (inclusive quantifier).

For example:

<version>[4.2]</version> 

would mean: use exactly version 4.2 and nothing else.

Or:

<version>[4.2,)</version> 

would mean: use version 4.2 or higher.

Or:

<version>(,4.2]</version> 

would mean: use a version below or exactly 4.2.

Answers 3

There is a way to minimize the damage but not to eliminate completely unless you exclude all the dependencies manually by specifying the groupid and artifactid of the excluded jars.

You can use Maven optional dependencies and declare all the dependencies in the Parent pom as optional and provide the version that you want to include in the Parent pom itself. Now if someone wants to use a dependency they will have to specifically declare that dependency in their pom, Since the parent pom will have the version the child modules do not have to maintain their own versions.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment