Issue
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
Solution
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>
Answered By - Karol Dowbecki
Answer Checked By - David Goodson (JavaFixing Volunteer)