Ant Tasks

There is a great deal of "folklore" knowledge about the individual Ant tasks that is not clear from the specification of the tasks offered in the Ant Manual.

The Sync Task (present since 1.6)

Here is an example of some lore of this type: The Sync task will delete 'dangling files' and 'dangling directories' in the target directory.

The Property Task

The Prefix Attribute

The Problem:
I have to access some properties (from a file), which are already defined in my build file. To be able to access the properties I can use the prefix attribute of the property task.

#Release information about the framework
#Thu Oct 14 16:25:12 CEST 2004

Ant Example Target

<target name="">    
  <!-- Read the properties from the release of the framework -->
  <property file="" prefix="framework"/>
  <echo message="${}"/>
  <echo message="${framework.release.version}"/>
  <echo message="${}"/>    


Buildfile: C:\build.xml
  [echo] 115
  [echo] 0.4
  [echo] framework
Total time: 3 seconds

The AntCall Task

Sometimes on the mailinglists there is a question on how to invoke a special target depending on the value of a property. Because there could be more than two values, you can´t use the usual if/unless-attributes of the target.

But the solution is quite easy: compound the name of the target by a common term and the value itself and let antcall do the job.

Ant Example Target

<project default="call">
    <target name="foo-upload"><echo>FOO upload</echo></target>
    <target name="bar-upload"><echo>BAR upload</echo></target>
    <target name="test-upload"><echo>TEST upload</echo></target>

    <target name="call">
        <input message="Please choose the server "
        <antcall target="${server}-upload"/>

You have only to ensure, that there are valid values for that property, e.g. a blabla would call the non existing blabla-upload which results in an error. But the input task here will ensure that.

BTW - the same can be used for loading platform specific properties:

<property file="${}.properties"/>
<property file=""/>

The Exec Task

On Windows the Exec task does not find the executable file if its directory was double quoted in the PATH environment variable. (Execute failed ... error 2: which means file not found.)

The Unzip Task

How to unzip the content of a subdirectory of a zip file without the path to the subdirectory. For example, if a zip file has a directory dir1/dir2, how to unzip dir2 into dir3 so that dir3 contains only dir2, not dir1/dir2?

The XSLT Task

This task allows you to perform XML transforms using the built-in XSLT system. XSLT 2.0 can also be done using the Saxon system.

Here is a demonstration UsingAntWithXSLT20AndSaxon.


  • No labels