Monday, April 11, 2011

Showing the svn / build version number in your (web) application

It is often useful to include the version information in your (web) applications so you can track which code/build that your customers use. This comes handy especially when you need to solve bugs. Always mention which version of code/build where the bug occurs when you write a bug reproduction scenario in your bug tracking tools (e.g. Bugzilla.) As an example, one of the most common way to include the version information is in the "About" dialog.

Showing a version number in a JSP web application
For example you can include this in your jsp:

Version: <%= application.getInitParameter("version-svn ") %>

which reads the version-svn parameter value in the web.xml:


Changing the web.xml parameter value using Ant script
In the Ant build.xml you can use XmlTask to change the version number in the web.xml:

<taskdef classname="com.oopsconsultancy.xmltask.ant.XmlTask" name="xmltask">

<target name="changeversionwebxml" depends="getsvnver">

<xmltask dest="${webinf}/web.xml" source="${webinf}/web.xml" failwithoutmatch="true">

  <replace path="/*[local-name()='web-app']/*[local-name()='context-param'][1]/*[local-name()='param-value'][1]/text()" withtext="${revision.max}">



The xpath predicate "/*[local-name()='name of an element']" is necessary to resolve the namespaces in the web.xml. The [1] is necessary since in my web.xml I use more than one "web-app/context-param/param-value" element, so I need to tell the xmltask that I want to change the first "param-value" that it finds. The ${revision} property is set using another Ant target described below.

Getting the svn revision number using Ant script
We can use SvnAnt to get the highest svn revision number of your source tree starting from the ${basedir}.

<path id="svnant.classpath">

<!-- if you use EclipseIDE, add the svnant jars using eclipse>preference>ant>classpath -->


<include name=" ">



<typedef classpathref="svnant.classpath" resource="org/tigris/subversion/svnant/svnantlib.xml">
<target name="getsvnver">
<wcVersion path="${basedir}"/>
<echo message= "Subversion revision.max: ${revision.max}" />

The ${basedir} points to your working directory which contains your source codes along with .svn files (svn bookkeeping files), one way to define the basedir in the build.xml for example:

<project name="project name" basedir="../" default="your default target">

Using a build number
Actually the svn revision information is more appropriate for developers. For a shipped product, a build number will be more useful. Instead of the svn number, you'd better include extra info such as time, build machine, build owner (developer who run the build task). For example using Hudson (a continuous integration tool) you can access the build information using environmental variables:
<property environment="env"/>
<property name="buildinfo" value="${env.JOB_NAME}-${env.BUILD_NUMBER}"/>

You can substitute the ${revision.max} in the xmltask above with this ${buildinfo}.

Please post your comments / suggestions. Thank you.

No comments: