如何覆盖maven中artifact包含的库的默认版本?

发布于 2025-01-17 22:19:43 字数 714 浏览 3 评论 0原文

我在pom.xml中有一个弹簧批量的依赖性,如下所示:

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

有一个Artifact Xstream,上述版本为1.4.7,需要更新为1.4.11。

可以添加如下:

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

正确的方法是什么?我正在考虑以下方法:

上面的代码将在那里,但我需要使用&lt;排除&gt;要专门从弹簧批次核心中排除Xstream trifact旧版本,还是Maven会自动照顾此版本?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你没皮卡萌 2025-01-24 22:19:43

更好的方法是使用 标签。依赖管理将确保即使其他一些传递依赖带来更高版本的依赖,版本也会得到维护。

用法:

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

注意: dependencyManagement 标签用于定义依赖项的版本和范围(如果不在编译的默认范围内),它不会将其中的依赖项添加到您的项目中,您必须定义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/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>

在这种情况下,spring-batch-core被添加为直接依赖项,如果它有xstream作为依赖项,您的项目将使用1.4.11版本甚至 spring-batch-core 也有不同版本的 xstream 作为依赖项。

参考: 依赖管理

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文