2

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?

AWT
  • 3,657
  • 5
  • 32
  • 60

1 Answers1

3

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" />

...
M A
  • 71,713
  • 13
  • 134
  • 174
  • This is my current workaround, I'm glad to see I'm on the right track. I plan to distribute myUploader.xml with a product and I'm trying to get the setup as simple as possible (no dependencies other than the import). I was hoping for just a single import and then we could invoke the task. But this is my runner-up choice right now. – AWT Jul 15 '15 at 19:37
  • 1
    If the setup part is needed, why not simply remove it from the target and place it before the `taskdef`, i.e. put the setup outside any target so that it runs when import `myUploader.xml`. – M A Jul 15 '15 at 19:53
  • 1
    I'm fairly new when it comes to ant and I didn't know I could do that. That does exactly what I needed, thanks! If you want to update your answer I will accept it. – AWT Jul 15 '15 at 19:55