Problem:
You want to create a build number in the web front end of your application (e.g. to have it on screenshots appended to bug reports during user acceptance test).
You are using maven for your builds and your code is under source control (e.g. using subversion).
Solution:
All you need is some maven plugins and a place to write the build number to.
In this case I’d like to have the maven version number followed by the subversion revision number and a timestamp of the build date like this:
Version: 1.0-SNAPSHOT r1 Built on: 2012-02-19 13:41
First I create a version.html in src/main/resources or src/main/templates or something like that:
Version: @VERSION@
Built on: @BUILTON@
As you can see there are two placeholders (@VERSION@ and @BUILTON@)
Next I insert and configure the plugins to create the version info and the timestamp and to replace the placeholders with them in my pom.xml:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>maven-buildnumber-plugin</artifactId>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<doCheck>true</doCheck>
<doUpdate>false</doUpdate>
</configuration>
</plugin>
This creates a property named buildNumber with the revision number of the checked out code. The configuration doCheck=true causes a check whether there are any uncommitted changes. If there were any, the build would fail. With doUpdate you could instruct the build to execute a svn update prior to execution of the build.
For more info about this plugin see here:
http://mojo.codehaus.org/buildnumber-maven-plugin/
We could also use this plugins goal “create-timestamp” to create the timestamp, but since maven itself provides a property for that we just need to set that properties format in the properties section of our pom:
<properties>
<maven.build.timestamp.format>yyyy-MM-dd HH:mm</maven.build.timestamp.format>
</properties>
Now having all we need, we need to replace the placeholders in our html and copy the replacement to our target folder.
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>maven-replacer-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<file>src/main/templates/version.html</file>
<outputFile>target/version.html</outputFile>
<regex>false</regex>
<replacements>
<replacement>
<token>@VERSION@</token>
<value>${version} r${buildNumber}</value>
</replacement>
<replacement>
<token>@BUILTON@</token>
<value>${maven.build.timestamp}</value>
</replacement>
</replacements>
</configuration>
</plugin>
This replaces the placeholders in src/main/templates/version.html with the values and writes the result into the specified target folder.
More info about this plugin is provided here:
http://code.google.com/p/maven-replacer-plugin/
Having that, you just need to include that into your pages. That’s it.
Conclusion:
We now have a build number on our pages.
There for sure is room to improve this solution and it is for sure not the only solution. But it did the job for me and I hope it will help you.
If I should have forgotten something in this description, do not hesitate to give me feedback via the comments form.