I'm Jonathan Bullock; creator of JBake, developer with a passion for the Internet, geek, gadget lover and fan of far too many sports. I occasionally give talks and tweet every now and then.
26 September 2010
I use Subversion for my personal projects and web sites and for one of my projects I wanted to use the SVN revision value as the version number for the project. As it's a Java based project and I'm using an Ant script to package & deploy it I wanted to retrieve the repository revision in the Ant script and automatically set the version value in the configuration file. After a bit of searching I found this article on how to get the revision value via Ant, all it requires is that the command line version of Subversion is installed.
So I added the following target to my Ant script to retrieve the last changed revision (slightly modified from the original):
<target name="svnrevision"> <exec executable="svnversion" outputproperty="repository.revision"> <arg value="-c" /> <redirector> <outputfilterchain> <tokenfilter> <replaceregex pattern="[0-9]+\:" replace="" /> <replaceregex pattern="[M]" replace="" /> </tokenfilter> </outputfilterchain> </redirector> </exec> <echo message="Current revision is ${repository.revision}" /> </target>
Then later on in my Ant script I use the extracted variable to set the version value in the version.properties file while copying it:
<copy file="./src/version.properties" tofile="./deploy/version.properties"> <filterset> <filter token="version" value="${repository.revision}" /> </filterset> </copy>
The version.properties file simply has the following line in it:
version=@version@
And that's it.