3

I would like to use Ant to get the username of the latest commit of a svn repository.

how can i do this as cross-platform as possible?

thanks!

clamp
  • 33,000
  • 75
  • 203
  • 299

4 Answers4

7

So, here is example for "svnant":

<path id="devlibs.path">
    <fileset dir="devlib" includes="**/*.jar" />
</path>

<typedef resource="org/tigris/subversion/svnant/svnantlib.xml" classpathref="devlibs.path" />

<svnSetting
        id="svn.settings"
        svnkit="false"
        javahl="false"
        username="${svn.username}"
        password="${svn.password}" />

<target name="get-svn-author">
    <svn refid="svn.settings">
        <singleinfo target="${svn.repo.url}" property="author.result" request="author" />
    </svn>

    <echo message="Author of last commit: ${author.result}" />
</target>

You should put "svnant.jar" and "svnClientAdapter.jar" to path identified by "devlibs.path". And other libraries required by "javahl" or "svnkit" to your "${ANT_HOME}/lib".

If you have SVN repository with 1.6 format, you could use "svnkit" (set to "true" in "svnSetting"-block). Because "svnkit" is a pure Java-toolkit it's best to use when you need cross-platform solution. If you already switched to 1.7, then I recommend to use SVN command line client ("svnSetting" configured for command line client in my example). Check this page (Slik SVN) for info about where to find and how to install different SVN command line clients.

Vadim Ponomarev
  • 1,346
  • 9
  • 15
  • This answer is also a good example of how to use SvnAnt's svnSetting since the parameters username, password, javahl, svnkit, etc. are now deprecated for the [svn](http://subclipse.tigris.org/svnant/svntask.html) task. – Jesse Jan 07 '14 at 21:09
7

Just in case you prefer a solution that does not use optional tasks, here's the gist of how you can do it:

<target name="get-svn-author">
    <tempfile property="svninfo.result"/>
    <exec executable="svn" output="${svninfo.result}">
        <arg value="info"/>
        <arg value="${svn.repo.url}"/>
    </exec>
    <tempfile property="svnauthor.result"/>
    <copy file="${svninfo.result}" tofile="${svnauthor.result}">
        <filterchain>
            <linecontainsregexp>
                <regexp pattern="Last Changed Author:*"/>
            </linecontainsregexp>
        </filterchain>
    </copy>
    <concat>
        <filelist files="${svnauthor.result}"/>
    </concat>
    <delete file="${svninfo.result}"/>
    <delete file="${svnauthor.result}"/>
</target>

One catch is that you write/delete files which may not always be possible (i.e. if you do not have permissions). Another catch is that an svn client (that can be run via svn command from the command line) must be installed.

malenkiy_scot
  • 16,415
  • 6
  • 64
  • 87
  • not related to your answer but can a `filset` dir= be linked to svn repository not to local folder ? for example in this i want the dir to be on svn server is that possible – Moudiz Jun 22 '17 at 12:10
1

I thought I had posted an answer. This is more or less malenkiy_scot's answer. However, I use the --xml flag on the svn info command. This put the output of the svn info command in xml output. I was then able to use the <xmlproperty> task to read it.

By using resource collections, I also avoided the problem of saving the output into a file which has to be deleted later on.

<project>
    <property name="svn.url" value="http://svn.foo.com/src/repos"/>
    <exec executable="svn"
        outputproperty="svn.info">
        <arg value="info"/>
        <arg value="--xml"/>
        <arg value="${svn.url}"/>
    </exec>
    <xmlproperty prefix="svn">
        <propertyresource name="svn.info"/>
    </xmlproperty>
    <echo message="Last committer = ${svn.info.entry.commit.author}"/>
</project>

I tried to see if I could get away with using the <exec> task itself as a resource. That way, I wouldn't even have to save the output of the svn info command to a property. But alas, I couldn't figure out how to do that.


The most platform neutral way is Vadim Ponomarev's answer. This was the way I had intended to answer this question.

However, I would have included the svnkit jar file in the devlib directory, and forced the use of svnkit. This makes sure it works no matter which Subversion client is installed. It will even work even if there is no Subversion client installed. Just keep the required jars inside a directory in the project.

Community
  • 1
  • 1
David W.
  • 105,218
  • 39
  • 216
  • 337
0

There is already a good solution within a executable shell. That solution has the advantage that you can do it as part of the build phase using scripting and not in the post-build phase.That is explained below. See https://stackoverflow.com/a/11837662/5842403

In fact, you can access to the information before the build phase finish by reading/parsing the ../builds/$BUILD_NUMBER/changelog.xml file inside the build folder. This file is created with the SVN commit triggering and not with the end of the build or post_build phase. That means, you can parse it at the start of the build phase of the same job with a script and insert the data in env variables.

Community
  • 1
  • 1
Joniale
  • 515
  • 4
  • 17