Cari di Apache Ant 
    Apache Ant User Manual
Daftar Isi
(Sebelumnya) Description TypeDirSet (Berikutnya)
Concepts and Types - Types

Directory-based Tasks

Directory-based Tasks

Some tasks use directory trees for the actions they perform.For example, the javac task, whichcompiles a directory tree with .java files into.class files, is one of these directory-based tasks. Becausesome of these tasks do so much work with a directory tree, the task itselfcan act as an implicit FileSet.

Whether the fileset is implicit or not, it can often be very useful towork on a subset of the directory tree. This section describes how you canselect a subset of such a directory tree when using one of thesedirectory-based tasks.

Apache Ant gives you two ways to create a subset of files in a fileset, both ofwhich can be used at the same time:

  • Only include files and directories that match any include patterns and do not match any exclude patterns in a given PatternSet.
  • Select files based on selection criteria defined by a collection of selector nested elements.

Patternset

We said that Directory-based tasks can sometimes act as an implicit<fileset>,but in addition to that, a FileSet acts as an implicit<patternset>.

The inclusion and exclusion elements of the implicit PatternSet can bespecified inside the directory-based task (or explicit fileset) viaeither:

  • the attributes includes and excludes.
  • nested elements <include> and <exclude>.
  • external files specified with the attributes includesfile and excludesfile.
  • external files specified with the nested elements <includesfile> and <excludesfile>.

When dealing with an external file, each line of the fileis taken as a pattern that is added to the list of include or excludepatterns.

When both inclusion and exclusion are used, only files/directories thatmatch at least one of the include patterns and don't match any of theexclude patterns are used. If no include pattern is given, all filesare assumed to match the include pattern (with the possible exception ofthe default excludes).

Patterns

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

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

In general, patterns are considered relative paths, relative to atask dependent base directory (the dir attribute in the case of<fileset>). Only files found below that basedirectory are considered. So while a pattern like../foo.java is possible, it will not match anything whenapplied since the base directory's parent is never scanned forfiles.

Examples:

*.java  matches  .java,x.java and FooBar.java, butnot 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 inthe pattern is matched against the first directory in the path to match. Thenthe second directory is matched, and so on. For example, when we have the pattern/?abc/*/*.javaand 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 itpossible to match multiple directory levels. This can be used to match acomplete 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 adirectory 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 endswith /or , then **is appended.For example, mypackage/test/ is interpreted as if it weremypackage/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 powerfulway to select just the files you want.

Selectors

The <fileset>,whether implicit or explicit in thedirectory-based task, also acts as an<and>selector container. This can be used to create arbitrarily complicatedselection criteria for the files the task should work with. See theSelector documentation for moreinformation.

Standard Tasks/Filesets

Many of the standard tasks in ant take one or more filesets which followthe rules given here. This list, a subset of those, is a list of standard anttasks that can act as an implicit fileset:

Examples

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

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

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

The same as the example above, but expressed using nested elements.

<delete dir="${dist}"> <include name="**/images/*"/> <exclude name="**/*.gif"/></delete>

Deleting the original set of files, the delete task can actas an implicit fileset.

Default Excludes

There are a set of definitions that are excluded by default from alldirectory-based tasks. As of Ant 1.8.1 they are:

 **/*~ **/#*# **/.#* **/%*% **/._* **/CVS **/CVS/** **/.cvsignore **/SCCS **/SCCS/** **/vssver.scc **/.svn **/.svn/** **/.DS_Store

Ant 1.8.2 adds the folllowing default excludes:

 **/.git **/.git/** **/.gitattributes **/.gitignore **/.gitmodules **/.hg **/.hg/** **/.hgignore **/.hgsub **/.hgsubstate **/.hgtags **/.bzr **/.bzr/** **/.bzrignore

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

This is the default list; note that you can modify the list ofdefault excludes by using the defaultexcludes task.

(Sebelumnya) Description TypeDirSet (Berikutnya)