Track every Build Number with Maven

Written by MikeNereson

October 9th, 2010 at 12:42 pm

Posted in Software Tools, java, maven

Tagged with , ,

With 2 comments


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. 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. I use it for reporting bugs against specific builds.


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 increment 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 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.xml. You need to setup the build trigger.


Now, you’ll need the build number and append it to your artifact’s final name. Add this to your pom.xml


Now you’re package goal will output a file named


This is a great start and now you can differentiate each and every build. However, I need to take this a step further.

I need 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. Add this to your pom.xm.

              <!-- versioning -->
              <echo message="[build version]"/>
              <delete file="target/projectname/version.html"/>
                <format property="rightNow" pattern="d MMM yyyy" locale="en"/>
              <copy todir="target/projectname">
                <fileset dir="src/main/webapp">
                  <include name="version.html"/>
                  <filter token="VERSION" value="${buildNumber}"/>
                  <filter token="BUILTON" value="${rightNow}"/>
              <echo message=" version is ${buildNumber}"/>

Version.html is simple.

<p class="version">Version: @VERSION@</p>
<p class="built">Built on: @BUILTON@</p>

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:


As an alternative to writing and importing version.html, you can read in an MVC controller and put the version in your page model. This is what I for my login pages. I use a java class to read the build number, format it, and cache it. I start the first build number at 00100, then format that to 0.1.0. So the next build is 00101 which gets displayed as 0.1.1.


Here is a usage tip. You can move the create goal into a build profile so that the build number only increments in specific situations. I use a profile for my Continuous Integration builds so that our version increments only when Hudson builds our apps. This also makes it possible to synchronize our Hudson build numbers to our module versions.


What do you use to track build numbers? How do your format it?


Related Posts


2 Responses to “Track every Build Number with Maven”

  1. Bonnie C on November 9th, 2012 1:05 am

    I would like to create the in ${basedir}/src/main/java so that I can read the buildNumber from my Spring controller. I have added the following line to pom.xml and checked in under src/main/java into svn repository.

    When I run mvn clean install, I get access denied and the is not updated.
    How do I get past the “Access denied” error?

  2. Emil on September 10th, 2015 4:29 pm

    Have you ever encountered a problem where the phase of create does not execute when you run a mvn clean install?

    I dont get anything from SVN with a mvn clean install, but if I run mvn buildnumber:create install it works. Any ideas?

Leave a Reply