是否可以覆盖子 POM 中的依赖项版本? [英] Is possible override the version of a dependency in child POM?

查看:392
本文介绍了是否可以覆盖子 POM 中的依赖项版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Maven 来管理我的项目的依赖项.我有我的父 POM,我正在使用依赖管理来避免在每个项目中编写公共依赖.

I'm working with Maven to management the dependencies of my projects. I have my parent POM and I'm using Dependency Management to avoid write the common dependencies in each project.

现在,我需要在我的子 POM 中更改一个依赖项的版本.

Now, I need change the version of one dependency in my child POM.

我有我父母的 POM

I have this of my parent POM

<dependencyManagement>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.1</version>
    </dependency>
</dependencyManagement>

我的子 POM 引用了父 POM

My child POM has a reference to the parent POM

<parent>
    <groupId>com.myproject</groupId>
    <artifactId>root-parent-pom</artifactId>
    <version>1.1.0.22</version>
    <relativePath>../parent/pom.xml</relativePath>
 </parent>

在这个子 POM 中,我想覆盖我在父 POM 中编写的相同依赖项的版本,就像这样.

And in this child POM I want to override the version of the same dependency that I wrote in the parent POM, something like this.

<dependency>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
    </dependency>
</dependency>

我这样做了,但是当我生成我的项目时,在这种情况下是一个 WAR,我的项目中包含的依赖项的最终版本是父 POM 上描述的版本.

I did that, but when I generate my project, in this case is a WAR, the final version of the dependency that is included in my project is the version that is described on the parent POM.

是否可以覆盖子 POM 中的版本?

Is possible override the version in child POM?

推荐答案

也许吧.如果您指定一个不同的版本,则该版本将覆盖 dependencyManagement 元素中的版本.

Maybe. If you specify a distinct version, then this version will override the one in the dependencyManagement element.

但仅针对此单个 POM.它不会神奇地分发到反应堆构建中的下一个 POM除非下一个 POM 将这个 POM 作为父级.所以如果你有这个设置:

But only for this single POM. It doesn't magically distribute to the next POM in the reactor build unless the next POM has this POM as parent. So if you have this setup:

parent
- mod1
- mod2
- war

然后你把它放到 mod1 中,那么 war 不会注意到,因为 war 使用 parent 作为父 POM.依赖项 mod1 说请使用 2.6",但这与使用 2.1 的任何其他 pom 相同 - 没有理由更喜欢它另一个.这就是 dependencyManagement 的强大之处:您可以在一个地方控制所有可传递依赖项的版本.

and you put this into mod1, then the war won't notice since war uses parent as parent POM. The dependency mod1 says "please use 2.6" but that's the same as any other pom which uses 2.1 - there is no reason to prefer that over the other. That's the power of dependencyManagement: You get a single place where you can control the versions of all transitive dependencies.

试试 mvn help:effective-pom 看看 Maven 将在你构建的每个部分使用什么.

Try mvn help:effective-pom to see what Maven will use in each part of your build.

要使 WAR 选择覆盖的版本,您需要在 WAR 的父 POM 或 WAR 的 POM 本身中指定它.

To make the WAR pick up the overwritten version, you need to specify it in the parent POM of the WAR or in the WAR's POM itself.

通常的解决方案是为所有设置默认版本 (2.1) 的项目设置一个父 POM.然后每个项目都有一个从全局父 POM 继承的父 POM.在这里您可以将版本设置为 2.6.由于项目的所有模块都继承自它,因此每个项目的父 POM 优先.

The usual solution is to have a parent POM for all projects which sets the default versions (2.1). Then you have a parent POM per project which inherits from the global parent POM. Here you can set the version to 2.6. Since all modules of the project inherit from it, the per-project parent POM takes precedence.

这篇关于是否可以覆盖子 POM 中的依赖项版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