Maven : Retrieve Subversion revision number with Ant

This page last changed on Feb 21, 2006 by Kees de Kooter

This ant task retrieves the revision number of HEAD using the svn.exe commandline tool. This has to be available on the path. Furthermore ant-contrib.jar needs to be present in the classpath. Altogether not a very portable solution...

<target name="find_revision">

    <property name="revision" value="HEAD"/>
    <property name="svn.root" value="svn://traffic01/var/svnroot/trafficits"/>
    <property name="log.dir" location="log"/>
    <property name="release.root" location="c:/var/projects/release"/>
    <!-- find out revision number of HEAD, need svn.exe installed on local machine -->
    <exec executable="svn" outputproperty="svnlog.out">
        <arg line="log ${svn.root} -r ${revision} -q"/>
    <!-- need ant-contrib.jar for this in lib dir of ant install -->
    <taskdef resource="net/sf/antcontrib/"/>
    <propertyregex property="revision.number" input="${svnlog.out}"
        <regexp pattern="r([0-9]*)"/>
    <echo>Revision found: ${revision.number}</echo>


I posted too soon. Here's the version that's working for me.

    <macrodef name="getsvnrevision">
        <attribute name="revision" default="HEAD"/>
        <attribute name="srcUrl"/>
        <attribute name="property"/>

        &lt;tempfile property=&quot;svninfo.log&quot;/&gt;
        &lt;exec executable=&quot;svn&quot; output=&quot;${svninfo.log}&quot;&gt;
            &lt;arg line=&quot;info @{srcUrl}&quot;/&gt;
        &lt;loadfile property=&quot;@{property}&quot; srcFile=&quot;${svninfo.log}&quot;&gt;
                    &lt;regexp pattern=&quot;Last Changed Rev: &quot;/&gt;
                &lt;deletecharacters chars=&quot;Last Changed Rev: &quot;/&gt;
        &lt;delete file=&quot;${svninfo.log}&quot;/&gt;
Posted by at Sep 15, 2006 02:04


Posted by kees at Sep 15, 2006 09:37