Tarin Gamberini

A software engineer and a passionate java programmer

How to include version information into a war using Maven

The scenario is the one in which we want to know some information about a deployed war going to a public url such as https://example.com/version.properties.

Achiving that is quite simple using maven.

The main idea leverages the maven filtering feature in order to filter a template file, for instance version.properties, which contains properties you’d like to see.

The version.properties template could be:

project.artifactId=${project.artifactId}
project.version=${project.version}
build.dateTime=${myproject.build.dateTime}
package_name=${project.artifactId}-${project.version}.${project.packaging}

On the left there are the properties name while on the right there are the properties value you are interested in.

Add into your pom.xml a suitable version of the maven-war-plugin:

pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <webResources>
            <resource>
                <filtering>true</filtering>
                <directory>src/packaging/</directory>
            </resource>
        </webResources>
    </configuration>
</plugin>

in which you configure the path to the version.properties template and enabling properties filtering.

The configuration call such directory path a webResources to point out that files contained there will end up in the root of the war so they are available as a public URL like https://example.com/version.properties. Conversely ordinary resources usually end up in WEB-INF/classes and are not available as public URL.

Download

Download the working example how_to_include_version_info_into_a_war from GitHub.

4 Comments to “How to include version information into a war using Maven”

Posted by Horatiu #

FYI: You have missed a tag:

 <webResources>
  <resource>
  ....
  </resource> 

In the GIT code it is correct! :)

Thanks for this post…really helped me to solve my problem.

Posted by Tarin Gamberini # Tarin Gamberini

Dear Horatiu,

Thank you very much for pointing the missed tag out.

I’m glad you have found this post useful.

Best regards,
Tarin

Posted by Fredrik #

This was a great tutorial.

Can you list the dependencies used in the pom file to?

And is there a list of available properties value that you can use?

Posted by Tarin Gamberini # Tarin Gamberini

Thanks Fredrik.

If you use a more recent version of the maven-war-plugin you can add in the configuration tag the

<archive> <manifest> <addClasspath>true</addClasspath> </manifest> </archive>

Then you can find the depenencies list in the MANIFEST.MF Class-Path property.

For more information look at the maven-war-plugin and maven-dependency-plugin.

Cheers,
Tarin

Post a comment

A comment is submitted by an ordinary e-mail. Your e-mail address will not be published or broadcast.

This blog is moderated, therefore some comments might not be published. Comments are usually approved by the moderator in one/three days.