How to Integrate Subversion, Version Control Software, With Apache ANT

By Angsuman Chakraborty, Gaea News Network
Friday, November 4, 2005

Subversion is a popular version control software and replacement for CVS, the grand-daddy of all version control software which is still widely used in open source community.

Recently I had the necessity to invoke Subversion commands like checkout from Ant, a popular java based build software which replaces make tool.

There are two ant tasks to accomplish this, the better one is SvnAnt from Subclipse project. However configuring it to work was non-trivial as proper usage documentation was missing. So I decided simplify the process to make your life easier.

However the extensive commands it supports are well documented.
Note: If prompted for user name / password use guest / guest.

Why SvnAnt?
Subversion is a full-fledged version control software obviously supporting commands like checkout, add or commit. The ant task from O’Reily didn’t even support checkout! It supported a very limited set from the available commands.
So I went for svnant task which had much better support. Also this way you don’t have to mess with exec’ing the Subversion CLI commands directly from your ant script. It is wrapped up for ease of use. It can also use JNI interface for speed.

To provide access to the Subversion API, svnant uses either the javahl - Subversion Java bindings or Subversion’s command line programs (which must be installed and in your PATH).

javahl uses JNI which must be setup appropriately. I preferred using the Subversion’s command line programs as subversion was already installed in my system.

Steps:
1. I am assuming you already have ant installed. If not do it first. Ensure %ANT_HOME% ($ANT_HOME for *nix) is defined and %ANT_HOME%\bin is appended to your %PATH%.

2. If you do not have subversion installed then first download it and install.

2.1 Ensure that svn is in your path by invoking svn from a command windows (cmd for windows, bash etc. for *nix).

3. Download svnant and extract the files to any directory.

4. Go to your project directory. If it doesn’t have a lib (or equivalent) directory to store required jar files then create one and copy all the files (*.jar ) from svnant’s lib directory:
commons-lang-2.0.jar
jakarta-regexp-1.3.jar
svnClientAdapter.jar
svnant.jar
svnjavahl.jar

5. To use svn task you have to add a taskdef to your build file. Use either of the following:
Option 1:
<taskdef name="svn" classname="org.tigris.subversion.svnant.SvnTask"/>
This requires the lib directory to be included with either “ant -lib lib” or by adding an extra parameter:
<taskdef name="svn" classname="org.tigris.subversion.svnant.SvnTask" classpathref="project.classpath"/>

where project.classpath is previously defined as:

<path id="project.classpath">
    <fileset dir="lib">
        <include name="**/*.jar"/>
      </fileset>
  </path>

Option 2 is simpler:
<taskdef resource="svntask.properties" classpathref="project.classpath"/>
The project.classpath is previously defined as shown before.

For example you can use this simple build.xml file to fetch latest code from WordPress repository:

<project name="WordPress" default="update" basedir=".">
  <path id="project.classpath">
    <fileset dir="lib">
        <include name="**/*.jar"/>
      </fileset>
  </path>
  <taskdef resource="svntask.properties" classpathref="project.classpath"/>
  <target name="update" description="Update WordPress from Subversion Repository" >
    <svn>
      <checkout url="https://svn.automattic.com/wordpress/trunk/" destPath="src" />
    </svn>
  </target>
</project>

Run this script by simply typing ant.

BTW: I think Subversion as version control software and ant as build and deployment software are two very strong products which should be part of any enterprise open source software stack.

Discussion

lagre
May 25, 2010: 7:39 am

Hi,

Under point 5 it says:

Option 1:
This requires the lib directory to be included with either “ant -lib lib” or by adding an extra parameter:
where project.classpath is previously defined as:

What should this classpath be? also Is there an example of a taskdef?

February 17, 2010: 8:37 am

I would like to thank you for the efforts you have made in writing this article. I like your post & I will always be coming frequently to read more of your post.


Kalpana
February 26, 2009: 1:39 pm

Hi,
I tried to checkout via SVN and used svnant. the build.xml in the zip file seems to work well but when i tried to use some other url then it failed. The exception is:
C:\svnant-1.2.1\build.xml:30: Can’t checkout
at org.tigris.subversion.svnant.commands.SvnCommand.executeCommand(Unkn
wn Source)
at org.tigris.subversion.svnant.SvnTask.execute(Unknown Source)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:269)
at org.apache.tools.ant.Task.perform(Task.java:364)
at org.apache.tools.ant.Target.execute(Target.java:301)
at org.apache.tools.ant.Target.performTasks(Target.java:328)
at org.apache.tools.ant.Project.executeTarget(Project.java:1215)
at org.apache.tools.ant.Project.executeTargets(Project.java:1063)
at org.apache.tools.ant.Main.runBuild(Main.java:632)
at org.apache.tools.ant.Main.startAnt(Main.java:183)
at org.apache.tools.ant.Main.start(Main.java:147)
at org.apache.tools.ant.Main.main(Main.java:230)
Caused by: java.lang.NullPointerException
at com.ibm.jsse.bg.setTcpNoDelay(Unknown Source)
at org.tmatesoft.svn.core.internal.util.SVNSocketFactory.createSSLSocke
(SVNSocketFactory.java:67)
at org.tmatesoft.svn.core.internal.io.dav.http.HTTPConnection.connect(H
TPConnection.java:167)
at org.tmatesoft.svn.core.internal.io.dav.http.HTTPConnection.request(H
TPConnection.java:304)
at org.tmatesoft.svn.core.internal.io.dav.http.HTTPConnection.request(H
TPConnection.java:257)
at org.tmatesoft.svn.core.internal.io.dav.http.HTTPConnection.request(H
TPConnection.java:245)
at org.tmatesoft.svn.core.internal.io.dav.DAVConnection.exchangeCapabil
ties(DAVConnection.java:454)
at org.tmatesoft.svn.core.internal.io.dav.DAVConnection.open(DAVConnect
on.java:97)
at org.tmatesoft.svn.core.internal.io.dav.DAVRepository.openConnection(
AVRepository.java:643)
at org.tmatesoft.svn.core.internal.io.dav.DAVRepository.getLatestRevisi
n(DAVRepository.java:172)
at org.tmatesoft.svn.core.wc.SVNBasicClient.getRevisionNumber(SVNBasicC
ient.java:479)
at org.tmatesoft.svn.core.wc.SVNBasicClient.getLocations(SVNBasicClient
java:848)
at org.tmatesoft.svn.core.wc.SVNBasicClient.createRepository(SVNBasicCl
ent.java:531)
at org.tmatesoft.svn.core.wc.SVNUpdateClient.doCheckout(SVNUpdateClient
java:827)
at org.tmatesoft.svn.core.javahl.SVNClientImpl.checkout(SVNClientImpl.j
va:1975)
at org.tigris.subversion.svnclientadapter.javahl.AbstractJhlClientAdapt
r.checkout(AbstractJhlClientAdapter.java:265)
at org.tigris.subversion.svnclientadapter.javahl.AbstractJhlClientAdapt
r.checkout(AbstractJhlClientAdapter.java:242)
at org.tigris.subversion.svnant.commands.Checkout.execute(Unknown Sourc
)
… 12 more


Kalpana
February 26, 2009: 1:32 pm

Hi,
I tried to do the SVN Checkout by following the steps given above but the checkout is not happening and the output says: “Can’t Checkout”.
The build.xml in the svnant works…


Harold Baker
January 9, 2009: 9:53 pm

This article is very interesting. Unfortunately some information is missing under item 5. There is a big blank space after the line:-

“where project.classpath is previously defined as:”

And another big blank space after the line:-

“For example you can use this simple build.xml file to fetch latest code from WordPress repository:”

Could you please complete the missing information or send it to me.

Thanks


Julian
May 20, 2008: 2:59 pm

I find the SVN Ant task very primitive compared to its CVS cousin. Maybe I’m missing something but I couldn’t figure out how to replace the cvs task with its svn alternative. I’m referring to the ‘command’ and ‘package’ attributes which could be specified as build properties and would make the build script pretty generic and parameterized.


Julian
May 20, 2008: 2:48 pm

I find the SVN Ant task very primitive compared to its CVS cousin. Maybe I’m missing something but I couldn’t figure out how to replace this cvs task with its svn alternative:


<cvs dest="${dest}"
command="${command}
tag=”${tag}”
package=”${packages}
output=”${output}”>


Flitt
May 3, 2007: 10:18 am

Ankush, if it is not too late :-)
There is an task for this purpuse in Core taskset. See example:

Flitt


Ankush Chadha
June 9, 2006: 1:09 pm

I am not able to set the interative option in svn, i.e. I have to use username and password option in a non interactive way.

Can anyone tell the format to enable authentication in an interactive way.

Thanks in advance.
Ankush

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :