Software Development – Java – Open Source
| Automatically create releases and maintenance branches with Maven »

Easy way to display your apps version number using Maven

Wednesday, May 30th, 2007 at 11:47 pm

Recently I wanted to print our apps version number to the log file. Since I’m using Maven and the Maven Release Plugin to create releases of our app I was looking for an easy way to incorporate the version in the pom.xml into the release and display it.

Fortunately Maven already provides a hook to have the version number written to a jars MANIFEST.MF. I simply added the following snippet to my pom.xml:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>test.App</mainClass>
            <addDefaultImplementationEntries>
              true
            </addDefaultImplementationEntries>
          </manifest>
        </archive>
      </configuration>
    </plugin>
  </plugins>
</build>

The important part is addDefaultImplementationEntries. This instructs Maven to add the following headers to the MANIFEST.MF of your jar:

Implementation-Title: display-version Implementation-Version: 1.0-SNAPSHOT Implementation-Vendor-Id: test

Now you can use this line of code to access the version number:

App.class.getPackage().getImplementationVersion()

where “App” is just any class in your jar.

In defiance of all those Maven haters out there thing are sometimes just that easy with Maven ;)

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to “Easy way to display your apps version number using Maven”

  1. Java Donkey Says:
    June 5th, 2007 at 4:05 pm

    Yes, that is nice functionality provided by Maven.

    Something somewhat related that I like to do with web apps is incorporate a build ID into the JSP pages too. The JSP can read it from a properties file that gets updated by Maven dynamically. This is very useful when you have multiple builds on multiple tiers being used by different clients. It’s not really a Maven specific thing but just something I thought of…

Leave a Reply