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

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

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


<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>buildnumber-maven-plugin</artifactId>
      <version>1.0-beta-3</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, you’ll need the build number and append it to your artifact’s final name. Add this to your pom.xml

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

Now you’re package goal will output a file named

projectname-1.0.1.war

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.

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

<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: http://mojo.codehaus.org/buildnumber-maven-plugin/

Alternatives

As an alternative to writing and importing version.html, you can read buildNumber.properties 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.





Update:Tips

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.

Feedback

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

Share

Related Posts

Comments

8 Responses to “Track every Build Number with Maven”

  1. serr on October 13th, 2010 6:52 pm

    why use ant here?

    much better to use maven-replacer-plugin or maven resources filtering.

  2. MikeNereson on October 13th, 2010 10:28 pm

    @serr – I’ve never heard of maven-replacer-plugin. I’ll check it out. Thanks.

    I don’t use this method actually, it is just a simple way to get a version number into an HTML file.

    I use resources to package the buildNumber.properties into my jars so that all my web app’s modules have independent versions.

  3. Nick Stolwijk on October 14th, 2010 5:35 am

    Also, the buildnumber plugin has moved to Codehaus. The newest versions can be found in Central, so you don’t need the extra repository. The newest version is 1.0-beta-4, see: http://mojo.codehaus.org/buildnumber-maven-plugin/

  4. MikeNereson on October 16th, 2010 11:57 am

    @Nick Stolwijk – Thanks, Nick. I’ve updated the link to the project and updated the sample XML to the latest version. Cheers.

  5. Shagufta on January 30th, 2012 9:47 am

    Your Update:Tips seems quite useful for me as I am trying something similar. Can you please explain the below in bit more detail please.
    “You can move the create goal into a build profile”

    I am already having a release profile in which I want to move this goal. But how exactly to go about it?

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

    I would like to create the BuildNumber.properties 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 BuildNumber.properties under src/main/java into svn repository.

    ${basedir}/src/main/java
    When I run mvn clean install, I get access denied and the BuildNumber.properties is not updated.
    How do I get past the “Access denied” error?

  7. MikeNereson on November 9th, 2012 10:24 am

    @Bonnie C

    There is probably more than one way to accomplish what you’re trying to do. Here is how I do it.

    buildNumber.properties should be src/main/resources

    Then you’ll need to include it as a resource in your build


    <build>
    <resources>
    <resource>
    <directory>.</directory>
    <includes>
    <include>buildNumber.properties</include>
    </includes>
    </resource>
    <resource>
    <directory>src/main/resources</directory>
    </resource>
    </resources>
    </build>

    Now the file will be packaged in to /classes

  8. JIRA: HAF Spiral on May 23rd, 2013 11:21 am

    [HAFSPRL-49] Enchance the Build process …

    -Tag the haf build in svn as haf-v1000-b[nexttagnumber], when builds are made to the test environment. There should be option to tag the build when you deploy to Test. If the option is checked, the user has to give a tag number. That number should be ….

Leave a Reply