javaanttaskdef

How can I run an ant task as a prerequisite to <taskdef>?


I created my own ant task using the instructions here. In my ant script, I create the <taskdef> like this:

<!-- myUploader.xml -->
<taskdef name="fileUpload" classname="com.awt.client.UploaderTask" classpath="lib/fileUploader.jar" />

<target name="setup" description="some required setup before taskdef!">
    <!-- checking for required jars, etc... -->
</target>

And then I can import the script that invokes this as an ant task:

<!-- build.xml -->
<import file="myUploader.xml" />
<fileUpload server="${server}" username="${username}" password="${password}" appname="TestApp" appversion="13" />

This all works fine. Now, there is a bit of setup I would like to happen in myUploader.xml before the taskdef occurs. <taskdef> does not like if, unless, or depends. How can I ensure that my setup task is called before <taskdef> is done?


Solution

  • One way is to move the taskdef inside the setup target:

    <target name="setup" description="some required setup before taskdef!">
        <!-- checking for required jars, etc... -->
        <taskdef name="fileUpload" classname="com.awt.client.UploaderTask" classpath="lib/fileUploader.jar" />
    </target>
    

    Then in the main buildfile, after importing myUploader.xml, invoke the setup target which is now responsible for defining your custom task.

    Or you can move the part of the setup target to outside (becoming a top-level section):

    <project>
    
         <!-- do required setup here -->
       <taskdef name="fileUpload" classname="com.awt.client.UploaderTask" classpath="lib/fileUploader.jar" />
    
    ...