0

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?

seenukarthi
  • 8,241
  • 10
  • 47
  • 68
user124
  • 423
  • 2
  • 7
  • 26

1 Answers1

3

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

seenukarthi
  • 8,241
  • 10
  • 47
  • 68
  • as i see, is parent tag for , but i cannot use it because it may some unknown effect.Can you tell me the way i asked in question? Do i need exclusion tag or not? – user124 Mar 31 '22 at 06:35
  • thanks for updating. But do i need dependency management. There is Maven’s nearest definition logic, by which if i just add the 1.4.11(updated version) without < dependencyManagement >, it should take it? – user124 Mar 31 '22 at 07:17
  • @user124 yes you can add 1.4.11 version of xstream to your pom.xml and it will work. you dont need to add exclution – seenukarthi Mar 31 '22 at 07:26
  • But then, do i need to exclude xstream 1.4.11 artifact from spring-batch-core? – user124 Mar 31 '22 at 07:29
  • @user124 no need to, also in exclusion version won't be mentioned only groupId and artifactId will be mentioned. You can verify which version is used by running the command `mvn dependency:list` – seenukarthi Mar 31 '22 at 07:43
  • i specifically asked for exclusion because someone told me that if we add in exclusion, then it wont download that particular dependency and it saves n/w bandwidth and m2 size.But i cannot find anyhwere that putting in exclusion means it wont download, though it wont use is mentioned – user124 Mar 31 '22 at 10:12