Saturday, January 10, 2009

Making your apps version available - using Apache Ant

One thing I prefer to do with the builds I manage and deploy to live servers and environments is to make sure that the build is able to self report what version and repository revision it was built from. My current place of work uses subversion as its source code management tool. Grabbing the subversion revision number for ant build on your local checkout of a repository is relatively straight forward - all you need is SVNKit. Here is what I do...
<target name="svnrevision" description="Retrieve svn info">
<java classname="org.tmatesoft.svn.cli.svnversion.SVNVersion"
outputproperty="svn.revision">
<classpath>
<fileset dir="${build.lib.dir}" />
</classpath>
<arg path="${basedir}" />
</java>
</target>

That ant target will nab the revision number and stuff it in a property named svn.revision for use later in the ant script. In the event that you are working on an exported code base that is not a local svn repo, your svn.revision property will have a value of "exported", thus keeping the build from breaking if you are building from an export or don't even have an svn client installed. All you need is the svnkit libraries for svnkit.jar and svnkit-cli.jar on your classpath (as I have done by placing them in the directory referenced by the ${build.lib.dir} property).

Once you have the revision number in a property, creating a version string for a properties file , for example, can be done with the propertyfile ant task:
<target name="versionproperties" depends="svnrevision">
<propertyfile file="build/classes/version.properties" >
<entry key="name" value="${project.name}" />
<entry key="version" value="${project.version}" />
<entry key="revision" value="${svn.revision}" />
</propertyfile>
</target>

Which will create a property file at the root of your classpath (assuming that is what is in build/classes) that looks similar to the following:
#Sat Jan 10 06:44:37 EST 2009
version=1.5.1
revision=3827
name=your_project_name


This properties file can easily be slurped up by your app using Java's Properties class and referenced very easily by some servlet or some Swing component to easily provide version reporting.

2 comments:

Jason Whaley said...

Seems blogger.com doesn't have a nice way of handling stuff in <pre> tags to keep it from rolling off of the right margin. I suppose I need one of those javascript syntax highlighters that will embed my paste in a frame. My apologies for the inconvenience.

Jason Whaley said...

And I settled on prettify