Directory-based Tasks

Some tasks use directory trees for the task they perform. For example, the javac task, which works upon a directory tree with .java files. Sometimes it can be very useful to work on a subset of that directory tree. This section describes how you can select a subset of such a directory tree.

Ant gives you two ways to create a subset, both of which can be used at the same time:

When both inclusion and exclusion are used, only files/directories that match the include patterns, and don't match the exclude patterns, are used.

Patterns can be specified inside the buildfile via task attributes or nested elements and via external files. Each line of the external file is taken as a pattern that is added to the list of include or exclude patterns.

Patterns

As described earlier, patterns are used for the inclusion and exclusion. These patterns look very much like the patterns used in DOS and UNIX:

'*' matches zero or more characters, '?' matches one character.

Examples:

*.java  matches  .java, x.java and FooBar.java, but not FooBar.xml (does not end with .java).

?.java  matches  x.java, A.java, but not .java or xyz.java (both don't have one character before .java).

Combinations of *'s and ?'s are allowed.

Matching is done per-directory. This means that first the first directory in the pattern is matched against the first directory in the path to match. Then the second directory is matched, and so on. For example, when we have the pattern /?abc/*/*.java and the path /xabc/foobar/test.java, the first ?abc is matched with xabc, then * is matched with foobar, and finally *.java is matched with test.java. They all match, so the path matches the pattern.

To make things a bit more flexible, we add one extra feature, which makes it possible to match multiple directory levels. This can be used to match a complete directory tree, or a file anywhere in the directory tree. To do this, ** must be used as the name of a directory. When ** is used as the name of a directory in the pattern, it matches zero or more directories. For example: /test/** matches all files/directories under /test/, such as /test/x.java, or /test/foo/bar/xyz.html, but not /xyz.xml.

There is one "shorthand" – if a pattern ends with / or \, then ** is appended. For example, mypackage/test/ is interpreted as if it were mypackage/test/**.

Example patterns:

**/CVS/* Matches all files in CVS directories that can be located anywhere in the directory tree.
Matches:
      CVS/Repository
      org/apache/CVS/Entries
      org/apache/jakarta/tools/ant/CVS/Entries
      
But not:
      org/apache/CVS/foo/bar/Entries (foo/bar/ part does not match)
org/apache/jakarta/** Matches all files in the org/apache/jakarta directory tree.
Matches:
      org/apache/jakarta/tools/ant/docs/index.html
      org/apache/jakarta/test.xml
      
But not:
      org/apache/xyz.java
      
(jakarta/ part is missing).
org/apache/**/CVS/* Matches all files in CVS directories that are located anywhere in the directory tree under org/apache.
Matches:
      org/apache/CVS/Entries
      org/apache/jakarta/tools/ant/CVS/Entries
      
But not:
      org/apache/CVS/foo/bar/Entries
      
(foo/bar/ part does not match)
**/test/** Matches all files that have a test element in their path, including test as a filename.

When these patterns are used in inclusion and exclusion, you have a powerful way to select just the files you want.

Examples

  
<copy todir="${dist}">
  <fileset dir="${src}" 
           includes="**/images/*" 
           excludes="**/*.gif" 
  />
</copy>

This copies all files in directories called images that are located in the directory tree defined by ${src} to the destination directory defined by ${dist}, but excludes all *.gif files from the copy.

This example can also be expressed using nested elements:

<copy todir="${dist}">
  <fileset dir="${src}"/>
    <include name="**/images/*"/>
    <exclude name="**/*.gif"/>
  </fileset>
</copy>

Default Excludes

There are a set of definitions that are excluded by default from all directory-based tasks. They are:

     **/*~
     **/#*#
     **/%*%
     **/CVS
     **/CVS/**
     **/.cvsignore

If you do not want these default excludes applied, you may disable them with the defaultexcludes="no" attribute.


Copyright © 2000,2001 Apache Software Foundation. All rights Reserved.