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

查看:30
本文介绍了在 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

build-helper:released-version

可能 regex-property 是您想要的,但是如果你的版本号符合标准",另外两个可能会救你.

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 目标,您需要做一些事情喜欢

To use the regex-property goal you would do something like

<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天全站免登陆