Loading POM version in your Java application

This can be done by writing your pom version into the Manifest file and then loading your manifest in your java code. Note that this will not work when running your code from eclipse IDE.

pom plugin configuration
Using the maven archiver plugin to write pom information into the application Manifest:

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

Loading and reading Manifest
Simply access the resource using the Manifest class

    Manifest manifest = new Manifest(this.getClass().getResourceAsStream("/META-INF/MANIFEST.MF"));
    String version = manifest.getMainAttributes().getValue("Implementation-Version");
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s