Ant Best Practices: Define and Reuse Paths

Last time, we discussed dependencies. Today's installment of Ant Best Practices is purportedly about resusing filesets by reference ID, but it's really another way to avoid duplication. The title of Eric's original article is 'Define and Reuse Paths'. The same advice works for paths, filesets, and filtersets. Here's an example:

<project name="OnceAndOnlyOnce">
  <fileset id="output_files" dir="${build.dir}">
  <include name="duplicato-*.jar" />
  </fileset>
<target name="copy_stuff">
    <copy todir="${special.shiny.dir}">
      <fileset refid="output_files" />
    </copy>
  </target>
</project>

You can see that any other use of the fileset will be able to refer to it by ID, rather than explicitly declare it again. One of the scariest builds that I ever saw was 4,000 lines of XML in a single file. One of my erstwhile colleagues spent a morning going through it and replacing filterset declarations with references to one filterset. It's such a useful feature to have in a build tool, and a fine piece of advice to give to someone who just started out writing build scripts.

The other point from the original article is that you need to use this technique alongside some modular approach to handling paths. You just can't really get away with having a path named 'standard.jars.classpath.without.selenium' (and yes, that's almost directly lifted from the build of a large Java project). You're much better off trying to split things like classpaths into categories like build-time and runtime. That's something I touched on in my article for the ThoughtWorks Anthology. I'm thinking of expanding on it in a future post. You like that idea? Let me know.

DevOps New Zealand