Tuesday, February 27, 2007

Maven: Keep track of your Build Number

Problem: You need to differentiate between build numbers. For example, you've just redeployed your application and need to ensure that the new version is what you are viewing and not the old one. Or, you need to keep track of how many times you build. There could be many reasons why you want to know the build number that you are on. For me, its just for fun.

Solution: maven-buildnumber-plugin. This Maven2 plugin will generate a unique build number each time your build your project. You can even configure which maven phase triggers the incrementation of the number. This plugin can also fetch data from SVN to ensure that a team of developers all get unique build numbers.

As a bonus, it generates a buildNumber.properties file so that you can read in this build number from anywhere in your project. Here is how I use the plugin.

First, update your pom.


<groupId>com.mycompany.myapp</groupId>
<artifactId>projectname</artifactId>
<packaging>war</packaging>
<version>1.0</version>

...

<pluginRepositories>
<pluginRepository>
<id>tlc</id>
<name>TLC Repository</name>
<url>http://commons.ucalgary.ca/pub/m2</url>
</pluginRepository>
</pluginRepositories>

...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>maven-buildnumber-plugin</artifactId>
<version>0.9.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>buildNumber0</item>
</items>
</configuration>
</plugin>
</plugins>
</build>
Now, by adding the following to your pom.xml, when you package your project, you'd get a file called

projectname-1.0.1.war


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


What I wanted, was to see the version on my application's index page. To do this, I use an ant filter to write the version and timestamp to a version.html file, and then copy it to my project' web app directory. I added this to my pom.xml.


<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>compile</phase>
<configuration>
<tasks>
<!-- versioning -->
<echo message="[build version]"/>
<delete file="target/projectname/version.html"/>
<tstamp>
<format property="rightNow" pattern="d MMM yyyy" locale="en"/>
</tstamp>
<copy todir="target/projectname">
<fileset dir="src/main/webapp">
<include name="version.html"/>
</fileset>
<filterset>
<filter token="VERSION" value="${buildNumber}"/>
<filter token="BUILTON" value="${rightNow}"/>
</filterset>
</copy>
<echo message=" version is ${buildNumber}"/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>


version.html is simple!

<label><em>Version</em></label> <small>@VERSION@</small><br>
<label><em>Built On</em></label> <small>@BUILTON@</small><br>

Finally, in my index page...

<jsp:include page="/version.html"/>
Almost everything you want to know about this plugin can be found on the plugin's site: http://commons.ucalgary.ca/projects/maven-buildnumber-plugin/introduction.html

6 comments:

Bhanu Prakash Koppaka said...

Actually its not creating war with updated version.html file. On compile its creating version.html under projectname directory. But its not including into war file.

Mike said...

This code performs the copy to the target. Make sure you replace things like projectname with your project final name.


<copy todir="target/projectname">
<fileset dir="src/main/webapp">
<include name="version.html"/>
</fileset>
<filterset>
<filter token="VERSION" value="${buildNumber}"/>
<filter token="BUILTON" value="${rightNow}"/>
</filterset>
</copy>

Bhanu Prakash Koppaka said...

Thanks Mike. I did the same. But its creating new version.html at the time of compilation, and its not putting into war (mvn war:war). Its taking version from source directly.

I am using same exact code you posted.

Mike said...

Try a mvn clean package

Bhanu Prakash Koppaka said...

Thanks for the reply. I observed some weird results.

When i do run mvn install first time its not creating version.html with build number, its coping orininal file into target. I mean, its not creating version.html with buildnumber after clean. Do you have this problem.

commands i executed:

mvn clean
mvn install - no build number in version.html
mvn install - there is a build number.
mvn clean install - no build number.
mvn install - there is a new build number

I used the same POM like yours

sk2 said...

Cheers for that tutorial!