maven dependency pulling a wrong dependency

15,637

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.

Share:
15,637
DarthVader
Author by

DarthVader

Updated on July 20, 2022

Comments

  • DarthVader
    DarthVader almost 2 years

    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>
    
  • DarthVader
    DarthVader over 11 years
    i see there is a conflict. how can i override the version to get the latest one or can i have a side by side ?
  • Tobb
    Tobb over 11 years
    In my experience, having them side by side does not work that good, since it often ends up trying to call the wrong one. But which dependency depends on the version you don't want?
  • DarthVader
    DarthVader over 11 years
    i want httpcore.4.2 not httpcore.4.1.*. i added dependencyManagement tag.would that work? please see my edit.
  • DarthVader
    DarthVader over 11 years
    i look at it within eclipse. and i see the conflict.
  • Tobb
    Tobb over 11 years
    Good for you. I can't see it, so hard to offer any real help.
  • DarthVader
    DarthVader over 11 years
    another project that i have is depending on httpcore.4.1.4
  • Tobb
    Tobb over 11 years
    If its an eclipse problem only (it is not part of the maven project in question), then you should just remove it from the eclipse java build path (Project -> properties)
  • Tobb
    Tobb over 11 years
  • liam xu
    liam xu about 5 years
    Even Any dependency version added explicitly in your pom will override the version of any transitive dependency of the same groupId/artifactId.,Does it mean there is no need to explicitly exclude the transitive jar?
  • Tobb
    Tobb about 5 years
    If you have defined a dependency with a specific version in your pom, then there is no need to exclude this dependency from being pulled transitively. One small exception is dependencies that contain the same classes (same package), but have a different groupId/artifactId.