Retrieve Subversion revision number with Ant

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"/>
	</exec>
	
	<echo>${svnlog.out}</echo>
	
	<!-- need ant-contrib.jar for this in lib dir of ant install -->
	<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
	
	<propertyregex property="revision.number" input="${svnlog.out}"
		select="\1">
		<regexp pattern="r([0-9]*)"/>
	</propertyregex>
	
	<echo>Revision found: ${revision.number}</echo>
	
</target>
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Sep 15, 2006

    Anonymous says:

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

    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"/>
    
    		<sequential>
    			<tempfile property="svninfo.log"/>
    			<exec executable="svn" output="${svninfo.log}">
    				<arg line="info @{srcUrl}"/>
    			</exec>
    			<loadfile property="@{property}" srcFile="${svninfo.log}">
    				<filterchain>
    					<linecontainsregexp>
    						<regexp pattern="Last Changed Rev: "/>
    					</linecontainsregexp>
    					<deletecharacters chars="Last Changed Rev: "/>
    					<striplinebreaks/>
    				</filterchain>
    			</loadfile>
    			<delete file="${svninfo.log}"/>
    		</sequential>
    	</macrodef>
    
  2. Sep 15, 2006

    Kees de Kooter says:

    Thanks!

    Thanks!