Hello there!
I am trying to get svn info from Ant. I think there are two ways.
I need to pick up proper version of JavaHL based on Subversion client version installed on machine.
Since I would like to shared the ant script between Windows and Linux box, it's a bit pain for me to switch the JavaHL in my Ant script.
So I decided to go for second option. This method is really straight forward but a bit hack.
The advantage of this method is it works at least svn command is available on your machine and it doesn't depend on svn client version. The disadvantage is it really depends on svn info output string e.g. it's design for human readability, not designed for API.
Anyway, here is the example for getting svn revision and set it to build.current.revision property. What this target do is redirecting svn info output and extracting revision line using regex.
Here is an another example. Get branch, tag, or trunk name (without root url) of the current project. It is useful for selecting deploy version automatically based on the project you checked out.
- SvnAnt
- Execute SVN command and extract information on Ant
I need to pick up proper version of JavaHL based on Subversion client version installed on machine.
Since I would like to shared the ant script between Windows and Linux box, it's a bit pain for me to switch the JavaHL in my Ant script.
So I decided to go for second option. This method is really straight forward but a bit hack.
The advantage of this method is it works at least svn command is available on your machine and it doesn't depend on svn client version. The disadvantage is it really depends on svn info output string e.g. it's design for human readability, not designed for API.
Anyway, here is the example for getting svn revision and set it to build.current.revision property. What this target do is redirecting svn info output and extracting revision line using regex.
<target name="get-svn-revision" description="Get revision via svnversion command"> <exec outputproperty="build.current.revision" executable="svnversion" dir="${basedir}"> <arg line="-n -c" /> <redirector> <outputfilterchain> <tokenfilter> <replaceregex pattern="^[0-9]*:?" replace="" flags="g" /> </tokenfilter> </outputfilterchain> </redirector> </exec> <echo message="Current Revision ${build.current.revision}" /> </target>You might notice the line:
< arg line="-n -c" />This line is for disabling language translation of svn output, otherwise regex pattern is unable to match the proper line.
Here is an another example. Get branch, tag, or trunk name (without root url) of the current project. It is useful for selecting deploy version automatically based on the project you checked out.
<target name="set-version-from-svn"> <exec outputproperty="svn.url" executable="svn" dir="${basedir}"> <env key="LANG" value="C" /> <env key="LC_MESSAGES" value="C" /> <arg line="info" /> <redirector> <outputfilterchain> <linecontainsregexp> <regexp pattern="^URL:" /> </linecontainsregexp> <tokenfilter> <replaceregex pattern="^URL:\s*(.+)$" replace="\1" flags="g" /> </tokenfilter> </outputfilterchain> </redirector> </exec> <echo message="SVN URL : ${svn.url}" /> <exec outputproperty="svn.repository.root" executable="svn" dir="${basedir}"> <env key="LANG" value="C" /> <env key="LC_MESSAGES" value="C" /> <arg line="info" /> <redirector> <outputfilterchain> <linecontainsregexp> <regexp pattern="^Repository\sRoot:" /> </linecontainsregexp> <tokenfilter> <replaceregex pattern="^Repository\sRoot:\s*(.+)$" replace="\1" flags="g" /> </tokenfilter> </outputfilterchain> </redirector> </exec> <!-- this line is just for safe: check if root url is available or not --> <condition property="svn.repository.root.unavailable"> <equals arg1="${svn.repository.root}" arg2="" trim="true" /> </condition> <fail message="Could not get SVN Repo Root" if="${svn.repository.root.unavailable}" /> <echo message="SVN Repo Root: ${svn.repository.root}" /> <!-- Example: Input: svn.repository.root=https://dukesoftutilsja.svn.sourceforge.net/svnroot svn.url=https://dukesoftutilsja.svn.sourceforge.net/svnroot/dukesoftutilsja/trunk Result: svn.version.to.deploy=trunk --> <propertyregex property="svn.version.to.deploy" input="${svn.url}" regexp="${svn.repository.root}/.+?/(.+)" select="\1" casesensitive="false" /> </target>You can do more hack extracting timestamp?
コメント