Subversion Revision Number in NAnt

Presenting the holy grail of NAnt and Subversion driven build scripts, this target gets the current subversion revision number from the directory in the “src.dir” property:

<target name="getSubversionRevision">
    <!-- Retrieve subversion revision number -->
    <echo message="Retrieving Subversion revision number"/>
    <property name="svn.revision" value="0"/>
    <exec
        program="svn"
        commandline='log "${src.dir}" --xml --limit 1'
        output="${src.dir}\_revision.xml"
        failonerror="false"/>
    <xmlpeek
        file="${src.dir}\_revision.xml"
        xpath="/log/logentry/@revision"
        property="svn.revision"
        failonerror="false"/>
    <echo message="Using Subversion revision number: ${svn.revision}"/>
</target>

Shamelessly stolen from Jonathan Malek.

5 comments

  1. I have tried to use this for a project that uses svn+ssh. When I run svn log I get a “svn: Can’t create tunnel: The system cannot find the file specified.” error. Do you know what might be happening here?

    Thanks.

  2. I think you need to set subversion to use a ssh program like plink for this to work, it sounds like this is set up but subversion can’t find the program. Not sure how to help from there, sorry!

  3. With this script you do not obtain the LAST HEAD revision,
    if you want the HEAD revision you must use the following:

  4. WordPress cuts XML code

    !– Retrieve subversion revision number –>

  5. !– Retrieve subversion revision number –>
    echo message=”Retrieving Subversion revision number”/>
    property name=”svn.revision” value=”0″/>
    exec
    program=”svn”
    commandline=’info –revision HEAD –xml’
    output=”${src.dir}\_revision.xml”
    failonerror=”false”/>
    xmlpeek
    file=”${src.dir}\_revision.xml”
    xpath=”/info/entry/commit/@revision”
    property=”svn.revision”
    failonerror=”false”/>
    echo message=”Using Subversion revision number: ${svn.revision}”/>