Apache Ant Tasks

Task is a piece of code that can be executed. A task can have multiple attributes and each task has a common structure. A common structure consists of task-name, attributes etc. See an example below.

Task Structure

A task may comprises of:

Apache Ant tasks are categories into two category.

  • Built-in Tasks
  • User defined Tasks

Apache Ant Predefined (Built-In) Tasks

Tasks those are provided by Apache Ant itself in it's library are known as built-in tasks. Apache ant provides large number of built-in tasks which can be used to execute distinguish tasks. A list is given below.

  • Archive Tasks
  • Audit Tasks
  • Compile Tasks
  • Execution Tasks
  • File Tasks
  • Logging Tasks
  • Mail Tasks

and many more...

Archive Tasks

Tasks which are used to compress and uncompress data are know as archive tasks. Some common built-in archived tasks are tabled below.

Task NameDescription
EarAn extension of the Jar task with special treatment for files.
JarA set of files.
TarCreates a tar archive.
UnjarUnzips a jarfile.
UntarUntars a tarfile.
UnwarUnzips a warfile.
UnzipUnzip a zip file.
WarAn extension of the Jar tasks.

Audit Tasks

Task NameDescription
JDependIt is used to invoke JDepend parser.

Compile Tasks

Tasks which are used to compile source files are know as compile tasks. Some common built-in compile tasks are tabled below.

Tasks NameDescription
DependDetermines which class files are out of date with respect to their resources.
JavacIt compiles the source file.
JspCIt runs the JSP compiler.
NetRexxCIt compiles a NetRexx source file.
RmicRuns the rmic compiler.

Execution Tasks

Tasks which are used to execute of run applications are know as execution tasks. Some common built-in execution tasks are tabled below.

Tasks NameDescription
AntIt runs Ant on specified buildfile.
AntCallIt runs another target in the same buildfile.
ApplyIt executes a system command.
JavaExecutes a Java class.
ParallelA container task that can contain other ant tasks.
SleepIt suspends execution for the specified time.

File Tasks

Tasks which are related to handle file operations are know as file tasks. Some common built-in file tasks are tabled below.

Tasks NameDescription
ChmodIt changes permission of the file.
ChownIt changes ownership of the file.
ConcatIt concatenates multiple files.
CopyIt copies file to new destination.
DeleteDelete a file.
MkdirIt creates a directory.

Apache Ant Built-In Task Concatenate Example

Concatenate a single file, appending if the destination file exists

How to use Apache Ant Tasks?

To use tasks, first we need to create a project by using <project> tag. After that create a target to group tasks by using <target> tag. Then we can execute tasks by putting tasks inside the target tag. Let's see an example, Java task is created here by using <java> tag.

Apache Ant User Define Tasks

Apache Ant allows us to write our own tasks. It is pretty easy to write own tasks. Some required steps are given below. Consider the following points.

  1. First create a Java class and extends <org.apache.tools.ant.Task class.
  2. Create setter and getter methods for each attribute.
  3. If task contains other tasks as nested elements then class must implement the interface org.apache.tools.ant.TaskContainer.
  4. If the task supports character data, write a public void addText(String) method.
  5. For each nested element, write, add or addConfigured method.
  6. Write a public void execute() method, without argument and throws a BuildException.




Latest Courses