ant maven subversion

Using svnversion from Ant

Posted by Cameron Stokes on December 12, 2009
software development, technical / No Comments

At work we use a combination of Subversion (svn), Maven, and Ant to build and deploy our applications and integrate the svn revision number into our deployment packages.  As an example, if we’re packaging our callcenter application our build scripts will create a callcenter-7175.jar package.  This makes it easy for us to upgrade and rollback between versions as needed.

The svnversion program makes it easy to get the revision number of your working copy by simply executing the program:

macbookpro:rel-091105 stokesc$ svnversion
7175

To use this from Ant we can use the exec task. The example below will execute svnversion and capture its output into the Ant property repository.revision which is used later on in our build script:

<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="Build Script" default="make" basedir=".">

    <target name="make">

        <!-- Get current working directory. -->
        <exec executable="pwd" outputproperty="dir.root" />

        <!-- Get subversion revision number. -->
        <exec executable="svnversion" outputproperty="repository.revision" />

        <echo message="Repository revision is ${repository.revision}" />

    </target>

</project>

This worked well for us for a few years but when we needed to build from an older branch the revision number wasn’t representative of the last revision in the branch.  Looking at the options for svnversion I found passing -c will return the revision number of the last change rather than the current revision which is what we really wanted.

macbookpro:rel-091105 stokesc$ svnversion -c
1:6985

You can see the difference in revision numbers from this command versus the one above, but this still isn’t perfect due to the starting revision number that’s been added.  A little change to our Ant script can strip this off for us.  Here we use the redirector and filterchain types and a regular expression to modify the output.

<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="Build Script" default="make" basedir=".">

    <target name="make">

        <!-- Get current working directory. -->
        <exec executable="pwd" outputproperty="dir.root" />

        <!-- Get subversion revision number. -->
        <exec executable="svnversion" outputproperty="repository.revision">
            <!-- Specify '-c' to get last changed rather than current revisions. -->
            <arg value="-c" />
            <!-- Use redirector/filterchain to parse output.
                 svnversion -c will return output in format [initial]:[current]
                 and we want to strip off [initial]: -->
            <redirector>
                <outputfilterchain>
                    <tokenfilter>
                        <replaceregex pattern="[0-9]+\:" replace="" />
                    </tokenfilter>
                </outputfilterchain>
            </redirector>
        </exec>

        <echo message="Repository revision is ${repository.revision}" />

    </target>

</project>

We’re now able to go back and package older versions of our application with a true indication of their svn revision.

Tags: , , , ,