How to add a version number to your maven webapp

I couldn’t find a simple recipe to add a version number to a maven-built webapp. The maven-war-plugin talks about how to filter, but no simple example is given.

So. I’ll assume you are building your webapp with hudson, that you’re using subversion, and that your webapp has some text file(s) (.html or .jsp, perhaps?) in src/main/webapp that are a good place to display your version number.

  1. Add a “display_version” to your maven properties:
    [xml highlight="8"]
    <project …
    <groupId> …
    <artifactId> …
    <name> …
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <properties>
    <display_version>v${project.version}-${SVN_REVISION}</display_version>
    </properties>
    [/xml]

    Note that hudson will fill in the SVN_REVISION value for you. If you want some other number from the build in your version, you can use other hudson variables, but cook the version number to your taste.

  2. Configure the maven-war-plugin to enable filtering:
    [xml highlight="20"]
    <project

    <build>
    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1-alpha-2</version>
    <configuration>
    <nonFilteredFileExtensions>
    <nonFilteredFileExtension>gif</nonFilteredFileExtension>
    <nonFilteredFileExtension>ico</nonFilteredFileExtension>
    <nonFilteredFileExtension>jpg</nonFilteredFileExtension>
    <nonFilteredFileExtension>png</nonFilteredFileExtension>
    <nonFilteredFileExtension>pdf</nonFilteredFileExtension>
    </nonFilteredFileExtensions>
    <webResources>
    <resource>
    <directory>src/main/webapp</directory>
    <filtering>true</filtering>
    </resource>
    </webResources>
    </configuration>
    </plugin>
    </plugins>
    </build>
    </project>
    [/xml]
    (Note that the nonFilteredFileExtension prevents binary file corruption. It was added in the maven-war-plugin 2.1-alpha-2.)

  3. Finally, Insert ${display_version} into your webapp:
    I had a jsp/html file that had a footer with a copyright notice that was a nice place to append a version stamp:

    ...
    &lt;div class=&quot;footer&quot;&gt;(c) 2009 All rights reserved. ${display_version}&lt;/div&gt;
    

Run mvn war:exploded or mvn package to try it out.

Update December 14, 2009:

Note that Hudson’s SVN_REVISION macro is empty if you build your project as a maven multi-module. Use one of the other hudson macros in your display_version instead.

Related posts:

  1. How to add a version number to your hudson-built maven jar
    Maven’s jar plugin will automatically add a “pom.properties” in your jar’s META-INF/maven/$groupId/$artifactId directory. If you build with hudson, there are a number of other values that you might want to......
  2. How to increase maven heapspace in hudson builds
    If your maven-built project fails in hudson (especially when you’re using the assembly plugin) and it isn’t a compile or test failure, check the console output. If it says “java.lang.OutOfMemoryError:......
  3. How to set up your Maven2 POM to support Java5
    Seeing this? [bash highlight="3,4"] $ mvn compile … generics are not supported in -source 1.3 (try -source 1.5 to enable generics) [/bash] You need to tell the maven-compiler-plugin to use......
  4. Getting started with Hudson
    Hudson is a slick, stable, and easy to install continuous integration environment. You’ll be using it in minutes. Honest. Install Java Hudson is a java webapp. Ubuntu comes with Sun’s......