Simulating rsync in ant

When writing Cocoon apps I often include an rsync setup in my ant build scripts, to keep the src and webapp directories synchronized during development.

As Cocoon reloads most stuff (even java classes with the proper setup in 2.2) when files are modified, this is very useful.

But some people with whom I work still insist on developing on a platform which does not provide the sync command out of the box (I’m not telling which ;-) , meaning that we have to keep cross-platforms versions of rsync.

So I was looking for a way to simulate an rsync loop with ant’s <copy> task.

Here it is – note the trick to get an infinite loop. I’m not an ant expert, so flames are welcome ;-)

rsync the source and webapp directories, allows changes to non-java
code (sitemaps, XSLT, etc.) to be propagated to the webapp directory
without having to stop Cocoon
<target name="rsync">
<!-- lower this thread's priority as much as possible -->
<!-- note that the nice task requires ant 1.6.5 -->
<nice newpriority="1"/>

<copy todir="${tools}/cocoon/webapp">
<fileset dir="${src}/cocoon/webapp"/>
<sleep seconds="1"/>

A bit hacky - call another target which calls this one, to create
an infinite loop.
Is there a risk of filling up the stack? This is for development
sessions anyway, so nothing critical.
<antcall target="rsync2"/>

<!-- used to hack an infinite loop for the rsync target -->
<target name="rsync2">
<antcall target="rsync"/>

The thing is…this is much slower than native rsync, so there’s room for improvement.

4 Responses to Simulating rsync in ant

  1. Why not simply use Ant’s built-in sync task?

  2. sync probably isn’t any better than copy in your case since it would first copy all newer files (just like copy, at the same speed) and after that delete all files from the target that are not present in the source.

    You don’t seem to need the delete step and it would only slow down things.

    You can make the build file arbitrarily complex by probing for rsync and using exec instead of copy if it is there.

    There is no built-in way for an endless loop other than what you’ve done, but you could use one of Ant-Contrib’s loop tasks or write one (using beanshell or javascript or whatever) yourself if you really need one.

    OTOH, rsync doesn’t give you the endless loop either, does it?

  3. Thanks for the clarifications! You’re right that rsync per se doesn’t provide the loop, what I run when it’s done outside of ant is a shell script which includes a loop.

  4. Why don’t you used a CVS or CVN script that fires when you check something in. I am in the habit of checking in every change and tagging the good ones for the over night build.

    This has saved me (and my company) when my laptop was stolen. We bought a new machine continued to develop.


%d bloggers like this: