Maven 项目版本继承 - 我是否必须指定父版本? [英] Maven project version inheritance - do I have to specify the parent version?

查看:64
本文介绍了Maven 项目版本继承 - 我是否必须指定父版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个项目:父项目:A,子项目:B

I have two projects: Parent project: A, Sub project: B

A/pom.xml:

<groupId>com.dummy.bla</groupId>
<artifactId>parent</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>pom</packaging>

在 B/pom.xml 中,我有:

And in B/pom.xml, I have:

    <parent>
        <groupId>com.dummy.bla</groupId>
        <artifactId>parent</artifactId>
        <version>0.1-SNAPSHOT</version>     
    </parent>

    <groupId>com.dummy.bla.sub</groupId>
    <artifactId>kid</artifactId>

我希望 B 从父级继承版本,所以在我的情况下,我需要放置 0.1-SNAPSHOT 的唯一地方是 A/pom.xml.但是,如果我从父部分下的 B/pom.xml 中删除 <version>0.1-SNAPSHOT</version>,maven 会抱怨缺少父版本.

I want B to inherit the version from parent, so the only place in my case I need to put 0.1-SNAPSHOT is A/pom.xml. But if i remove the <version>0.1-SNAPSHOT</version> from B/pom.xml under the parent section, maven complains about the missing version for the parent.

有没有办法可以只使用 ${project.version} 或类似的东西来避免在两个 poms 中都有 01.-SNAPSHOT ?

Is there a way I can just use ${project.version} or something like this to avoid having 01.-SNAPSHOT in both poms?

推荐答案

从 Maven 3.5.0 开始,使用 ${revision} 有一个很好的解决方案占位符.有关详细信息,请参阅 FrVaBe 的回答.对于以前的 Maven 版本,请参阅下面我的原始答案.

Since Maven 3.5.0 there is a nice solution for this using ${revision} placeholder. See FrVaBe's answer for details. For previous Maven versions see my original answer below.

不,没有.您始终必须指定父母的版本.幸运的是,在大多数情况下,它作为模块的版本被继承.此外,这个父级的版本声明是由 Maven Release Plugin 自动碰撞的,所以 - 事实上 - 只要你使用 Maven Release Plugin 发布或只是碰撞版本,你有两个地方的版本不是问题.

No, there isn't. You always have to specify parent's version. Fortunately, it is inherited as the module's version what is desirable in most cases. Moreover, this parent's version declaration is bumped automatically by Maven Release Plugin, so - in fact - it's not a problem that you have version in 2 places as long as you use Maven Release Plugin for releasing or just bumping versions.

请注意,在某些情况下,这种行为实际上还不错,并提供了您可能需要的更多灵活性.有时您想使用一些以前的父版本来继承,但这不是主流情况.

Notice that there are some cases when this behaviour is actually pretty OK and gives more flexibility you may need. Sometimes you want to use some of previous parent's version to inherit, however that's not a mainstream case.

这篇关于Maven 项目版本继承 - 我是否必须指定父版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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