tags:

views:

19

answers:

2

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?

A: 

You can have the build fail on dependency analysis warnings using the dependency plugin.

See

http://maven.apache.org/plugins/maven-dependency-plugin/examples/failing-the-build-on-dependency-analysis-warnings.html

shipmaster
AFAIK, `dependencies:analyze-only` doesn't detect duplicates.
Pascal Thivent
+1  A: 

The Maven Dependency Plugin will include a new dependency:analyze-duplicate in its version 2.2 and this is IMHO the closest thing you can get without specific development (it won't fail the build but will list duplicate dependencies).

This goal has been added for MDEP-206 (Add new Mojo to find duplicate dependency declared), which is fixed, but the version 2.2 has yet to be released so if you want to use this feature, you'll have to build a SNAPSHOT from the source tree:

https://svn.apache.org/repos/asf/maven/plugins/trunk/maven-dependency-plugin/

If you really want to fail the build in case of duplicate, either write your own mojo (extend the above one) or submit a RFE of the Maven Dependency Plugin.

As a side note, Maven 3 does Throw [a] Validation Error if pom contains a dependency with two different versions out of the box. To be totally accurate, this will be reported as warning in Maven 3.0 to not break existing Maven 2.x builds (see MNG-4005) and will be increased to error in 3.1 (see MNG-4622).

Pascal Thivent
@Pascal Thanks for the comprehensive answer. Hopefully I'll have time to get this integrated in the next few days. I'm slightly surprised this feature isn't built in; is it a lack of demand, or some technical reason?
Alison