Mittal has posted 1 posts at DZone. You can read more from them at their website. View Full User Profile

How to Use SVN Tasks with ANT

12.25.2008
| 98075 views |
  • submit to reddit
This post is about using ANT to perform some of the most common source-control related tasks such as export, tagging, and branching. I am using ANT version 1.7.0 and SVN Ant version 1.1-rc3, bound against Subversion 1.4.0.

The related software can be downloaded here:
  1. SVN Ant = http://subclipse.tigris.org/svnant.html
  2. ANT = http://ant.apache.org/

I shall start with build.properties, which lists a few key/value pairs used in our SVN Ant task build file, referred as svn-tasks.xml:

Content of build.properties:

####START of SVN Properties ####
svn.repository.url=http://xyz.com/repos/somereponame
svn.project.base.path=someprojectname
svn.username=user name to access repo
svn.password=password to access repo
#This shall be name of the tag,
#This property should always be updated before build starts
#This property shall be used to export
tag.name=SOME_TAG_NAME_12222008
#This shall be name of new branch,
#this property should be used only when new branch is to be created
new.branch.name=NEW_BRANCH_12222008
####END of SVN Properties ####
Content of svn-tasks.xml:
<property file="build.properties"></property>

<!-- SVN and SVN-ANT Tasks properties -->
<property name="svn.repository.url" value="${svn.repository.url}"/>
<property name="svn.project.base.path" value="${svn.project.base.path}" />
<property name="svn.base.url" value="${svn.repository.url}/${svn.project.base.path}"/>
<property name="svnant.lib.dir" location="svn-ant-lib"/>
<property name="svnant.javahl" value="false" />
<property name="svnant.svnkit" value="true" />
<!-- SVN and SVN-ANT Tasks properties -->


<!-- *************************************************************** -->
<!-- Set-Up of SVN-ANT classpath -->
<!-- *************************************************************** -->
<path id="svnant.classpath">
<fileset dir="${svnant.lib.dir}">
<include name="**/*.jar" />
</fileset>
</path>

<!-- *************************************************************** -->
<!-- Loading of SVN task -->
<!-- *************************************************************** -->
<typedef resource="org/tigris/subversion/svnant/svnantlib.xml" classpathref="svnant.classpath" />


<!-- *************************************************************** -->
<!-- tool-availability: Determine if SVN-ANT is available. -->
<!-- *************************************************************** -->
<target name="tool-availability">
<available resource="org/tigris/subversion/svnant/svnantlib.xml"
classpathref="svnant.classpath"
property="available.svnant"
/>
<echo message="SVN-ANT is available = ${available.svnant}"/>
</target>


<!-- **************************************************************** -->
<!-- does-svnant-exist: depends on tool-availablility and -->
<!-- displays error message -->
<!-- ***************************************************************** -->
<target name="does-svnant-exist" depends="tool-availability">
<fail unless="available.svnant">
SVN-ANT is not available, cannot perform tagging or checkout/export svn ant task.
</fail>
</target>


<!-- ****************************************************************** -->
<!-- svntag: performs tagging using properties from -->
<!-- build.properties and uses SVNANT tasks -->
<!-- ******************************************************************* -->
<target name="svntag" description="tags individual project using svnant task">
<property name="svn.tag.message" value="Tagging Project ${project.name} with tag name ${tag.name} from trunk "/>
<property name="src.url" value="${svn.base.url}/${project.name}/trunk/"/>
<property name="dest.url" value="${svn.base.url}/${project.name}/tags/${tag.name}"/>

<echo message="${svn.tag.message}"/>
<echo message="${src.url}"/>
<echo message="${dest.url}"/>

<svn javahl="${svnant.javahl}" svnkit="${svnant.svnkit}" username="${svn.username}" password="${svn.password}">
<copy srcUrl="${src.url}" destUrl="${dest.url}" message="${svn.tag.message}"/>
</svn>
</target>


