Ant Best Practices: Use ZipFileSet

Ant Best Practices: Use ZipFileSet

(image taken from the superbly named MasochismTango’s photostream)


Welcome to the lucky thirteenth edition of Ant Best Practices. You probably guessed this one: Use the ZipFileSet type when you make a zip file in Ant.

This one slipped past me recently. We were working on a web project and the developers added a cache-busting feature to stop CSS stylesheets being cached by the reader’s browser. So they wrote build targets to:

  • fetch the static content
  • lay it out in a directory, nested under an arbitrary kind of key
  • zip it all up for deployment later

It looked something like this:

<project name="web" default="zipfile">
  <property name="build.dir" value="build" />
  <property name="tmp" value="${build.dir}/tmp" />
  <target name="zipfile">
    <copy todir="${tmp}/static/random_token">
      <fileset dir="code" />
    </copy>
<!-- more static files, you getthe idea -->
    <zip file="${build.dir}/static.zip">
      <fileset dir="${tmp}" />
    </zip>
  </target>
</project>


Sounds fine, right?

Hmm. Actually, no. The approach gets top marks for actually working, but where I should have intervened was the copying of the files about to make the paths that were desired. In this case, the <zipfileset> lets you pluck files from wherever they might be, and put them into the right place:

<project name="web" default="zipfile">
  <property name="build.dir" value="build" />
  <target name="zipfile">
  <zip file="${build.dir}/static.zip">
    <zipfileset prefix="random_token" dir="code"/>
  </zip>
  </target>
</project>


Really, that’s it. I was originally quite surprised that it made Eric’s original list of practices: It’s quite a simple change to make. But on writing about it I’m thinking this should be a refactoring. It’d be great to invoke an automated refactoring and introduce a zipfileset whenever you saw tedious copying and zipping operations. Anyway. Do this, and it will make your build faster and easy to read. Result.

Tagged

One thought on “Ant Best Practices: Use ZipFileSet

  1. […] In 2003, Eric M. Burke published an article at OnJava called Top 15 Ant Best Practices. Are the practices still relevant? What has changed in the last 4 years? In this series I’ll be reviewing each one and trying to find out. 1 of 15: Adopt consistent style 2 of 15: Put the build file at the root of your project 3 of 15: Prefer a single buildfile 4 of 15: Provide good help 5 of 15: Provide a clean target 6 of 15: Manage dependencies with Ant 7 of 15: Define and reuse paths 8 of 15: Define proper target dependencies 9 of 15: Use properties for configurability 10 of 15: Keep the build process self contained 11 of 15: Use version control 12 of 15: Use Ant as the least common denominator 13 of 15: Use zipfileset […]

Comments are closed.

%d bloggers like this: