Friday, October 03, 2003

Creating an Ant Task with Eclipse

  1. Add ant.jar to your eclipse project.
  2. Create package to hold task classes. I recommend xxx.yyy.ant.tasks
  3. Create task class. Here is a simple one:
    package com.affy.ant.tasks;
    import org.apache.tools.ant.*;
    public class HelloWorld extends Task {
        public void execute() throws BuildException {
            System.out.println("Hello World!");
        }
    }
    
  4. Select Window, Preferences, Ant, Classpath
  5. Set ANT_HOME
  6. Add the folder where your class files are stored (xxx/build/classes, for example)
  7. Select Task, Add Task
  8. Assign a task name (ie, HelloWorld) to use in build.xml
  9. Select location (the folder xxx/build/classes for example)

That's how easy it is to start writing your own Ant tasks. If you're going to invoke build.xml from within Eclipse, then you only need the following lines:

    <target name="HelloWorld">
        <HelloWorld/>
    </target>

Otherwise, you need to tell Ant where to find the class file. For example,

    <path id="cp">
        <pathelement path="xxx/build/classes"/>
    </path>
    <target name="HelloWorld">
        <taskdef name="HelloWorld" classname="org.wwre.ant.tasks.HelloWorld" classpathref="cp"/>
        <HelloWorld/>
    </target>

Post a Comment