Tuesday, March 22, 2011

Maven auto increment build number

Recently I have been working on auto incrementing build number and generating build artefact name containing build number for application build with maven. Here is how it was done.

In the pom.xml file add following plugin:

<plugins>
         <plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.0-beta-4</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<doCheck>true</doCheck>
<doUpdate>false</doUpdate>
<format>${version}.{0,number}</format>
<items>
<item>buildNumber</item>
</items>
</configuration>
</plugin>

Now the $buildNumber value can be used from inside the pom file e.g. this way:


<build>
<finalName>
${project.artifactId}-${buildNumber}
</finalName>



Every time build gets to the maven validate phase, there is a buildNumber.properties file created or updated with current build number. However there is a catch.

This solution is good if the buildNumber.properties file is not being moved or deleted  and I have learned that if the file is gone, the build number will be reset to 1 again, making this not usable, especially on dynamically created build agents, as it is used in my organization (Teamcity and Amazon EC2 integration).

More information about the build number plugin: http://mojo.codehaus.org/buildnumber-maven-plugin/

No comments: