How to get the username of the last svn commit using ant?

笑着哭i 提交于 2019-12-12 17:13:51

问题


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!


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/10070871/how-to-get-the-username-of-the-last-svn-commit-using-ant

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!