<!-- ****************************************************************** -->
<!-- svnexport: performs export using properties from -->
<!-- build.properties and uses SVNANT tasks -->
<!-- ****************************************************************** -->
<target name="svnexport" description="exports individual project using svnant task">
<property name="svn.tag.message" value="Exporting Project ${project.name} with tag name ${tag.name}"/>
<property name="src.url" value="${svn.base.url}/${project.name}/tags/${tag.name}"/>
<property name="destPath" value="${dest.path}"/>
<echo message="${svn.tag.message}"/>
<svn javahl="${svnant.javahl}" svnkit="${svnant.svnkit}" username="${svn.username}" password="${svn.password}">
<export srcUrl="${src.url}" destPath="${destPath}/${project.name}"/>
</svn>
</target>

<!-- ****************************************************************** -->
<!-- svnbranch: creates a new branch using properties from -->
<!-- build.properties and uses SVNANT tasks -->
<!-- ****************************************************************** -->
<target name="svnbranch" description="creates a new branch for individual project using svnant task">
<property name="svn.branch.message" value="Creating new branch for Project ${project.name} with new branch name ${new.branch.name} from trunk"/>
<property name="src.url" value="${svn.base.url}/${project.name}/trunk/"/>
<property name="dest.url" value="${svn.base.url}/${project.name}/branches/${new.branch.name}"/>

<echo message="${svn.branch.message}"/>
<echo message="${src.url}"/>
<echo message="${dest.url}"/>

<svn javahl="${svnant.javahl}" svnkit="${svnant.svnkit}"
username="${svn.username}" password="${svn.password}">
<copy srcUrl="${src.url}" destUrl="${dest.url}" message="${svn.branch.message}"/>
</svn>
</target>
Published at DZone with permission of its author, Mittal Bhiogade.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Bob Brown replied on Thu, 2008/12/25 - 9:58pm

I have pretty much moved away from ant these days; it's worth giving gant a try: http://gant.codehaus.org/

MUCH nicer. 

 Also check out gradle for more complex build requirements: http://www.gradle.org/

 I don't have any relationship with either project, by the way.

 

Gian Franco Casula replied on Fri, 2008/12/26 - 12:26pm

Thank you for this clear and useful post.

...some of us are still using these excellent tools :-)

Jim Priest replied on Mon, 2008/12/29 - 3:15pm

Excellent post - I added it to my Ant wiki:  http://www.thecrumb.com/wiki/ant

Mittal Bhiogade replied on Mon, 2008/12/29 - 5:52pm in response to: Jim Priest

Sure thing !!!

Chen Fli replied on Mon, 2009/04/27 - 3:09am

That's a great article goes right into my Favorits. Good and clear examples, wish it had more checkin and other options.

Evangeline Raj replied on Mon, 2009/05/04 - 3:41am

Can anyone post the exact Syntax for tagging in svnant?? Thanks In advance. Evan

Mittal Bhiogade replied on Wed, 2009/05/20 - 8:41am

Tagging in SVN is mere copying.

i.e  copy from "trunk" to "tags" or copy from "branches" to "tags"using:

 

<svn javahl="${svnant.javahl}" svnkit="${svnant.svnkit}" username="${svn.username}" password="${svn.password}">
<copy srcUrl="${src.url}" destUrl="${dest.url}" message="${svn.tag.message}"/>
</svn>
 
Hope this helps. 

 

Duo Ji replied on Fri, 2009/06/12 - 10:33am

