MRX
MRX

Reputation: 1651

maven dependency version issues

I just got in issue related to Maven and Dependent JAR version. I create following project to analyze issue.

  1. I created App_1.jar which is using spring version 4.2.9.
  2. I created App_2.jar which is using spring version 4.3.4.
  3. I created App_3.jar which is using spring version 4.3.6.
  4. I created App_Main.war which will App_1.jar, App_2.jar, App_3.jar.

According to maven if you use different version it will use the latest one but in my case it using the spring version of jar which i included first which is App_1.jar and its version is 4.2.9.

Here is the code.

**App_1 POM.xml**

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.ksh</groupId>
      <artifactId>App_1</artifactId>
      <packaging>jar</packaging>
      <version>1.0</version>
      <name>App_1</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.9.RELEASE</version>
        </dependency>
      </dependencies>
    </project>

**App_2 POM.xml**

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ksh</groupId>
  <artifactId>App_2</artifactId>
  <packaging>jar</packaging>
  <version>1.0</version>
  <name>App_2</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.4.RELEASE</version>
    </dependency>
  </dependencies>
</project>

**App_3 POM.xml**

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ksh</groupId>
  <artifactId>App_3</artifactId>
  <packaging>jar</packaging>
  <version>1.0</version>
  <name>App_3</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.6.RELEASE</version>
    </dependency>
  </dependencies>
</project>

App_Main POM.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>App_Main</artifactId>
  <packaging>war</packaging>
  <version>1.0</version>
  <name>App_Main Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>com.ksh</groupId>
      <artifactId>App_1</artifactId>
      <version>1.0</version>
    </dependency>
    <dependency>
      <groupId>com.ksh</groupId>
      <artifactId>App_2</artifactId>
      <version>1.0</version>
    </dependency>
    <dependency>
      <groupId>com.ksh</groupId>
      <artifactId>App_3</artifactId>
      <version>1.0</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>App_Main</finalName>
  </build>
</project>

Upvotes: 0

Views: 1082

Answers (1)

alfcope
alfcope

Reputation: 2427

Where did you read maven use the latest one?

For transitives dependencies Maven uses a "nearest-wins" strategy to resolves version conflicts, and that means it will use the version of the closest dependency to your project in the tree of dependencies.

Maven transitive dependencies

One possible solution is use the <dependencyManagement> to resolve your conflicts.

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>spring_context_version_you_want_to_use</version>
        </dependency>
    </dependencies>
</dependencyManagement>

Maven dependency managament

You can check your tree of dependencies with the command:

mvn dependency:tree -Dverbose -Dincludes=your-jar

Resolving conflicts using the dependency tree

Upvotes: 1

Related Questions