Detect Maven Dependency Mediation

As of Maven 2.0.9 a new feature was added to Maven called dependency mediation.
Dependency mediation is the technique used by Maven to resolve your project dependencies in the specific case when a dependency occurs multiple times in your dependency tree.
Usually this occurs on transitive dependencies linked through the dependencies of your project.
In these cases mediation will be performed using the nearest win-strategy.
In short this strategy means that Maven will use the version declared in the pom.xml that is closest to your project pom.xml.
Hence, no in-depth intelligence is used to resolve the dependency conflict.
Actually, I can’t really think of a conflict resolve strategy that would really solve this problem.

Any strategy I can think of has the hazard of linking incompatible dependencies in to your project.
Using Maven version ranges can ofcourse resolve compatibility between artifacts, but this also requires you to establish a compatibility matrix of your dependencies. A pretty tedious task if you ask me.

Now this whole mediation feature might sound like a very undesirable feature, but it’s not!
With this feature you can now at least be made aware of any dependency conflicts in your project dependencies.
When you build your project using the -X switch, Maven will output all mediations (and a lot more) that have been performed.

Now, wouldn’t it be cool if there was a maven-plugin to detect mediation?
JDriven took the liberty of extending Apache’s dependency plugin with such a feature and share it with you.

The goal used to detect mediation is mvn dependency:mediation.
Additionally two interesting parameters can be added:
1) -DdisallowMediation=false
2) -DinspectArtifactId={some artifactId}
As the name already implies, disallowMediation determines whether or not mediation is allowed.
When allowed, the plugin will only warn about mediation being performed on dependencies. Very usefull when combined with for instance Jenkins’ ${IS_M2RELEASEBUILD} variable to disallow mediation for release builds, yet allowing it for snapshot builds.
The inspectArtifactId parameter is much like goal dependency:tree -Dverbose=true, it will check mediation and print dependency information regarding the artifact having id {some artifactId}.

In your pom.xml your can add additional configuration to filter which dependencies must be checked on mediation:

An inclusion or exclusion filter can be defined to either put dependencies in scope or out scope for mediation checking. Be ware the example configuration filter is also used for other goals supported by the dependency plugin.

The goal dependency:mediation can easily be added to for instance your Jenkins build configuration to prevent you from releasing a project with unchecked version mediations.

Below the patch needed to add the mediation feature to dependency plugin version 2.8.
More information about how maven mediation works is covered in a nice detailed puzzle quiz held at Devoxx in 2012.

Have fun!


2 thoughts on “Detect Maven Dependency Mediation

    • Hi,

      I did not know the enforcer plugin.
      Looks very interesting.
      Goal dependencyConvergence seems to do a simular job without options to exclude artifacts when desired.
      The puzzle is also nice, I will add it to the blog for those who wish to learn more about maven dependency mediation.


Leave a Reply

Your email address will not be published. Required fields are marked *