Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maven dependency pulling a wrong dependency

I have a dependency as follows:

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.2</version>
        <scope>compile</scope>
    </dependency>

This is pulling down another dependency httpcore.4.1.4 which throws a ClassDefNotFound, when i deploy httpcore.4.2 everything works.

I added both of the dependencies as follows:

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.2</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.2</version>
        <scope>compile</scope>
    </dependency>

and still facing the same issue ie: mvn brings down httpcore.4.1.2 not httpcore.4.2

how can i resolve this?

EDIT:

added;

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.2</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    </dependencyManagement>
like image 819
DarthVader Avatar asked Nov 29 '12 21:11

DarthVader


People also ask

How do I fix Maven dependency issues?

If the dependencies weren't imported correctly (IntelliJ IDEA highlights them), try to perform the following actions: You can check your local maven repository in the Maven | Repositories settings and try to update it. You can check the jar file of the local . m2 repository to see if it was downloaded correctly.

What is Maven dependency conflict?

Each dependency that we include in our project might link to other artifacts. Maven can automatically bring in these artifacts, also called transitive dependencies. Version collision happens when multiple dependencies link to the same artifact, but use different versions.

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.

Does Maven override dependency version?

By taking advantage of Maven's nearest definition logic, developers can override the version of a dependency by declaring it on the root pom. xml file.


1 Answers

You might have a transitive dependency, one your other dependencies depend on the version you don't want.

To get an overview of all dependencies, direct and transitive, try:

mvn dependency:tree

If you find a crash between different versions of the same dependency, the first thing you should do is figure out whether the crash is critical (do you need both?) If not, upgrade so that the lowest dependency version will become equal to the highest. If it is a transitive dependency consider upgrading the version of this.

If you just want to lock on to a specific version of the dependency, you have some choices:

Exclude the transitive dependency:

<dependency>
  <groupId>com.something</groupId>
  <artifactId>something</artifactId>
  <exclusions>
    <exclusion>
      <groupId>com.somethingElse</groupId>
      <artifactId>somethingElse</artifactId>
    </exclusion>
  </exclusions>
</dependency>

Include a specific version:

<dependency>
  <groupId>com.somethingElse</groupId>
  <artifactId>somethingElse</artifactId>
  <version>2.0</version>
</dependency>

Any dependency version added explicitly in your pom will override the version of any transitive dependency of the same groupId/artifactId.

Although being a bit of a puzzle, you should try to get compatible versions of your dependencies, that being version with the same version transitive dependencies.

like image 114
Tobb Avatar answered Oct 15 '22 09:10

Tobb