Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different versions of the same dependency in Maven

I have a maven project that depends on both Woodstox and XStream. Unfortunately XStream also depends on Woodstox, but a version slightly older than what I need. In the meantime though, the artifact names of the Woodstox libs changed, so maven won't consider them multiple versions of the same artifact. But the package and class names are the same, which means there is a conflict at runtime.

Now, I could obviously hack the old woodstox jar out of the build (a war file in our case) somehow but what is the proper way of solving this type of problem?

like image 440
biziclop Avatar asked Jan 28 '11 10:01

biziclop


People also ask

How do I exclude a specific version of a dependency in Maven?

Multiple transitive dependencies can be excluded by using the <exclusion> tag for each of the dependency you want to exclude and placing all these exclusion tags inside the <exclusions> tag in pom. xml. You will need to mention the group id and artifact id of the dependency you wish to exclude in the exclusion tag.

What is Maven dependency conflict?

Maven's dependencyManagement section is a mechanism for centralizing dependency information. One of its most useful features is to control versions of artifacts used as transitive dependencies.

How Maven handles and determines what version of dependency will be used when multiple version of an artifact are encountered?

Dependency mediation - this determines what version of an artifact will be chosen when multiple versions are encountered as dependencies. Maven picks the "nearest definition". That is, it uses the version of the closest dependency to your project in the tree of dependencies.


2 Answers

You could try excluding woodstox dependency in your dependency declaration for xstream.

  <dependency>
        <groupId>xstream.group</groupId>
        <artifactId>xstream</artifactId>
        <version>a.b.c</version>
        <exclusions>
            <exclusion>
                <groupId>woodstox.group</groupId>
                <artifactId>woodstox</artifactId>
            </exclusion>
        </exclusions>
  </dependency>
like image 159
Raghuram Avatar answered Sep 28 '22 21:09

Raghuram


If you are lucky, the solution suggested by Raghuram will work.

If not, you will have to create a modified version of the XStream jar, probably using the Maven Shade Plugin, merging both XStream woodstox into one Jar, renaming all woodstox packages.

like image 40
Sean Patrick Floyd Avatar answered Sep 28 '22 22:09

Sean Patrick Floyd