javamavenmaven-3pom.xmlbuild-numbers

Increase property value with Maven after each Build


This is the script on build.xml, in a non maven project, on Netbeans, every time that I "Build", it is increased by 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>

This is resources file that I use and I want Maven write it too: 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

I already read about mojohaus but doesn't seems to fit what I need.

I know that I have to add a Plugin, with some Executions/Goals tags, but I don't know how to tell Maven that increase that property's value by 1.


Solution

  • Here is how I successfully implemented it:

    <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>
    

    What we are actually doing:

    To make it work, the initial value of the buildnumber property in the build.properties file should be set a 0 (the property must exist upfront). However this file would also risk to be continuously in conflict being under version control, that's why the whole behaviour should be wrapped into a maven profile and used only in certain cases (e.g. the team leader during a release). This constraint would actually lead to a more standard approach: let a CI server handle the whole mechanism, not maven.

    Side note: unfortunately, the properties-maven-plugin doesn't provide much configuration, it will always read and write all the build properties, which is harmless in most of the case although not optimal. Better would be to have an include/exclude mechanism to filter and only read/write the buildnumber property.