• 2.1.0

Versioning of JAR/WAR Application

Oracle recommends to use MANIFEST.MF for versioning of a Java application. We also recommend to use Manifests utility class for reading version data in runtime. For example, you're developing a web application and want to show its currently deployed version number somewhere in the HTML page. You can see how it works, for example, in www.s3auth.com. Pay attention to the small gray rectangle at the right bottom corner of the HTML page. It contains the version of the application and its Git revision hash. Every time a new version is deployed to the production server we see a new hash there. It is very convenient for developers and end-users.

You can see how it is done in www.s3auth.com by reading their pom.xml at Github. Let's reproduce it here step by step.

First, you should retrieve the revision number from your SCM (either it is Git, SVN, Mercurial, or something similar). We're using buildnumber-maven-plugin:

<build>
  <plugins>
    [...]
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>buildnumber-maven-plugin</artifactId>
      <version>1.1</version>
      <executions>
        <execution>
          <goals>
            <goal>create</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

With a default configuration this plugin will retrieve revision number and save it into buildNumber Maven property.

The next step is to set your custom META-INF/MANIFEST.MF attribute to this generated value. maven-war-plugin has a special configuration property exactly for this purpose:

<plugin>
  <artifactId>maven-war-plugin</artifactId>
  <configuration>
    <archive>
      <manifestEntries>
        <My-Version>${project.version}</My-Version>
        <My-Hash>${buildNumber}</My-Hash>
      </manifestEntries>
    </archive>
  </configuration>
</plugin>

After packaging of your WAR artifact take a look at META-INF/MANIFEST.MF file. You will see something like this (if you're using Git):

My-Version: 1.0-SNAPSHOT
My-Hash: d1cd2d628c3b56ab2766a0a658c040724e13d3ba

The last and final step is to read these values in the application and show them to the end-user inside HTML. If you're using JSP:

<html>
  <body>
    [...]
    <div id="version">
      <%= com.jcabi.manifests.Manifests.read("My-Version") %>
      <%= com.jcabi.manifests.Manifests.read("My-Hash") %>
    </div>
  </body>
</html>

That's it.