在Maven中,如何在运行时动态构建属性值? [英] In Maven, how can I dynamically build a property value at runtime?

查看:424
本文介绍了在Maven中,如何在运行时动态构建属性值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在maven中,使用以下语法在pom中设置属性非常容易:

In maven it is very easy to set properties in a pom with the following syntax:

...
<properties>
  <myValue>4.06.17.6</myValue>
 </properties>
...

现在我需要构建一个取决于我的版本的属性POM。为了创建属性,我想执行以下操作(java伪代码):

Now I need to build a property which depends on the version of my pom. For creating the property i want to do the following (java pseudo code):

String[] parts = version.split("\\.");
String.format("V%s_%s_%s_P%s", splitted[0],  splitted[1],splitted[2],splitted[3]);
// example: 4.06.17.6 => V_4_06_17_P6

它应该是动态的,因为它在我们的存储库中用作标记名称,并且必须始终是与工件的版本同步。

It should be dynamic, because it is used as a tag name in our repository and must always be in sync with the version of the artifact.

如何创建动态属性的任何想法?

Any ideas how to create that "dynamic" properties?

推荐答案

Mojo的Build-Helper Maven插件可以帮助你。

Mojo's Build-Helper Maven Plugin can help you out here.

有许多目标可用于帮助改造属性。

There are a number of goals that can be used to help transform properties.

build-helper:parse-version

构建 - 帮助者:发布版本

可能正则表达式属性是你想要的,但是如果你的版本号符合标准,那么另外两个可能会为你节省。

Probably regex-property is the one you want, but if your version numbers conform to the "standards" the other two might save you.

使用 regex-property 你会做的目标

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.7</version>
        <executions>
          <execution>
            <id>regex-property</id>
            <goals>
              <goal>regex-property</goal>
            </goals>
            <configuration>
              <name>tag.version</name>
              <value>${project.version}</value>
              <regex>^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)\.(-SNAPSHOT)?$</regex>
              <replacement>V$1_$2_$3_P$4</replacement>
              <failIfNoMatch>true</failIfNoMatch>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>

注意:我的正则表达式可能略有偏差,所以你应该测试上面的内容。

Note: my regex might be slightly off so you should test the above.

注意:属性值仅在此执行绑定的阶段之后可用于执行。它所绑定的默认阶段是 validate 但如果您处于不同的生命周期(例如站点生命周期),则该值将不可用。

Note: The property value will only be available for executions after the phase that this execution is bound to. The default phase that it is bound to is validate but if you are on a different lifecycle (e.g. the site lifecycle) the value will not be available.

这篇关于在Maven中,如何在运行时动态构建属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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