Maven Dependency Conflicts

September 24th, 2009 by nils

Have you ever seen an exception like this:

org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'XY' defined in class path resource
[applicationContext.xml]: Instantiation of bean failed; nested
exception is org.springframework.beans.
BeanInstantiationException: Could not instantiate bean class
[XY]: Constructor threw exception; nested exception is
java.lang.LinkageError: You are trying to run JAXB 2.0 runtime
(from jar:file:/.../WEB-INF/lib/jaxb-impl-2.1.8.jar!/com/sun/
xml/bind/v2/model/impl/ModelBuilder.class)but you have old
JAXB 1.0 runtime earlier in the classpath (at jar:file:
WhiteSpaceProcessor.class) Please remove the JAXB 1.0
runtime for 2.0 runtime to work correctly.

Well, I have, several times… and the task of having to figure out which library causes this dependency conflict seemed unresolvable pretty scary at first! Luckily, there is the m2eclipse plug-in with its excellent dependency graph. So if you are using Eclipse, whether you are actually using m2eclipse to manage your project or not, just the dependency graph makes it worth having a look at it. I still run my maven tasks on the command line, but have m2eclipse installed, just to be able to use the graph.

Dependency Graph

After identifying which library is causing the dependency conflict, all you have to do is to add an “exclude” node to that dependency in your pom.xml. In the above case, this snipped did the trick:

    <exclusions>         <exclusion>             <artifactId>jaxb-impl</artifactId>             <groupId>javax.xml</groupId>         </exclusion>         ...     </exclusions>

Different Maven Versions on OS X

April 24th, 2009 by nils

I keep running into the problem that different projects I’m working on require different versions of maven. I have two versions installed, maven 2.0.6 in /usr/share/maven-2.0.6 and maven 2.0.9 in /usr/share/maven. To find out which version of maven you are running, open a terminal and type the following

$ mvn --version
Maven version: 2.0.9
Java version: 1.6.0_07
OS name: "mac os x" version: "10.5.6" arch: "x86_64" Family: "mac"

In this case, maven 2.0.9 is running, so we’ll see how to switch to maven 2.0.6. First we need to know where the maven executable, or in this case, the symbolic link to the executable is located:

$ which mvn

Next, we’ll use ls to check where the symbolic link is pointing:

$ ls -l /usr/bin/mvn
lrwxr-xr-x  1 root  wheel  24 Apr 24 14:26 /usr/bin/mvn -> /usr/share/maven/bin/mvn

Finally, if you have different maven versions installed, you can switch between them by overwriting the symbolic link:

$ sudo ln -fhsv /usr/share/maven-2.0.6/bin/mvn /usr/bin/mvn

After providing your password, the symbolic link should now be pointing to the maven 2.0.6 executable. To doublecheck, type

$ mvn --version

The output should be something like this

Maven version: 2.0.6