Adding The Subversion Build Number To Your Application Using Ant

I have to admit, lately I’m a big, big fan of development using CFEclipse, Subversion, and building Apache Ant scripts to do builds and deployments with for ColdFusion applications. I have quite a few more ideas to add to some existing Ant scripts that I have to do builds, and I’ll keep posting them when I get them flushed-out.

I finally had the opportunity to spend an hour or two on setting appropirate version numbers for various reasons, including being able to roll back to various snapshots of an application, etc. It’s also nice to display the version number publically on a given application somewhere to show clients that yes, you do make incremental changes to an application more frequently than they might think.

Anyhow, to grab the build number of a given Subversion build, you will need two additional things other than just plain Ant. You will need to install the Jakarta ORO package to get one of the optional tasks to work, and you will also need to have the Subversion command line client installed.

The task is really simple really– I’ll just put it here and then explain what it does (pardon the formatting):

<property name="buildversion.outputfile" value="${}\views\version\buildnum.cfm" />
<target name="setBuildVersion" description="Sets the buildversion for the current build">
<exec executable="svnversion" spawn="false" dir="${}" output="${buildversion.outputfile}">
<arg line="."/>
<replaceregexp file="${buildversion.outputfile}"
replace="<cfset Request.Event.setArg('buildnum', \1) />"

Step by step, here is basically what this task does:

1. Using the svnversion command from the Subversion command line client, it grabs the build number from the builds directory where I’ve already deployed a build using a previous task I wrote.

2. The output of said task is piped to a .cfm file in a views folder. So basically the file contents are currently something to the effect of 1390.

3. Using the ant replaceregexp task, we find the build number and assign it as a group, then replace it with a regular expression that transforms it into <cfset Request.Event.setArg('buildnum', 1390) /> in the same file. An important note about the replace attribute in the above regexp, make sure you turn the brackets to & l t; and & g t; (without the spaces of course). Because Ant tasks are formatted in XML (yes, I know you don’t have to do it in XML, but stay with me here), you cannot use brackets– you need to use the HTML entities instead.

So now we have a file which has the build number in it that you can cfinclude. You can find a way to append this into your application anyway you want, but here is a quick and dirty Mach II plug-in I wrote which sets the full version into the Arguments scope:

<cfcomponent hint="Provides a means for setting the current version of the application" extends="MachII.framework.Plugin">
<cffunction name="preEvent" returntype="void" access="public">
<cfset Request.Event.setArg("MajorVersion", 1.0) />
<cfinclude template="/views/version/buildnum.cfm">
<cfset Request.Event.setArg("AppVersion", Request.Event.getArg("MajorVersion") & "." & Request.Event.getArg("BuildNum"))>

I’m sure there are more elegant ways to do that, but it does the job and I didn’t want to spend 800 hours on it. Anyhow, hopefully you find this entry helpful. :)

This entry was posted in Culture, Languages, Tips, Hacks, & Tricks, Tools and tagged , , , , , , , , . Bookmark the permalink.

5 Responses to "Adding The Subversion Build Number To Your Application Using Ant"

Leave a reply