Using Maven build number plugin to load code revision details

In this blog post, I will demonstrate how to use Maven build number plugin to get build number and version details from your source code repository for use in Spring web applications. I use this technique for a number of web apps I develop to register which version of the codes (i.e. tag) the systems are running on to help operation support and debugging.

Project configuration

I have the following project setup:

  1. Maven 3
  2. Subversion as source code repository
  3. Eclipse IDE with m2e plugin

Getting build number from subversion

1. Add Maven plugin buildnumber-maven-plugin to pom.xml file

First add the following to your pom file:


The above plugin config adds the build number plugin to the build lifecycle. Note the use of the javasvn provider to connect to SVN.

2. Add build property placeholders

The build number plugin is now run every time you build the project and will make build number (e.g. SVN revision number) available to Maven. The next step is to include a properties file so the values can be passed into the web app via Spring. For example, create a file (e.g. in  src/main/resources) as below and add it to the files to be loaded by Spring property placeholder configurer:
build.version=${version}  # Maven version
build.revision=${buildNumber} # Source code revision number 
build.timestamp=${timestamp} # long value of check in time

Running build should then produce the following files with the values replaced with real ones from subversion, e.g.

3. Using build number in web app

Now you can use the build number in your web app. For example, in a Spring MVC controller interceptor to inject the values to the incoming http request for display in the web page footer.

public class PageRequestInterceptor extends HandlerInterceptorAdapter {

private String buildVersion;

private String buildRevision;

private DateTime buildTimestamp;

public void setBuildTimestamp(String timestamp) {
buildTimestamp = new DateTime(Long.parseLong(timestamp));

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

request.setAttribute(“buildVersion”, buildVersion);
request.setAttribute(“buildRevision”, buildRevision);
request.setAttribute(“buildDateTime”, buildTimestamp);

4. Fix up Eclipse m2e plugin lifecycle mapping

If you are using Eclipse, you also need to update the Maven plugin lifecycle mapping to enable the build number plugin. Otherwise, Eclipse auto build will overwrite the resolved build properties file created by the build number plugin, e.g. in /target/classes, with the one in your source path. Add the below in the pom file as child to the <build> tag:

 <!-- This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself. -->

You can verify the above is working by looking at the Project Maven lifecycle properties in Eclipse. RIght click on the project, the select Properties->Maven->Lifecycle Mapping

That’s it.