How to get the pom properties at runtime?

Maven Properties Jam
I recently needed to access some of the pom properties at runtime. To be more specific, I needed the version related properties: artifact id, group id, and version. It turned out that there are at least three ways do that.

Each of the three approaches has its upsides and downsides. You feel free to pick the one that suits you the best.

Get the version info from the manifest

The correct place to find the version info is the manifest of the jar file. Maven unfortunately does not add it there by default. You can instruct maven to do that for you by properly configuring the maven-jar-plugin.

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
    <archive>
        <manifest>
            <mainClass>org.programirame.Main</mainClass>
            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
        </manifest>
    </archive>
</configuration>
</plugin>

The important moment here is to set the <strong>addDefaultImplementationEntries</strong> value to true. This tells maven to add the following values to the manifest file:

  • Implementation-Title: ${project.name}
  • Implementation-Version: ${project.version}
  • Implementation-Vendor-Id: ${project.groupId}
  • Implementation-Vendor: ${project.organization.name}
  • Implementation-URL: ${project.url}

After the jar artifact is built in this way, getting the info in the code is easy:

Package mainPackage = Main.class.getPackage();
String version = mainPackage.getImplementationVersion();
String groupId = mainPackage.getName();
String artifactId = mainPackage.getImplementationTitle();

This maybe is a bit more difficult to test but it is a very easy and straightforward. However, if you need to access any other pom properties, this won’t work.

Get the pom properties using filtering

Maven has the option of filtering. It allows you to replace variables in your resource files with actual values of pom properties. First, create a pom.properties file in your resource folder. The name of the file is arbitrary. Add the variables you need to the file:

artifactId=${project.artifactId}
groupId=${project.groupId}
version=${project.version}

Next, configure the resource section of the build stage:

<build>
<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        <includes>
            <include>**/pom.properties</include>
        </includes>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>false</filtering>
        <excludes>
            <exclude>**/pom.properties</exclude>
        </excludes>
    </resource>
</resources>
.....
</build>

This instructs maven to go over the pom.properties files and change all the variables with actual values. After you build the project the file can be found in the resulting jar, containing all the real values. All you need to do then is read the property file as you would read any other property file in java:

Properties properties = new Properties();
properties.load(Main.class.getResourceAsStream("/pom.properties"));

String version = properties.getProperty("version");
String artifactId = properties.getProperty("artifactId");
String groupId = properties.getProperty("groupId");

 

Get the pom properties using the properties plugin

Maven has a <a href=”http://www.mojohaus.org/properties-maven-plugin/usage.html”>dedicated plugin</a> for getting pom properties in and out of files. This is how you configure writing the properties to a file names version.properties:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
    <execution>
        <phase>generate-resources</phase>
        <goals>
            <goal>write-project-properties</goal>
        </goals>
        <configuration>
            <outputFile>
                ${project.build.outputDirectory}/version.properties
            </outputFile>
        </configuration>
    </execution>
</executions>
</plugin>

This will write all properties defined in the properties section of the pom into the version.properties file. But this has its downside. If you want a property to appear in the file, you will have to define it in the properties section. The version info does not appear there by default.

Sharing is caring