Apache Ant Types

Apache Ant provides rich set of types, some of them are given below. We can use these to deal with data, files, path etc and can also be used as service.

TypeDescription
ClassFileSetIt is used to create Jar with all required classes.
DirSetIt groups the directories.
FileListA list of files.
FileSetA group of files.
FileMapperIt helps to map source and target file.
FilterSetA group of filters.
PatternSetA group of patterns that are referenced by ids.
SelectorsA FileSet element, helps to select elements
TarFileSetIt is a special form of a FileSet.
ZipFileSetA set of zip files.

ClassFileSet

ClassFileSet is a special type of FileSet which includes all of the class files upon which the root classes are depend. It is used to create JAR with all the required classes for a particular application.

ClassFileSet are declared by the id value which is then used as reference.

DirSet

A DirSet is a group of directories. It supports <patternset> and it's nested elements <include>, <includesfile>, <exclude>, <excludefiles>. It has various attributes which are given below.

AttributeDescriptionRequired
dirIt holds root of the directory tree of this DirSetYes
includesA list of patterns of directories.No
includesfileName of a file which is to be included.No
excludesA list of patterns of directories that must be excluded.No
excludesfileName of a file which is to be excluded.No
casesensitiveIt is used to specify whether case-sensitivity should be applied or not.No
follosymlinksIt holds a symbolic link to be followed.No
erroronmissingdirIt specifies what happens if the base directory does not exist.No

Apache Ant DirSet Example

This example will group all the directories names classess found inside the apps subdirectory of ${build.dir} and excludes which has test in their name.

FileList

FileList is a list (collection) of files. It can be be used with <concat>, <dependset> tasks. FileList is useful for specifying files that may or may not exists. It has attributes given below.

AttributeDescriptionRequired
dirIt is used to hold base directory of this list.Yes
filesList of file namesYes

FileList Example

FileSet

FileSet is a group of files from a single root directory. It collects files based on patternsets specified by Ant. The <include> and <exclude> elements are called patternsets. A patternset is a collection of file matching patterns. By default these are casesensitive, but can be disabled by setting casesensitive = false.

File Set has attributes which are given below.

AttributeDescriptionRequired
dirIt holds root directory the FileSet.Either dir or file must be specified.
fileFor specifying a single-file fileset.Either dir or file must be specified.
includesA list of patterns of files that must be included.No
includesfileA file name.No
excludesA list of patterns of files that must be excluded.No
excludesfilesA File name pattern.No
casesensitiveBoth the include and exclude patterns are casesensitive.No
followsymlinksThe symbolic links should be followed.No

Lets see an example and copy files from one directory to another.

FileSet Example

// build.xml

The above code will copy all the java files from dir2 to dir1.

FileMapper

Sometimes after executing a task, the source file and the output both are different and then not be mapped. Apache Ant uses FileMapper to map source and created target file. An instance of org.apache.tools.ant.util.FileNameMapper class and <mapper> element is used to map the files.

The mapper has the following attributes.

AttributeDescriptionRequired
typespecifies one of the built-in implementations.No
classnamespecifies the implementation by class name.Exactly one of these.
classpaththe classpath to use when looking up classname.No
classpathrefApache Ant? User ManualNo
fromthe from attribute for the given implementation.Depends on implementation
tothe to attribute for the given implementation.Depends on implementation

All built-in mappers are case-sensitivity.

Mapper Example

FilterSet

FilterSet is a group of filters and used by two major operations (tasks) copy and move. It uses id and refid attributes, id is used to define a FilterSet and refid is used to refer another FilterSet.

It is also possible to nest FilterSet into FilterSet to get a set union of the contained filters.

It has various attributes which are given below.

Fileset Attributes

AttributeDescriptionDefaultRequired
begintokenIt is a string marking the beginning of a token.@No
endtokenIt is a string marking the end of a token.@No
filtersfileIt specifies a single filtersfile.NoneNo
recurseUsed for searched more tokens.trueNo
onmissingfiltersfileIt Indicates behavior when a nonexistent filtersfile is specified.failNo

Here, we are copying the abc.txt file to the dist directory from the build directory but wish to replace the token @DATE@ with today's date.

FilterSet Example





Latest Courses