Skip to content

Showing off your SVN revision number in your web application

If you have serveral different environments to which you publish your web application, a common problem is to know what version of the application is installed in which environment. The solution to this is to publish the version number somewhere. But you want to automate the process. This is how you do.

My set up is Java, with Seam framework and JSF, ant as build tool and Hudson as continuous build server. Subversion is the version handling tool.

What I wanted was to:

  • Always be able to see the revision number, even if I build locally without Hudson.
  • Not change version controlled source code.

My approach was to let ant find out the latest subversion revision number for my project, add that number to a file, and let JSF display the information from that file into the web-GUI. My only drawback with this solution is that I have to edit the file for every new release number, i.e. if I go from 2.0.4 to 2.0.5, I have to edit that part of the version number myself.

Create a text file to hold revision information

You need a file somewhere in your web application that holds your revision information that you want to display. But this file cannot be controlled by subversion because it will be changed everytime you build your system with ant. If the file was version controlled, as soon as ant changed it, subversion would want you to submit it.

The design is instead to check in a template file that contains the text you want to display and a tag. Ant will take this template and create a new file, substitute the tag with the revision info, and place the file somewhere within your web application so that it will be included in the war-file.

I created a file called revision_template.txt that I checked in. It contains the following information:

Version 2.0.4.REVISION

”REVISION” is the tag that will be substituted. In my case, for each release that I will create of the application, I need to update this file with the base revision information, e.g when the application goes from 2.0.4 to 2.0.5.

Subversion commands from ant

First off, you need subversion commands in ant. This is solved by SvnAnt from Tigris. SvnAnt gives you the possibility to add a svn ant-task in your ant targets, thus providing svn commands.

Download the SvnAnt libraries and place them in your directory for third party libraries that should not be shipped with your web application, i.e. not WEB-INF/lib.

In you build.xml, set up the path to the libraries.

<!-- Making SvnAnt available -->
 <path id="svnant.classpath">
   <pathelement location="${3pp}/svnant.jar"/>
   <pathelement location="${3pp}/svnClientAdapter.jar"/>
   <pathelement location="${3pp}/svnkit.jar"/>
   <pathelement location="${3pp}/svnjavahl.jar"/>

Create a new target (or modify an existing if you prefer) that finds out the latest revision number and inserts that into a file that will be read by JSF later on. Make sure you add your user and password to access subversion. The target must be run before any packaging of your web application takes place since it will create a new file that must be shipped with your war-file. I let my init-target depend on this new target.

<target name="set-revision">
  <typedef resource="org/tigris/subversion/svnant/svnantlib.xml" classpathref="svnant.classpath" />
  <svn username="user" password="password" javahl="false">
    <info target="." />

  <copy overwrite="true" file="${web}/view/revision_template.txt" tofile="${web}/view/revision.txt" />
  <replace file="${web}/view/revision.txt">
    <replacefilter token="REVISION" value="${}" />
  </replace >

The typedef makes the svn ant-task available. Then ant logs in to svn and runs the info command on ”.”. This sets the variable to the latest revision number.

Then ant takes the template file, copies it to a new file called revision.txt, and substitutes ”REVISION” to the value of the variable.

Making the revision text visible

In the JSF-file where you want to show your text, insert the following:

<rich:insert src="/view/revision.txt"/>

This will simply display the text in the file. Make sure your path is correct. See more about the rich:insert tag here.

If you want to apply styling to the text, surround it with a h:outputText.

Summing up

Showing off your application’s revision information is a neat feature that, especially during intense test/release cycles, will be very useful, as you will always be able to trace back to exactly what version of the source code that is included in a particular release.

Posted in Programming, Tools.

Tagged with , , , , , .

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Some HTML is OK

or, reply to this post via trackback.