Vineet Manohar is CTO / Co-Founder of online Car Search Engine, iSeeCars.com. Vineet is a DZone MVB and is not an employee of DZone and has posted 20 posts at DZone. You can read more from them at their website. View Full User Profile

How to Display Maven Project Version in Your Webapp Overview

09.23.2010
| 16615 views |
  • submit to reddit

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.

 

<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><strong>1.0-SNAPSHOT</strong></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

References

 

From http://www.vineetmanohar.com/2010/09/how-to-display-maven-project-version-in-your-webapp

Published at DZone with permission of Vineet Manohar, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Aleksey Maksimov replied on Thu, 2010/09/23 - 4:08am

Hi,

Why use plugin? This is out of the box Maven functionality, it is called "resource filtering":

http://www.sonatype.com/books/mvnref-book/reference/resource-filtering-sect-description.html 

 

Nicolas Frankel replied on Thu, 2010/09/23 - 4:56am

With filtering, you just create a properties file with ${project.version}. Now you can read it and make it available everywhere.

Milan Agatonovic replied on Thu, 2010/09/23 - 5:19am

or:

1. in pom.xml, declare maven-war-plugin,with configuration that enables filtering of directory src/main/webapp and includes **/version.html:

2. in src/main/webapp/version.html: Version: ${project.version}

3. you can see the version: http://localhost:8080/mywebapp/version.html

Milan

Barry Fitzgerald replied on Thu, 2010/09/23 - 5:34am

Bizarre...

Froeder Marvin replied on Thu, 2010/09/23 - 6:58am in response to: Barry Fitzgerald

+1 on the bizarre....

Resource filtering is all you need, and it is available out of box on maven.

Kjetil Ødegaard replied on Thu, 2010/09/23 - 11:03am

Another way is to read the pom.properties file which Maven creates in all WAR files under the META-INF directory. You can even create a Spring bean with <util:properties location="classpath:META-INF/groupId/artifactId/pom.properties" /> and just read from that.

Vineet Manohar replied on Thu, 2010/09/23 - 1:02pm

I hadn't used maven resource filtering before... it does seem more easier in this case.

Matt Young replied on Thu, 2010/09/23 - 2:52pm

I had problems with maven filtering a a while back. I found an alternate solution that I like very much. Here is how I do it:

1) in your pom add the following properties inside the <properties></properties> tags:

<maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss.SSS</maven.build.timestamp.format>
<buildDate>${maven.build.timestamp}</buildDate>

2) Add the following configuration to the build phase:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<app-version>${project.version}</app-version>
<build-date>${buildDate}</build-date>
</manifestEntries>
</archive>
</configuration>
</plugin>

The above 2 configurations make the manifest file for the war (there is a similar config for jars) contain a build timestamp and a version number.

3) create a listener that populates a Map of context entries from the manifest and store it in the application context. I have one in my utils jar that I use everywhere (see below). configure the listener to start as soon as the app loads in the web.xml.

public class ManifestAdapterContextLoadListener implements
ServletContextListener {
private static final Logger LOGGER = Logger.getLogger( ManifestAdapterContextLoadListener.class.getName() );


/**
* (non-Javadoc)
* @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
*/
public void contextInitialized(ServletContextEvent sce) {
try
{
Map<String,Object> manifest = new HashMap<String,Object>();
InputStream is =sce.getServletContext().getResourceAsStream("META-INF/MANIFEST.MF");
Properties props= new Properties();
props.load(is);
Set<Entry<Object,Object>> set=props.entrySet();

for (Entry<Object,Object> entry: set)
{
manifest.put(entry.getKey().toString(), entry.getValue());
}
sce.getServletContext().setAttribute("manifest", manifest);

}
catch (Exception e)
{
LOGGER.log(Level.SEVERE,"Unable to poulate the Manifest Map.",e);
}


}

/**
* (non-Javadoc)
* @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
*/
public void contextDestroyed(ServletContextEvent sce) {


}



}

4) in your JSPs, simply access the values from the manifest via the context variables as so:
Application Version. ${manifest['app-version']} / Build Date: ${manifest['build-date']}


Maxime Gréau replied on Sat, 2011/02/19 - 6:36am

Hi @Matt Young Unless you want to filter ${buildDate} in a properties file, you don't need to create the properties
...
<buildDate>${maven.build.timestamp}</buildDate> 
...
You can simlply do this :
...
<tmanifestEntries> 
<app-version>${project.version}</app-version> 
<build-date>${maven.build.timestamp}</build-date> 
</manifestEntries>
...
Regards.

Pankaj Tandon replied on Thu, 2011/03/10 - 2:44pm

Hi,

Just stumbled upon this discussion.

Here is an elegant solution that is a combination of several above.

Uses tags.

http://nayidisha.com/techblog/mycontent/posts/nd-utils/site/version.html

And is discussed here:

http://nayidisha.com/techblog/some-nifty-utilities

Later on I discovered the need to be able to display build times using maven (and not ant :)

For that, a simple solution would be to place the following in one of your pom.xmls:

  <properties>
     <maven.build.timestamp.format>MMM dd, yyyy 'at' HH:mm z</maven.build.timestamp.format>    
     <timestamp>${maven.build.timestamp}</timestamp>
  </properties>

 

And then create a maven filtered resource, typically a property file that contains:

version.date=${timestamp}
version.number=${project.version}

 

Which in turn is used by the JSPs.

 

I wouldn't filter the jsp. That would be a bit... um.. inelegant :)

 

 

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.