Java 带有属性的 Maven 版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19123013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:18:38  来源:igfitidea点击:

Maven version with a property

javamavenversion

提问by Jan Pe?ta

I have big Maven (Tycho) project witch about 400 plug-ins.

我有大约 400 个插件的大型 Maven (Tycho) 项目。

We have specified version of application in each POM file.

我们在每个 POM 文件中都指定了应用程序的版本。

Is there a way how to specify the version for all POM:s only on one place?

有没有办法只在一个地方为所有 POM:s 指定版本?

I would expect some think like:

我希望有些人会这样想:

<properties>
<buildVersion>1.1.2-SNAPSHOT</buildVersion>
</properties>

....

<version>${buildVersion}</version>

We have parent pom.xml:

我们有父母pom.xml

<modelVersion>4.0.0</modelVersion>
<groupId>company</groupId>
<artifactId>build.parent</artifactId>
<version>1.1.2-SNAPSHOT</version>
<packaging>pom</packaging>

Then in each POM is reference to parent POM:

然后在每个 POM 中引用父 POM:

<parent>
  <artifactId>build.parent</artifactId>
  <groupId>company</groupId>
  <relativePath>../build.parent/pom.xml</relativePath>
  <version>1.1.2-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<groupId>company</groupId>
<artifactId>artifact</artifactId>
<version>1.1.2-SNAPSHOT</version>
<packaging>eclipse-plugin</packaging>

采纳答案by GeroldBroser reinstates Monica

See the Maven - Usersforum 'version' contains an expression but should be a constant. Better way to add a new version?:

请参阅Maven - 用户论坛'version' 包含一个表达式但应该是一个常量。添加新版本的更好方法?

here is why this is a bad plan.

the pom that gets deployed will not have the property value resolved, so anyone depending on that pom will pick up the dependency as being the string uninterpolated with the ${ } and much hilarity will ensue in your build process.

in maven 2.1.0 and/or 2.2.0 an attempt was made to deploy poms with resolved properties... this broke more than expected, which is why those two versions are not recommended, 2.2.1 being the recommended 2.x version.

这就是为什么这是一个糟糕的计划。

部署的 pom 不会解析属性值,因此依赖该 pom 的任何人都会将依赖项视为未插入 ${ } 的字符串,并且在您的构建过程中会发生很多欢闹。

在 maven 2.1.0 和/或 2.2.0 中尝试部署具有解析属性的 poms ......这超出预期,这就是为什么不推荐这两个版本的原因,2.2.1 是推荐的 2.x 版本.

回答by Frederic Close

If you have a parent project you can set the version in the parent pom and in the children you can reference sibling libs with the ${project.version}or ${version}properties.

如果您有父项目,您可以在父 pom 中设置版本,在子项目中您可以使用${project.version}${version}属性引用同级库。

If you want to avoid to repeat the version of the parent in each children: you can do this:

如果你想避免在每个孩子中重复父母的版本:你可以这样做:

<modelVersion>4.0.0</modelVersion>
<groupId>company</groupId>
<artifactId>build.parent</artifactId>
<version>${my.version}</version>
<packaging>pom</packaging>

<properties>
<my.version>1.1.2-SNAPSHOT</my.version>
</properties>

And then in your children pom you have to do:

然后在您的孩子 pom 中,您必须执行以下操作:

    <parent>
      <artifactId>build.parent</artifactId>
      <groupId>company</groupId>
      <relativePath>../build.parent/pom.xml</relativePath>
      <version>${my.version}</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <groupId>company</groupId>
    <artifactId>artifact</artifactId>
    <packaging>eclipse-plugin</packaging>

    <dependencies>
        <dependency> 
           <groupId>company</groupId>
           <artifactId>otherartifact</artifactId>   
           <version>${my.version}</version>
or
           <version>${project.version}</version>
        </dependency>
    </dependencies>

hth

回答by Constantino Cronemberger

Using a property for the version generates the following warning:

使用版本的属性会生成以下警告:

[WARNING]
[WARNING] Some problems were encountered while building the effective model for xxx.yyy.sandbox:Sandbox:war:0.1.0-SNAPSHOT
[WARNING] 'version' contains an expression but should be a constant. @ xxx.yyy.sandbox:Sandbox:${my.version}, C:\Users\xxx\development\gwtsandbox\pom.xml, line 8, column 14
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]

If your problem is that you have to change the version in multiple places because you are switching versions, then the correct thing to do is to use the Maven Release Plugin that will do this for you automatically.

如果您的问题是因为您要切换版本而必须在多个地方更改版本,那么正确的做法是使用 Maven Release Plugin,它会自动为您执行此操作。

回答by rince

If you're using Maven 3, one option to work around this problem is to use the versions plugin http://www.mojohaus.org/versions-maven-plugin/

如果您使用的是 Maven 3,解决此问题的一种选择是使用版本插件 http://www.mojohaus.org/versions-maven-plugin/

Specifically the commands,

具体的命令,

mvn versions:set -DnewVersion=2.0-RELEASE
mvn versions:commit

This will update the parent and child poms to 2.0-RELEASE. You can run this as a build step before.

这会将父和子 poms 更新为 2.0-RELEASE。您可以在此之前将其作为构建步骤运行。

Unlike the release plugin, it doesn't try to talk to your source control

与发布插件不同,它不会尝试与您的源代码控制对话

回答by ACV

The correct answer is this (example version):

正确答案是这个(示例版本):

  • In parent pom.xml you should have (notinside properties):

    <version>0.0.1-SNAPSHOT</version>
    
  • In all child modules you should have:

    <parent>
        <groupId>com.vvirlan</groupId>
        <artifactId>grafiti</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    
  • 在父 pom.xml 中你应该有(不在里面properties):

    <version>0.0.1-SNAPSHOT</version>
    
  • 在所有子模块中,您应该具有:

    <parent>
        <groupId>com.vvirlan</groupId>
        <artifactId>grafiti</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    

So it is hardcoded.

所以它是硬编码的。

Now, to update the version you do this:

现在,要更新版本,请执行以下操作:

mvn versions:set -DnewVersion=0.0.2-SNAPSHOT
mvn versions:commit # Necessary to remove the backup file pom.xml

and all your 400 modules will have the parent version updated.

并且您所有的 400 个模块都将更新父版本。

回答by Kariem

With a Maven version of 3.5 or higher, you should be able to use a placeholder (e.g. ${revision}) in the parent section and inside the rest of the pom, you can use ${project.version}.

使用 Maven 3.5 或更高版本,您应该能够在父部分和 pom 的其余部分使用占位符(例如 ${revision}),您可以使用${project.version}.

Actually, you can also omit project properties outside of parentwhich are the same, as they will be inherited. The result would look something like this:

实际上,您也可以省略其他相同的项目属性parent,因为它们将被继承。结果看起来像这样:

<project>
    <parent>
    <artifactId>build.parent</artifactId>
    <groupId>company</groupId>
    <relativePath>../build.parent/pom.xml</relativePath>
    <version>${revision}</version>  <!-- use placeholder -->
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>artifact</artifactId>
    <!-- no 'version', no 'groupId'; inherited from parent -->
    <packaging>eclipse-plugin</packaging>

    ...
</project>

For more information, especially on how to resolve the placeholder during publishing, see Maven CI Friendly Versions | Multi Module Setup.

有关更多信息,尤其是有关如何在发布期间解析占位符的信息,请参阅Maven CI Friendly Versions | 多模块设置