Technology weblog

Wednesday Jun 06, 2007

Having a time stamp of your Maven build available in your web application

Having a time stamp printed in on or more pages of your web application may be useful in development environments, where a new version of an application may be generated on a nightly basis, or even using continuous integration. This time stamp makes it explicit at which time the current version has been built, hence which features should be present and/or which bugs should have been fixed.

This may readily be accomplished by using the Maven Ant Run plug-in.

- P U T C O M P I L E T I M E T O K E N I N P A G E

<format property="TODAY" pattern="dd/MM/yyyy hh:mm"/>
<property name="war" value="${basedir}/target/${artifactId}-${version}.war"/>

<property name="wardir" value="${basedir}/target/${artifactId}-${version}"/>
<!-- Remove existing WAR -->
<delete file="${war}" />

<replace file="${wardir}/WEB-INF/pages/myPage.html"
token="@@@" value="${TODAY}"/>
<jar destfile="${war}" basedir="${wardir}"/>

Here we have placed a special token, namely @@@ in the page myPage.html where the time stamp is to be substituted. After the packaging stage

  • The resulting WAR is deleted
  • The token in the myPage.html file is replaced by the current time stamp
  • A new WAR file is generated

Since the substitution takes place in the target directory of Maven, the WAR is guaranteed to be the same WAR as before. Moreover, this way we do not touch any of the source files, which is necessary for usage with Continuum. Otherwise Continuum would detect a change in one of the affected source files, and would therefore initiate a new check-out and build, which would continuously be repeated, even though none of the developers actually commited a change! This will lead to thousands of builds in no-time!

Also, it turned out to be necessary to throw away the WAR file packaged by Maven first and to carry out this step after the packaging stage. Any attempt at any other stage turned out to be impossible, as prior stages don't have the files available at the expected location and any substitutions at later stages are no longer included in the already packaged WAR file.

Summarizing, the above method seems to be the simplest possible solution to get a time stamp reflecting the built time in one of your pages of your web application. Of course, the Ant script may easily be extended to affect more or even all of your pages, if needed.


Post a Comment:
Comments are closed for this entry.

Hire us