How to display maven project version in your webapp

Overview

Sometimes it is useful to display the version of your web application, typically in the footer area of the web page. By displaying the version information, one can quickly determine which version of the app is running or users can use it for error reporting purposes. Displaying the version is also useful when there are frequent automated releases using the maven release plugin and it is hard to track which version is actually deployed on a server. Here’s an illustration of how version can be displayed in the footer.

Project Version shown in footer

This article describes how to display the version of your maven project in your web application.

Step 1: Create a View file to display the version

First we will create a view file which will display the version. The view file could be a JSP, HTML, XHTML, a Facelet or any format depending on your stack. In this example, we will create a JSP file which will print the version somewhere in the page. However, instead of the hardcoding an actual version, we put a special token ‘PROJECT_VERSION’. We don’t want to hard code the version information as it will change from release to release. The special token will be replaced with actual maven project version when the app is packaged.

Create the following jsp.

src/main/webapp/version.jsp

Here’s a simple file which displays the version information.

<html>
  <body>Project version is <i>PROJECT_VERSION</i></body>
</html>

Step 2: Add snippet to pom.xml

The maven replacer plugin is a simple and useful plugin which can replaces tokens in file of your choice. Add the following snippet of code to your pom.xml. (Note: you can also use the maven resources plugin’s filter feature to accomplish this).

<project>
 <build>
  <plugins>
   <!-- replace version in file -->
   <plugin>
    <groupId>com.google.code.maven-replacer-plugin</groupId>
    <artifactId>maven-replacer-plugin</artifactId>
    <version>1.3.2</version>
    <executions>
     <execution>
      <!-- the replace should happen before the app is packaged -->
      <phase>prepare-package</phase>
      <goals>
       <goal>replace</goal>
      </goals>
     </execution>
    </executions>

    <configuration>
     <includes>
      <!-- replace the token in this file -->
      <include>target/myproject/version.jsp</include>
     </includes>
     <regex>false</regex>
     <!-- the name of the token to replace -->
     <token>PROJECT_VERSION</token>
     <!-- replace it with the maven project version -->
     <value>${project.version}</value>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>

To test the above setup, you can run the following command:

mvn prepare-package

Navigate to the following file.

target/myproject/version.jsp

The contents of this file will look something like this (assuming that your project version is 1.0-SNAPSHOT):

<html>
  <body>Project version is <i>1.0-SNAPSHOT</i></body>
</html>

When your application ships, the version.jsp file will always have the correct version. You can see the version by invoking the version.jsp page.

http://localhost:8080/myproject/version.jsp

Note:

  • Your view file does not have to be a JSP, it can be any a view file suitable to your stack. It could be part of your footer which could be included at the bottom in each page.
  • The special token can be changed from PROJECT_VERSION to another value of your choice, just change it in the view file as well as the pom.xml
  • Instead of having the version contained in a view file, you could have the version in a properties file or a message bundle and then display a message from the message bundle on the view
  • Update – you can also accomplish the replacement using the filtering feature of maven resource plugin

References

Related posts:

  1. How to automate project versioning and release with Maven
  2. The plugin ‘org.codehaus.mojo:selenium-maven-plugin’ does not exist or no valid version could be found
  3. Tweet your builds with Maven Twitter Plugin
  4. 3 ways to run Java main from Maven
  5. Maven Selenium

7 comments to How to display maven project version in your webapp

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Get Adobe Flash playerPlugin by wpburn.com wordpress themes