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
<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <webResources>
            <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.

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.