Javatpoint Logo
Javatpoint Logo

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 Name Description
Ear An extension of the Jar task with special treatment for files.
Jar A set of files.
Tar Creates a tar archive.
Unjar Unzips a jarfile.
Untar Untars a tarfile.
Unwar Unzips a warfile.
Unzip Unzip a zip file.
War An extension of the Jar tasks.

Audit Tasks

Task Name Description
JDepend It 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 Name Description
Depend Determines which class files are out of date with respect to their resources.
Javac It compiles the source file.
JspC It runs the JSP compiler.
NetRexxC It compiles a NetRexx source file.
Rmic Runs 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 Name Description
Ant It runs Ant on specified buildfile.
AntCall It runs another target in the same buildfile.
Apply It executes a system command.
Java Executes a Java class.
Parallel A container task that can contain other ant tasks.
Sleep It 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 Name Description
Chmod It changes permission of the file.
Chown It changes ownership of the file.
Concat It concatenates multiple files.
Copy It copies file to new destination.
Delete Delete a file.
Mkdir It 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.





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA