Creating an Ant XDoclet Task

Problem

You want to create an Ant XDoclet task and subtask to generate a new type of file.

Solution

Extend xdoclet.DocletTask to create the main task, if necessary, and extend xdoclet.TemplateSubTask to create one or more subtasks.

Discussion

This recipe is the first part of a five-part recipe. It describes the steps needed to create a new Ant XDoclet task and subtask. We are going to create tasks for the JUnitPerfDoclet code generator.

Tip

JUnitPerfDoclet does not need a main XDoclet task. Specifically, we do not need to extend xdoclet.DocletTask because no extra functionality needs to be added. All of the functionality needed by JUnitPerfDoclet lies in the subtask. For completeness, this recipe shows how to create a Doclet task.

Creating an Ant XDoclet task

The first step is to create the main Ant XDoclet task. This task is much like the ejbdoclet task, serving as an entry point into the XDoclet engine.

Here are the steps needed to create an Ant XDoclet task:

  1. Create a new Java source file and give it a name. For this example, create a YourNewDocletTask.java file and add it to your project.

  2. Add the following import:

    import xdoclet.DocletTask;
  3. Extend the DocletTask class:

    public class YourNewDocletTask extends DocletTask {
  4. Add the necessary public getter and setter methods for attributes your task defines. For example, if your new task defines an attribute called validId, then you would have this setter method:

    public void setValidId(String id) {
        this.id = id;
    }

Get Java Extreme Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.