whaley

Drawing a Blank

Dependency Convergence in Maven

I was running in to a problem with a Java project that occured only in IntelliJ Idea, but not on the command line, when running specific test classes in Maven. The exception stack trace had the following in it:

1
Caused by: com.sun.jersey.api.container.ContainerException: No WebApplication provider is present

That seems like an easy problem to fix - it is the exception message that is given when jersey can’t find the provider for JAX-RS. Fixing it is normally just a matter of making sure jersey-core is on the classpath to fulfill SPI requirements for JAX-RS. For some reason though this isn’t happening in IntelliJ Idea. I inspected the log output of the test run and it is quite clear that all of the jersey dependencies are on the classpath. Then it dawns me on the try running mvn dependency:tree from inside of Idea. Here is what I found:

`mvn dependency:tree` output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[INFO] +- org.mule.modules:mule-module-jersey:jar:3.2.1:provided
[INFO] |  +- com.sun.jersey:jersey-server:jar:1.6:provided
[INFO] |  +- com.sun.jersey:jersey-json:jar:1.6:provided
[INFO] |  |  +- com.sun.xml.bind:jaxb-impl:jar:2.2.3-1:provided
[INFO] |  |  \- org.codehaus.jackson:jackson-xc:jar:1.7.1:provided
[INFO] |  +- com.sun.jersey:jersey-client:jar:1.6:provided
[INFO] |  \- org.codehaus.jackson:jackson-jaxrs:jar:1.8.0:provided
...
[INFO] +- org.jclouds.driver:jclouds-sshj:jar:1.4.0-rc.3:compile
[INFO] |  +- org.jclouds:jclouds-compute:jar:1.4.0-rc.3:compile
[INFO] |  |  \- org.jclouds:jclouds-scriptbuilder:jar:1.4.0-rc.3:compile
[INFO] |  +- org.jclouds:jclouds-core:jar:1.4.0-rc.3:compile
[INFO] |  |  +- net.oauth.core:oauth:jar:20100527:compile
[INFO] |  |  +- com.sun.jersey:jersey-core:jar:1.11:compile
[INFO] |  |  +- com.google.inject.extensions:guice-assistedinject:jar:3.0:compile

Notice how I have jersey-core 1.11 coming from jclouds-core but jersey 1.6 everywhere else. That, my friends, is a dependency convergence problem. Maven with its default set of plugins (read: no maven-enforcer-plugin) does not even warn you if something like this happens. In this case, somehow jclouds-core depends directly on jersey-core and happens to resolve the dependency to the version that jclouds-core declared first before jersey-core can be resolved as a transitive dependency on mule-module-jersey.

To fix the symptom, all I had to do was add the jersey-core dependency explicitely as a top level dependency in my pom:

pom.xml
1
2
3
4
5
6
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-core</artifactId>
    <version>${jersey.version}</version>
    <scope>provided</scope>
</dependency>

But doing so only fixes the symptom, not the problem. The real problem is that the maven project I’m working on does not presently attempt to detect or resolve dependency convergence problems. This is where the comes in handy. You can have the enforcer plugin run the DependencyConvergence rule agaisnt your build and have it fail when you have potential conflicts in your transitive dependencies that you haven’t resolved through exclusions or declaring direct dependencies yet. Binding the maven-enforcer-plugin to your build would look something like this:

pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-enforcer-plugin</artifactId>
            <version>1.0.1</version>
            <executions>
                <execution>
                    <id>enforce</id>
                    <configuration>
                        <rules>
                            <DependencyConvergence/>
                        </rules>
                    </configuration>
                    <goals>
                        <goal>enforce</goal>
                    </goals>
                    <phase>validate</phase>
                </execution>
            </executions>
        </plugin>
    </plugins>
...
</build>

I chose to bind to the validate phase since that is the first phase to be run in the maven lifecycle. Now my build fails immediately and contains very useful output that looks like the following:

1
2
3
4
5
6
7
8
9
Dependency convergence error for org.codehaus.jackson:jackson-jaxrs:1.7.1 paths to dependency are:
+-com.nodeable:server:1.0-SNAPSHOT
  +-org.mule.modules:mule-module-jersey:3.2.1
    +-com.sun.jersey:jersey-json:1.6
      +-org.codehaus.jackson:jackson-jaxrs:1.7.1
and
+-com.nodeable:server:1.0-SNAPSHOT
  +-org.mule.modules:mule-module-jersey:3.2.1
    +-org.codehaus.jackson:jackson-jaxrs:1.8.0

There are many rules you can apply besides DependencyConvergence. However, if the output from the DependencyConvergence rule looks anything like mine does presently, it might take you a while before you get around to getting your maven build to pass and conform to other rules.

Comments