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

<build>
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
      <includes>
        <include>**/*.prop</include>
      </includes>
    </resource>
  </resources>
</build>

Java method to extract the version

public String getAPIVersion() {
	String path = "/version.prop";
	InputStream stream = getClass().getResourceAsStream(path);
	if (stream == null) return "UNKNOWN";
	Properties props = new Properties();
	try {
		props.load(stream);
		stream.close();
		return (String)props.get("version");
	} catch (IOException e) {
		return "UNKNOWN";
	}
}

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.