Coding With Fun
Home Docker Django Node.js Articles FAQ

Extend Ant


May 25, 2021 Apache Ant


Table of contents


Extend Ant

Ant comes with a predefined set of tasks, but you can create your own, as shown in the following example.

Custom Ant tasks should extend the org.apache.tools.ant.Task class, and should also expand the execute() approach. Here's a simple example:

package com.tutorialspoint.ant;

import org.apache.tools.ant.Task;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.BuildException;

public class MyTask extends Task {
   String message;
   public void execute() throws BuildException {
      log("Message: " + message, Project.MSG_INFO);
   }

   public void setMessage(String message) {
      this.message= message;
   }
}

In order to run custom tasks, you need to add the following to the Hello World fax web application:

<target name="custom">
   <taskdef name="custom" classname="com.tutorialspoint.ant.MyTask" />
   <custom message="Hello World!"/>
</target>

After performing the customization task above, a message will be printed with the message: 'Hello World!'

>c:\>ant custom
>test:
>[custom] Message : Hello World!
>elapsed: 0.2 sec
>BUILD PASSED

This is just a simple example of how you can use Ant's capabilities to do anything you want to improve the efficiency of your build and deployment process.