Hi, I am a co-op student currently working in a small company. I would like to say thank you very much for making  the post, it really helped me and tought me a lot about svnant.However, I did encounter some problem and need more help.

 I am trying to use <mkdir> and <copy>(url-url) in my build file. However, I always got error, even through the task is actually done. I mean either new dir is made or tag is made. The reason I said either is that since there will always be an error, only one task can be done. Another thing I noticed is that instead of "http://svn.....", I had to use "svn://svn....." for it to access the repos. Below is what the error looks like, can anyone help. 

 

      [svn] <Copy> started ...
      [svn] 12/06/2009 11:09:16 org.tmatesoft.svn.core.internal.util.DefaultSVNDebugLogger log
      [svn] SEVERE: WC: org.tigris.subversion.javahl.NotifyInformation.<init>(Ljava/lang/String;IILjava/lang/String;Lorg/tigris/subversion/javahl/Lock;Ljava/lang/String;IIIJLjava/lang/String;Lorg/tigris/subversion/javahl/RevisionRange;)V
      [svn] java.lang.NoSuchMethodError: org.tigris.subversion.javahl.NotifyInformation.<init>(Ljava/lang/String;IILjava/lang/String;Lorg/tigris/subversion/javahl/Lock;Ljava/lang/String;IIIJLjava/lang/String;Lorg/tigris/subversion/javahl/RevisionRange;)V
      [svn]     at org.tigris.subversion.javahl.JavaHLObjectFactory.createNotifyInformation(JavaHLObjectFactory.java:685)
      [svn]     at org.tmatesoft.svn.core.javahl.SVNClientImpl$11.handleEvent(SVNClientImpl.java:1740)
      [svn]     at org.tmatesoft.svn.core.wc.SVNBasicClient.dispatchEvent(SVNBasicClient.java:365)
      [svn]     at org.tmatesoft.svn.core.wc.SVNCopyClient.copyReposToRepos(SVNCopyClient.java:1205)
      [svn]     at org.tmatesoft.svn.core.wc.SVNCopyClient.setupCopy(SVNCopyClient.java:740)
      [svn]     at org.tmatesoft.svn.core.wc.SVNCopyClient.doCopy(SVNCopyClient.java:458)
      [svn]     at org.tmatesoft.svn.core.javahl.SVNClientImpl.copyOrMove(SVNClientImpl.java:797)
      [svn]     at org.tmatesoft.svn.core.javahl.SVNClientImpl.copy(SVNClientImpl.java:724)
      [svn]     at org.tigris.subversion.svnclientadapter.javahl.AbstractJhlClientAdapter.copy(AbstractJhlClientAdapter.java:766)
      [svn]     at org.tigris.subversion.svnclientadapter.javahl.AbstractJhlClientAdapter.copy(AbstractJhlClientAdapter.java:735)
      [svn]     at org.tigris.subversion.svnclientadapter.javahl.AbstractJhlClientAdapter.copy(AbstractJhlClientAdapter.java:722)
      [svn]     at org.tigris.subversion.svnant.commands.Copy.execute(Unknown Source)
      [svn]     at org.tigris.subversion.svnant.commands.SvnCommand.executeCommand(Unknown Source)
      [svn]     at org.tigris.subversion.svnant.SvnTask.execute(Unknown Source)
      [svn]     at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
      [svn]     at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
      [svn]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      [svn]     at java.lang.reflect.Method.invoke(Unknown Source)
      [svn]     at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:105)
      [svn]     at org.apache.tools.ant.Task.perform(Task.java:348)
      [svn]     at org.apache.tools.ant.Target.execute(Target.java:357)
      [svn]     at org.apache.tools.ant.Target.performTasks(Target.java:385)
      [svn]     at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1329)
      [svn]     at org.apache.tools.ant.Project.executeTarget(Project.java:1298)
      [svn]     at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
      [svn]     at org.eclipse.ant.internal.ui.antsupport.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
      [svn]     at org.apache.tools.ant.Project.executeTargets(Project.java:1181)
      [svn]     at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.run(InternalAntRunner.java:423)
      [svn]     at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.main(InternalAntRunner.java:137)
      [svn] svn: Error while dispatching event: org.tigris.subversion.javahl.NotifyInformation.<init>(Ljava/lang/String;IILjava/lang/String;Lorg/tigris/subversion/javahl/Lock;Ljava/lang/String;IIIJLjava/lang/String;Lorg/tigris/subversion/javahl/RevisionRange;)V
      [svn] svn: Error while dispatching event: org.tigris.subversion.javahl.NotifyInformation.<init>(Ljava/lang/String;IILjava/lang/String;Lorg/tigris/subversion/javahl/Lock;Ljava/lang/String;IIIJLjava/lang/String;Lorg/tigris/subversion/javahl/RevisionRange;)V
      [svn] org.tigris.subversion.javahl.NotifyInformation.<init>(Ljava/lang/String;IILjava/lang/String;Lorg/tigris/subversion/javahl/Lock;Ljava/lang/String;IIIJLjava/lang/String;Lorg/tigris/subversion/javahl/RevisionRange;)V
      [svn] <Copy> failed !

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.