Home » Java » Enterprise Java » Getting version string from a Maven based web application

About Zemian Deng

Zemian Deng

Getting version string from a Maven based web application

When you package a maven project, it will automatically generate a pom.properties file inside that will contains the version, artifactId and groupId information. These are handy to have and to display for your web application at runtime. One can use a method like following to retrive it.

public class Application {
     private String version;

     public String getVersion() {
        if (version == null) {
            String res = "META-INF/maven/myapp/pom.properties";
            URL url = Thread.currentThread().getContextClassLoader().getResource(res);
            if (url == null) {
                version = "SNAPSHOT." + Utils.timestamp();
            } else {
                Properties props = Utils.loadProperties(res);
                version = props.getProperty("version");
            }
        }
        return version;
    }
}

Sounds good? Not too fast! Turns out you have to do little more trick to have this working properly for deployment. By default the maven war plugin will package your classes files into the WEB-INF/classes, but the pom.properties are in META-INF at the same level, and not in WEB-INF/classes/META-INF! This resulted the above code not finding your resource pom.properties from classpath!

To fix this, you need to add the following to your pom.xml file:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <archiveClasses>true</archiveClasses>
                </configuration>
            </plugin>

This will tell maven to jar up your classes along with the pom.properties in a separate file, then place it in WEB-INF/lib folder instead of using the unpacked WEB-INF/classes version. This forces the pom.properties to be properly added and read by our getVersion() method.

(0 rating, 0 votes)
You need to be a registered member to rate this.
5 Comments Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
lmpampaletakis
lmpampaletakis
5 years ago

It is not javaScript, it is Java…. So “version == null” is not right… Use equals instead

Dias Neto
Dias Neto
4 years ago
Reply to  lmpampaletakis

Why is it not right? I don’t see any problem to use == operator when I am checking if a variable is null, doesn’t matter what I have in equals or hashCode implementations, it is null!

Dias Neto
Dias Neto
4 years ago
Reply to  Dias Neto

The more importat detail, if version is really null, using equals method would throw a NullPointerException, so it is totally wrong to use equals method.

Gerry Matte
5 years ago

In the code snippet (line 11) refernce is made to Utils.loadProperties(res) but there is no way to tell where one should find this method.

Gerry Matte
5 years ago

Understanding what will be the correct value for “res” in line 6 is not obvious.
I suggest that line 6 be replaced by three lines that obtain their values from your project’s POM.XML
String groupId=”ca.gerrymatte.tests”;
String artifactId=”jbosstests”;
String res = “META-INF/maven/”+groupId+”/”+artifactId+”/pom.properties”;

The gotcha for me was the unexpected “/” between groupId and artifactId.