Reputation: 410
For your information, I'm new to the Maven environment and not familiar with Java development.
First, I wrote a project which is using org.apache.httpcomponents:httpclient:4.3.6
. It ran well and had no problem at all. And I put the source code (a Java file) into another Eclipse web project which is configured with Maven. So I added a dependency into the pom.xml
and it was compiled successfully. But a problem popped out when I ran it. It says java.lang.NoSuchFieldError
. After lots of tries and fails, I concluded that it was caused by a version problem.
Dependencies in pom.xml
:
<dependencies>
<dependency>
<groupId>net.generalsvc</groupId>
<artifactId>ubi.base.bin</artifactId>
<version>2.1.0-SNAPSHOT</version>
</dependency>
...
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>
</dependencies>
The net.generalsvc:ubi.base.bin
includes dependency of org.apache.http:httpclient:4.2.1
. So result dependency hierarchy is like below.
I think my code is refering the '4.2.1' version not the '4.3.6' one. How can I assign the '4.3.6' version to my Java file? Can I preserve the project's existing depencencies with adding the httpclient:4.3.6
for my code?
Upvotes: 0
Views: 195
Reputation: 609
This will exclude the specific from the maven build
<dependency>
<groupId>net.generalsvc</groupId>
<artifactId>ubi.base.bin</artifactId>
<version>2.1.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.apache.http</groupId>
<artifactId>httpclient</artifactId>
</exclusion>
</exclusions>
</dependency>
Upvotes: 1
Reputation: 798
Maven uses a measure of "distance" to decide which dependency is to be used. That measure is one step per pom.xml (the dependencies in your pom.xml are 1, the dependencies on those dependencies' pom are 2, etc), and the one nearer is included, so if you include in your pom.xml a version, that's what's being used.
The same dependency (same groupId and artifactId) can't be used with two different versions, so you can't preserve your dependencies' dependencies and include another version of them.
Upvotes: 1