A little Ant tiplet

When putting together an Ant script, it’s common to maintain some sort of hierarchy of “targets” which get invoked in a certain order (by setting dependencies for each). A typical script goes something like this (pseudo-ant!):

target name = "clean"
target name = "build"
target name = "test"
target name = "deploy"

Now, for a clean target, I used to do something like this:

<target name="clean">
  <delete dir="src" />

Simple and to the point eh: get Ant to delete your source directory, ready for the “build” target to, well, re-build it. Super, smashing, great. But if you use source control (and of course, you do) you may run into a whole world of trouble with this approach. For example, one of my clients uses Subversion, and I started to see these sorts of errors when attempting to commit the changes resulting from the build:

Working copy not locked; this is probably a bug, please report
svn: Directory '/Users/SeymourButtz/Workspaces/Eclipse/Naffproject/src/.svn'
containing working copy admin area is missing

Lawks! “Fear not” I said to myself, and popped off to the Ant manual “Delete” page. Once there, I discovered that I simply needed to get down with the kids, and update my Ant skillz. Don’t use the square old delete dir=… directive! Use this badger instead:

<target name="clean">
  <delete includeemptydirs="true">
    <fileset dir="src" defaultexcludes="true" />

Slightly more verbose, but also a lot cleaner (pardon the pun): it wipes out your src directory as before, but having that defaultexcludes flag means the operation preserves any “required” sub-folders, which typically comprise such hidden gems as .svn and .cvs

Hip hip hooray!

(By the way, more on the Subversion error can be found in the FAQ).

Comments on this post are now closed.


I’m a software architect / developer / general IT wrangler specialising in web, mobile web and middleware using things like node.js, Java, C#, PHP, HTML5 and more.

Best described as a simpleton, but kindly. You can read more here.