Ant Classpath and Custom Tasks

By default, the IDE ignores your environment's CLASSPATH variable whenever it runs Ant. This setting is defined by the Properties setting in Ant Settings, which sets the build.sysclasspath property to ignore. For your build script to use customs tasks, you must add the tasks to Ant's classpath in the IDE.

You can add custom tasks to Ant's classpath within the IDE by:

Build Scripts With an Explicit Classpath

Using an explicit classpath is the recommended method, as it ensures that your build scripts will be fully portable. You can write your tasks and include instructions to compile them and produce a JAR file in the build file. To use these tasks, include the long form of taskdef, which includes a classpath. Here is a simple of such a task.

The advantage of this method is that no special preparation is needed to begin using the script. The script is entirely self-contained and portable. This method also makes it easier to develop your tasks within the IDE, as the script compiles them for you automatically.

To make the your build scripts even more robust, use a property instead of a hard-coded location to specify the classpath to your tasks. You can store the property in the build script itself or in a separate ant.properties file. You can then change the classpath setting throughout your script by simply changing the value of the specified property.

Build Scripts With the IDE Filesystems Classpath

If you do not plan to use your build script outside of the IDE, you can mount the filesystem containing the source code for your tasks in the IDE's Filesystems tab. In your build script, add the following subelements to your taskdef:

<classpath>
    <pathelement path="${netbeans.library.path}"/>
    <pathelement path="${netbeans.filesystems.path}"/>
</classpath>
You do not need to provide values for {netbeans.library.path} and {netbeans.filesystems.path}. These properties are global Ant properties which the IDE expands to classpaths containing the IDE's own libraries (including Ant, parsers, and other libraries) and all of your mounted filesystems.The Ant script automatically looks for tasks definitions in your filesystems every time it runs.

Build Scripts Without a Defined Classpath

If you cannot declare a classpath in your build script, or you are using third-party build scripts which you cannot alter, you can add the tasks to your install_directory/modules/patches/org-apache-tools-ant-module/ directory. Then choose Tools and choose Options, expand the Building node, and select Ant Settings. Then click the ellipsis (...) button in the Properties setting and delete build.sysclasspath=ignore. All of the libraries in this directory are picked up by Ant when you run a build script.

tip

This method can cause problems with executing your Ant scripts outside of the IDE. When you delete build.sysclasspath=ignore, everything in the IDE's classpath, including the IDE APIs, XML parsers, and so forth, is quietly added to the Ant classpath. You could therefore unknowingly make your Ant script dependent on one of these additional libraries and thereby make it not run outside of the IDE.

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 modules/patches/org-apache-tools-ant-module/ directory of your IDE install directory.

Implicit Tasks

One style that is not supported by the IDE 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 be supported in the Ant module in the future.

See also
Configuring Ant Settings
Creating and Running an Ant Project

Legal Notices