How to get svn revision in ant using svn or svnversion
This article is aimed to you whom use svn in linux environment. If you are using windows and Tortoise this post may be more interesting.
Here are two ways to get revision number when you build your project. The first one is a complete method to get this number and the second one will give you this number including a trailing if there is (see). In both examples I try to get the current revision number of Zend Framework …
Using svn
In this case we use “svn info” and put the data into xml file svninfo.xml. After that we load in the data using xmlproperty and select the revision number. Your target would look like this
<target name="get-revision"> <exec executable="svn" output="svninfo.xml"> <arg line="info --xml Zend" /> </exec> <xmlproperty file="svninfo.xml" collapseattributes="true" /> <property name="svn.revision" value="${info.entry.revision}" /> <echo>${svn.revision}</echo> </target>
And after running this target you get an output as bellow and the echo statement should give you 17746
<?xml version="1.0"?> <info> <entry kind="dir" path="Zend" revision="17746"> <url> http://framework.zend.com/svn/... ...framework/standard/trunk/library/Zend </url> <repository> <root>http://framework.zend.com/svn/framework</root> <uuid>****</uuid> </repository> <wc-info> <schedule>normal</schedule> <depth>infinity</depth> </wc-info> <commit revision="17740"> <author>****</author> <date>2009-08-22T02:53:32.295192Z</date> </commit> </entry> </info>
Using svnversion
This case is really simple and you don’t need an output file. However, the result may contain none numeric character and you need to convert it if you do need an integer. The easiest way is to parse this as an int in your application before you start using it. In this example the echo statement should print out something like 17746M
<target name="get-version"> <exec outputproperty="svn.revision" executable="svnversion" dir="Zend" /> <echo>${svn.revision}</echo> </target>