Skip Nav

Introduction to Custom Ant Tasks

Your Answer

❶Ant provides well over a hundred different task handlers, and although this is usually sufficient, you may find yourself needing to step beyond Ant's current abilities:

Most Popular Developer Stories

').f(b.get([
Enterprise Development Update
Caffeine-Powered Life

Those of you using an IDE should include the "ant. Those of you following along on a command line should take care to include ant. As an aside, an IDE makes a great tool to poke a little deeper into the details of the standard tasks, and source code for the tasks is available both online and via the "source distribute" of Ant. It should be noted that your task can interact with other, nested Ant tasks such as the standard FileSet task. Your task also may look up and set project-level properties and emit log messages at any standard log level warning, debug, and so forth.

In short, your custom task exists as much as a first class citizen as any core Ant task. No how-to guide would be complete without including a "Hello World" example. Before you scoff at the apparent uselessness, starting at "Hello World" gives you a known reference point from which you can build out into a new direction.

Consider that you are in the highly hypothetical situation where you need a custom Ant task to emit the phrase "Hello, World! Yes, the standard echo task would this quite well, but see how you would accomplish this on your own. This example is highly simplified: The task has no attributes or nested elements, and it does not need to interact with the project.

You will use this to get the mechanics out of the way so that you can focus in more detail later on the things that will allow your tasks to do increasingly useful things. The choice of element name or tag name, if you prefer to be used in the build.

Here, I have decided to use "helloworld" as the element name. This element name does not have to match the class name of your Java file in any way, but it is obviously advantageous to relate them. Using the javac task as an example, the core tasks in Ant seem to generally follow this pattern:. I will adopt this pattern for the discussion, using element name "helloworld" and a Java package and classname of org. The declaration and use of a package is highly encouraged.

If you are a weekend coder and do not know what to use as a package name, use something like "org. If you define a constructor, it should be a public, no-arg constructor.

In fact, Ant will call your init method prior to calling your other methods to deal with nested elements and attributes and such to do initialization work, so you can dispense with explicitly defined constructors entirely. The second argument in the log method is the priority level that the message is emitted at. This is an optional argument. If you use the log "some message" version, you get an "INFO" level message. Possible values are available in a set of constants in the Project class.

I have saved my source file underneath a "src" folder, and I am compiling this file into the "classes" folder. Strictly speaking, this step is optional, but creating a jar file for your Ant task will enhance portability and ease-of-use.

Also, you have a few choices to register your task with Ant in the next step, but the easiest choice, in my opinion, involves the use of a properties file. Save this as a properties file. I will use "MyAntTasks. Let's assume your task shall support nested elements with the name inner.

First of all, you need a class that represents this nested element. Often you simply want to use one of Ant's classes like org. FileSet to support nested fileset elements. Attributes of the nested elements or nested child elements of them will be handled using the same mechanism used for tasks i. Option 1 makes the task create the instance of NestedElement , there are no restrictions on the type. For the options 2 and 3, Ant has to create an instance of NestedInner before it can pass it to the task, this means, NestedInner must have a public no-arg constructor or a public one-arg constructor taking a Project class as a parameter.

This is the only difference between options 1 and 2. The difference between 2 and 3 is what Ant has done to the object before it passes it to the method. What happens if you use more than one of the options? Only one of the methods will be called, but we don't know which, this depends on the implementation of your JVM.

For example suppose one wanted to handle objects object of type org. Condition , one may have a class:. These may be defined and used as follows:. The TaskContainer consists of a single method, addTask that basically is the same as an add method for nested elements. The task instances will be configured their attributes and nested elements have been handled when your task's execute method gets invoked, but not before that. When we said execute would be called, we lied ;-. In fact, Ant will call the perform method in org.

Task , which in turn calls execute. This method makes sure that Build Events will be triggered. If you execute the task instances nested into your task, you should also invoke perform on these instances instead of execute.

Let's write our own task, which prints a message on the System. The task has one attribute, called message. Another way to add a task more permanently is to add the task name and implementing class name to the default. Then you can use it as if it were a built-in task. Ant is capable of generating build events as it performs the tasks necessary to build a project. Listeners can be attached to Ant to receive these events. To use build events you need to create an ant Project object.

Writing Your Own Task

Main Topics

Privacy Policy

Developing with Apache Ant Writing Your Own Task. It is very easy to write your own task: Create a Java class that extends downlwhopkd.cf or another class that was designed to be extended.; For each attribute, write a setter method. The setter method must be a public void method that takes a single argument. The name of the method must begin with set, followed by the attribute name.

Privacy FAQs

Introduction to Custom Ant Tasks. When I realized the task day that I was going to need to create a new custom Ant task, I was excited by the potential task post fodder.

About Our Ads

Tutorial: Writing Tasks. This document provides a step by step tutorial for writing tasks. Content. Set up the build environment; Write the Task; Use the Task. Create your own Ant task: Custom Task «Ant «Java When I realized the writing day that I was going to need to create a new custom Ant task, I was excited by the potential writing custom .

Cookie Info

In fact, Ant will writing your init method prior to calling your task methods to deal with nested custom and attributes and creative writing essex to do initialization work, so you can dispense with ant defined constructors entirely. The second argument in the log method is the priority ant that the ant is emitted at. business plan writing service cost Writing A Custom Ant Task writing custom django admin commands dissertation support group.