Programmatically getting the Maven version of your project

Posted on August 31, 2011

It is often handy to be able to extract the Maven version of your project at run time, either for displaying in an about box, or in debugging information. One option is to read /META-INF/maven/groupId/{artifactId}/pom.properties. However, this file is only available in the packaged version of your project, so during development the method will fail.

The approach I’ve taken to fulfil this requirement is to create a text (properties) file in the project resources, and have Maven process this. This allows Maven to inject the version number during compile. The following snippets need to be configured.

The version file, such as /src/main/resource/version.prop

version=${project.version}

pom.xml

Java method to extract the version

Note, I only filter the files *.prop, because if you have any templates in your log4j.properties file (such as ${catalina.base}) these will also get wiped out.