每次构建后使用 Maven 增加 属性 值

Increase property value with Maven after each Build

这是 build.xml 上的脚本,在非 maven 项目中,在 Netbeans 上,每次我 "Build",它都会增加 1。

<target name="-pre-compile" description="Sets the buildversion for the current build">
<propertyfile file="${src.dir}\recursos\language.properties">
    <entry key="application.buildnumber" value="1" type="int" operation="+"/>
    <entry key="application.builddate" value="now" type="date"/>
</propertyfile>
</target>

这是我使用的资源文件,我也希望 Maven 编写它:languange.properties

application.title=Software title...
#build version control
application.buildnumber=334
application.builddate=2016/09/07 15\:16
application.version=1
application.icon=/icons/icon.png

我已经读过有关 mojohaus 的资料,但似乎不符合我的需要。

我知道我必须添加一个带有一些 Executions/Goals 标签的插件,但我不知道如何告诉 Maven 将 属性 的值增加 1。

以下是我成功实施的方法:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.0.0</version>
            <executions>
                <execution>
                    <phase>initialize</phase>
                    <id>read-props</id>
                    <goals>
                        <goal>read-project-properties</goal>
                    </goals>
                    <configuration>
                        <files>
                            <file>src/main/resources/build.properties</file>
                        </files>
                    </configuration>
                </execution>
                <execution>
                    <phase>generate-resources</phase>
                    <id>write-props</id>
                    <goals>
                        <goal>write-project-properties</goal>
                    </goals>
                    <configuration>
                        <outputFile>src/main/resources/build.properties</outputFile>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.gmaven</groupId>
            <artifactId>gmaven-plugin</artifactId>
            <version>1.4</version>
            <executions>
                <execution>
                    <id>add-dynamic-properties</id>
                    <phase>initialize</phase>
                    <goals>
                        <goal>execute</goal>
                    </goals>
                    <configuration>
                        <source>
                                project.properties.buildnumber = (project.properties.buildnumber.toInteger() + 1).toString();
                        </source>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <!-- debug print out, to be removed afterwards -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.5</version>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <target>
                            <echo message="${buildnumber}" />
                        </target>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

我们实际上在做什么:

  • 我们正在使用 properties-maven-plugin 从文件中读取文件(通过其 read-projet-properties 目标),src/main/resources/build.propertiesinitialize 阶段,因此真的很早默认构建周期
  • 现在,buildnumber 属性 已从文件和我们构建的一部分中提取。在同一阶段,我们使用 gmave-plugin 通过一个小脚本增加它的值。
  • 然后在 generate-resources 阶段(作为示例)我们 writebuildnumber 的新值(覆盖)到同一个文件,用于下一次迭代。这一步是基础,因为我们需要将状态存储在某个地方,并且它应该在版本控制下,即项目的一部分。 src/main/resources 可能不是最佳选择,因为它将与应用程序一起打包(您总是可以跳过它),因此您可以将它存储在其他文件中(但它仍然应该是版本化项目的一部分)。
  • 然后,作为 debug/proof,antrun 将打印当前的 buildnumber 值。

要使其工作,build.properties 文件中 buildnumber 属性 的初始值应设置为 0(属性 必须预先存在)。然而,这个文件也有可能在版本控制下不断发生冲突,这就是为什么整个行为应该被包装到一个 maven 配置文件中并且只在某些情况下使用(例如,在发布期间的团队领导)。这种约束实际上会导致更标准的方法:让 CI 服务器处理整个机制,而不是 maven。

旁注:不幸的是,properties-maven-plugin 没有提供太多配置,它总是会读取和写入所有构建属性,这在大多数情况下是无害的,尽管不是最佳选择。更好的办法是有一个 include/exclude 机制来过滤并且只有 read/write buildnumber 属性.