javamavendependenciesapache-httpcomponents

maven dependency pulling a wrong dependency


The dependency bellow pulls another dependency: httpcore.4.1.4.

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

Doing so will throws ClassDefNotFound, during deployment.
Using httpcore.4.2 though, everything works.

I even added both dependencies directly into my pom:

<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>

This doesn't change anything and I'm still facing the same issue: maven downgrades httpcore.4.2 to httpcore.4.1.2.

The dependencyManagement looks like this:

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

Solution

  • 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.