Paflow
Paflow

Reputation: 2377

Two versions of library dependency in Maven

In my pom.xml I have

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20170516</version>
</dependency>

And my Program needs and uses this version om JSON.

import org.json.JSONObject;

When I put in

final JsonObject jsonObject = new JsonObject();
System.out.println( jsonObject.getClass().getPackage().getImplementationVersion());

I get

20170516

Okay, alright. (Note: This is a class of the program, not the test!)

Now I run my Unittest (Mockito, JUnit) with mvn test. I get an error, which is related to the JSONObject version. And the log says:

0.0.20131108.vaadin1

I found out, that this version comes from this dependency

<dependency>
    <groupId>org.skyscreamer</groupId>
    <artifactId>jsonassert</artifactId>
    <version>1.5.0</version>
    <scope>test</scope>
</dependency>

If I delete it, my test works fine.

But now other tests fail, which uses this dependency

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

and in pom.xml

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.3.4.RELEASE</version>
</dependency>

How can I configure maven, that the program uses JSON version 20170516, but spring-test can still use jsonassert?

Even if almost the same name, I don't think this is a duplicate of

-- Edit 1

mvn dependency:tree | grep json 
[INFO] +- org.skyscreamer:jsonassert:jar:1.5.0:test 
[INFO] |  \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test 
[INFO] +- com.jayway.jsonpath:json-path-assert:jar:2.2.0:test 
[INFO] |  +- com.jayway.jsonpath:json-path:jar:2.2.0:test 
[INFO] |  |  \- net.minidev:json-smart:jar:2.2.1:test 
[INFO] +- org.json:json:jar:20170516:compile

Upvotes: 1

Views: 1280

Answers (2)

Karol Dowbecki
Karol Dowbecki

Reputation: 44980

Unless spring-test or jsonassert will shade the org.json:json dependency internally in the future version you are stuck having to use one version of org.json:json across the classpath.

Not all Java dependencies are compatible, see classpath hell.

You can try defining a Dependency Exclusion for the problematic version but this can potentially brake jsonassert dependency:

<dependency>
  <groupId>org.skyscreamer</groupId>
  <artifactId>jsonassert</artifactId>
  <version>1.5.0</version>
  <scope>test</scope>
  <exclusions>
    <exclusion>
      <groupId>com.vaadin.external.google</groupId>
      <artifactId>android-json</artifactId>
    </exclusion>
  </exclusions>
</dependency>

Upvotes: 2

user51
user51

Reputation: 10243

you need to add the dependencies that you want to enforce specific version when there is conflict in dependencyManagement. This ensures the maven uses 20170516 version of json dependency even though jsonassert depends different version.

    <dependencyManagement>
    <dependencies>
        <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20170516</version>
        </dependency>
        <dependency>
            <groupId>org.skyscreamer</groupId>
            <artifactId>jsonassert</artifactId>
            <version>1.5.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    </dependencyManagement>

Please see Differences between dependencyManagement and dependencies in Maven

or you can use <exclusions> to exclude a child dependency.

Upvotes: 5

Related Questions