Adding The Subversion Build Number To Your Application Using Ant

02 Aug 2005

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):

``` ...

    <exec executable="svnversion" spawn="false" dir="${project.build.root}" output="${buildversion.outputfile}">
        <arg line="."/>
    </exec>
    <replaceregexp file="${buildversion.outputfile}"
        match="([0-9]*)"
        replace="<cfset Request.Event.setArg('buildnum', ) />"
        byline="false"/>
</target>

```

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

  1. Using the [svnversion](http://svnbook.red-bean.com/en/1.1/re57.html) 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](http://ant.apache.org/manual/OptionalTasks/replaceregexp.html) 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:

```

<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"))>
</cffunction>

```

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. :)