I am using Maven 2 to build my Java project and I am looking for a way to present the current version number of the pom.xml to the user (using a Servlet or JSP for example).
Among the answers we see a few variations of the "load a properties file" method and I'm going to append to that with yet another alternative version that works with Maven 3.5, but probably also with older versions.
You only need to do one thing in the pom.xml
; turn on something called resource filtering. This is easily done by finding the
tag and placing your resource folder which you wish to filter in there. It will look like follows:
...
src/main/resources
true
...
...
Note: the elipsis ...
means code is being omitted
if you want to, you may also add your custom variables, but this is not necessary:
1.1-SNAPSHOT
...
Create a .properties
file and place it in your maven resources file. I'm going to call mine application.properties
. For simplicity I'm going to put it in the default resources folder; src/main/resources
but you can edit that to pretty much any folder you'd like.
|-- pom.xml
|-- src
|-- main
|-- java
|-- webapp
|-- resources
`-- application.properties
In the application.properties
file I'm going to add the version property I want:
author=eFox
build=${build.version}
version=${project.version} # This is the default maven variable that you could and should use.
This is where my method differs from the above mentioned versions. Instead of loading the properties file as a Property class, load the META-INF pom.properties
or make this a controller class, the we're going to load it as a resource:
<%@ page import = "java.util.ResourceBundle" %>
<% ResourceBundle resource = ResourceBundle.getBundle("application");
String version = resource.getString("version");
String author = resource.getString("author");%>
...
...
Site version: <%=version %> by <%=author%>