An Ant 'exclude classes' example

Problem: You want to build your Java project using Ant but you need to be able to skip certain files -- typically unit test files -- during the compilation or deployment processes.

Solution: You can skip files during the Ant compilation process by using the Ant exclude pattern. Here's an example that shows several exclude patterns in some XML code taken directly from an Ant build script:

<target name="compile" depends="clean">
  <javac destdir="classes" source="1.5" >
    <src path="src"/>
    <exclude name="**/_*.java"/>
    <exclude name="**/Test*.java"/>
    <classpath refid="class.path"/>


In this example, I'm intentionally excluding Java source code files whose names either begin with an underscore or the string Test. These are two file-naming patterns that I use for my unit test classes, and this is a simple way of keeping these test classes from being deployed with the rest of my project.

As a final note, in this example the **/* syntax tells Ant to apply this pattern to all sub-directories.