Article:
  Writing Ant Tasks
Subject:   Feedback
Date:   2004-06-04 02:05:08
From:   erikhatcher
A couple of comments - in your first example, you do not even need to extend Task or import BuildException - you never throw it or use the Task infrastracture. Simply a "public void execute()" method makes a class an Ant task.


I find it comical that you used javac and jar at the command-line to build your Ant task JAR. You should have used Ant's javac and jar and shown how to build and use the custom task all in one shot. Interestingly, with the new scriptdef task you could have made your adder task purely as script (JavaScript for example).


And yes, you can use an Ant task as .class not in a JAR. Use the classpath capability of taskdef to set it. An even slicker method of defining tasks is through the new Antlib capability which avoids having fully qualified classnames in your build file which are subject to change.

Full Threads Oldest First

Showing messages 1 through 3 of 3.

  • Michael Fitzgerald photo Feedback
    2004-06-05 22:21:11  Michael Fitzgerald | O'Reilly AuthorO'Reilly Blogger [View]

    (1) Sure, a simple execute() could do, as described in the tutorial, which I recommeded to readers. I wrote a simple task that has a parallel structure to the more complex Jing task, following the guidelines in the first six steps of "Writing Your Own Task". Are those guidelines incorrect or out of date? If so, this section of the Ant manual needs to be updated.

    (2) Ant is a nice tool, but I don't feel it is necessary to use it to build something as simple as Add.java -- one very small class with almost zero potential for continuous debugging or rebuilding.

    (3) As far as using .class files: How exactly do you do that? I tried it every which way from Tuesday with no luck. Show us! (I haven't paid any attention to Antlib yet. Thanks for pointing it out.)
    • using classpath vs jar
      2004-07-21 20:07:48  hapybrian [View]

      Using a class file seems to work just fine for me.
      Using the "Add.class" example you use, simply
      make sure your classpath contains the directory
      where Add.class lives. Before running ant, I
      set my class path to "blahblah;newtasks" (under
      windoze). newtasks is a subdirectory of my
      current directory. newtasks contains Add.class
      (which is in the newtasks package). Similarly,
      you can add the classpath to your taskdef line

      <taskdef name="adder" classname="newtasks.Add"
      classpath="newtasks"/>

      This was done using ant 1.5, and jdk 1.4

      Brian
      • using classpath vs jar
        2004-11-23 21:22:00  Xapp [View]

        <project name="Add" default="dist" basedir=".">
        <description>
        The simple ant file to build and test Add.
        </description>
        <!-- set global properties for this build -->
        <property name="src" location="src"/>
        <property name="build" location="build"/>
        <property name="dist" location="dist"/>
        <property environment="env"/>

        <target name="init">
        <tstamp/>
        <mkdir dir="${build}"/>
        </target>

        <target name="compile" depends="init"
        description="compile the source " >
        <javac srcdir="${src}" destdir="${build}" classpath="${env.ANT_HOME}/lib/ant.jar"/>
        </target>

        <target name="dist" depends="compile"
        description="generate the distribution" >
        <mkdir dir="${dist}/lib"/>

        <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
        <jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>
        </target>

        <target name="test" depends="compile">
        <taskdef name="Add" classname="net.rsherk.ant.Add" classpath="${build}"/>
        <Add op1="1" op2="2"/>
        </target>
        <target name="clean"
        description="clean up" >
        <!-- Delete the ${build} and ${dist} directory trees -->
        <delete dir="${build}"/>
        <delete dir="${dist}"/>
        </target>
        </project>