My solution for the standard Maven WAR plugin
Add a resource tag to the assembly section, which allows you to filter (aka "search and replace"):
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> .... <build>
Then in your src / main / resources add the version.properties file containing any filter variables that correspond to one of the standard maven assembly variables (you can also use the filter function to load your own custom variables):
pom.version=${pom.version}
Now when you do a “maven package” or install maven, it copies the version.properties file to the WEB-INF / classes and performs a search and replace to add the pom version to the file.
To get this using Java, use a class, for example:
public class PomVersion { final private static Logger LOGGER = LogManager.getLogger(PomVersion.class); final static String VERSION = loadVersion(); private static String loadVersion() { Properties properties = new Properties(); try { InputStream inStream = PomVersion.class.getClassLoader().getResourceAsStream("version.properties"); properties.load(inStream); } catch (Exception e){ LOGGER.warn("Unable to load version.properties using PomVersion.class.getClassLoader().getResourceAsStream(...)", e); } return properties.getProperty("pom.version"); } public static String getVersion(){ return VERSION; } }
Now you can simply call PomVersion.getVersion () to put the version number of the pom file on the page. You can also get the WAR file with the same number using the filter variable in finalName inside pom.xml:
<build> <finalName>my-killer-app-${pom.version}</finalName> ... </build>
so now if you install the version of your application in your pom as 01.02.879:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.killer.app</groupId> <artifactId>my-killer-app</artifactId> <packaging>war</packaging> <name>This App Will Rule The World</name> <version>01.02.879</version> ... </project>
when you run "mvn install", the name of the war file includes the version number:
my-killer-app-01.02.879.war
Finally, if you use Spring heavily, for example using SpringMVC / SpringWebFlow, you can make an oneton bean service that uses this class to avoid referencing a low-level class by name:
@Service("applicationVersion") public class ApplicationVersion { final static String VERSION = PomVersion.getVersion(); public String getVersion() { return VERSION; } }