Home > Mobile >  How to override default version of library included by artifact in maven?
How to override default version of library included by artifact in maven?

Time:04-02

I have a spring batch dependency in my pom.xml declared as below:

           <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-core</artifactId>
            <version>3.0.9.RELEASE</version>
        </dependency>

There is one artifact xstream that is included by above with version 1.4.7 and it needs to be updated to 1.4.11.

It can be added as follow:

    <groupId>com.thoughtworks.xstream</groupId>
    <artifactId>xstream</artifactId>
    <version>1.4.11</version>
    </dependency>

What is the correct way for this?I am thinking of following approach:

Both above pieces of code will be there but do I need to use < exclusions > to specifically exclude xstream artifact old version from spring-batch-core or does maven takes care of this automatically?

CodePudding user response:

Better way will be using <dependencyManagement/> tag. Dependency management will make sure the version will be maintained even if some other transitive dependency brings higher version of the dependency.

Usage:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.thoughtworks.xstream</groupId>
            <artifactId>xstream</artifactId>
            <version>1.4.11</version>
        </dependency>
   </dependencies>
</dependencyManagement>

Note: dependencyManagement tag is used for defining the version and scope (if not in the default scope which is compile) of a dependency it does not add the dependencies in it to you project, you must define separate <dependencies/> section in your pom.xml for adding dependencies to your project.

In your case it will be like.

<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/xsd/maven-4.0.0.xsd">

...

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.thoughtworks.xstream</groupId>
                <artifactId>xstream</artifactId>
                <version>1.4.11</version>
            </dependency>
       </dependencies>
       ...
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-core</artifactId>
            <version>3.0.9.RELEASE</version>
        </dependency>
        ...
    </dependencies>
...

</project>

In this case spring-batch-core is added as a direct dependency and if it has xstream as dependecny you project will use 1.4.11 version even spring-batch-core has a different version of xstream as dependency.

Ref: Dependency Management

  • Related