DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. Dependency Convergence in Maven

Dependency Convergence in Maven

Jason Whaley user avatar by
Jason Whaley
·
Jan. 11, 13 · Interview
Like (0)
Save
Tweet
Share
3.57K Views

Join the DZone community and get the full member experience.

Join For Free

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:

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:treefrom inside of Idea. Here is what I found:

[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:

<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 maven-enforcer-plugin 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:

<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:

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.


Dependency Apache Maven

Published at DZone with permission of Jason Whaley, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • ChatGPT Prompts for Agile Practitioners
  • Kotlin Is More Fun Than Java And This Is a Big Deal
  • The Future of Cloud Engineering Evolves
  • The Quest for REST

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: