Creating Custom Ant Tasks

You can create Ant tasks to use in your build scripts. An easy way to start is by using the Custom Task template provided by the IDE. Within the IDE, code completion is available for the Ant API (Javadoc documentation for the Ant API is mounted in the Javadoc tab).

To use the template:

  1. Choose File and choose New from the main window.
  2. Expand the Ant Build Scripts node and select Custom Task. Click Next to proceed.
  3. Provide a class name and package in the Name and Package text fields.
  4. Click Finish.
The Source Editor opens with a template in which you can define your custom task.

The Ant module supports two styles: with and without an explicit classpath.

With an Explicit Classpath

Using an explicit classpath is recommended. Write your tasks and then include in the build file, for instance, instructions to compile them (and produce a JAR file for them as well). To use them, include the long form of taskdef, which includes a classpath. Here is a simple of such a task.

The advantage is that no special preparation is needed to begin using the script; it is entirely self-contained. You can develop the tasks within the IDE also; the script compiles them for you automatically.

With an Automatic Classpath

According to the script, custom tasks are not stored in a specific location. The classpath attribute is missing from taskdef. You need to add the tasks to your classpath before starting Ant at the command line.

Inside the IDE, it also works to start the IDE with the tasks in its classpath. But a much better idea is to just mount the filesystem containing the source code for your tasks in the IDE's Filesystems tab. You can use the IDE to interactively develop and compile them. The Ant script automatically looks for tasks definitions in your filesystems every time it runs. To make this work, you must add the following subelements to your taskdef:

<classpath>
    <pathelement path="${netbeans.library.path}"/>
    <pathelement path="${netbeans.filesystems.path}"/>
</classpath>
The properties are expanded by the IDE to classpaths containing the IDE's own libraries (including Ant, parsers, and so on) first, and then to all of your mounted filesystems.

Optional Tasks

The Ant integration module includes Ant's optional.jar file. These tasks are mostly optional, because they require special libraries not shipped with Ant (and generally not shipped with the IDE either). If you need some of these tasks, download the extra libraries and place the JAR files in the IDE's modules/patches/org-apache-tools-ant-module/ directory.

Implicit Tasks

One style that is not supported by the module is defining tasks implicitly. Ant enables you to add default task definitions to a special file that lets any script you use automatically pick up task definitions. This feature might supported in the Ant module in the future.

Custom Tasks and the Explorer View

If you define custom tasks and then run a project using them at least once, the Ant module recognizes their XML structure and enables you to edit them in the Explorer just like standard tasks. The same applies to custom data types.

For more information on writing Ant tasks, see Using Ant in the Ant 1.4.1 Manual.

See also
Configuring Ant Settings
Creating and Running an Ant Project

Legal Notices