Ant is not a programming language and therefore has no native looping mechanism. In the absence of external plugins a trick that can used is an XSL transformation. Process the input XML file into a Ant script which implements the desired operation on each file.
Example
├── build.xml
├── files-process.xsl
├── files.xml <-- Input listed above
├── test1.props
├── test2.props
└── test3.props
Run the build and the files listed in "files.xml" are deleted:
build:
[delete] Deleting: /home/mark/tmp/test1.props
[delete] Deleting: /home/mark/tmp/test2.props
[delete] Deleting: /home/mark/tmp/test3.props
Run the build a second time and an error is generated:
BUILD FAILED
/home/mark/tmp/build.xml:6: The following error occurred while executing this line:
/home/mark/tmp/build-tmp.xml:4: file not found: test1.props
build.xml
Use the xslt task to generate a temporary Ant script containing the desired file deletion logic:
<project name="demo" default="process-files">
<target name="process-files">
<xslt style="files-process.xsl" in="files.xml" out="build-tmp.xml"/>
<ant antfile="build-tmp.xml"/>
</target>
<target name="clean">
<delete file="build-tmp.xml"/>
</target>
</project>
files-process.xsl
The following stylesheet generates an Ant script:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<project name="genbuild" default="build">
<target name="build">
<xsl:apply-templates select="property-bundle/file-name"/>
</target>
</project>
</xsl:template>
<xsl:template match="file-name">
<available file="{.}" property="{generate-id()}.exists"/>
<fail message="file not found: {.}" unless="{generate-id()}.exists"/>
<delete file="{.}" verbose="true"/>
</xsl:template>
</xsl:stylesheet>
build-tmp.xml
To aid readability I have formatted the generated Ant script:
<project name="genbuild" default="build">
<target name="build">
<available file="test1.props" property="N65547.exists"/>
<fail message="file not found: test1.props" unless="N65547.exists"/>
<delete file="test1.props" verbose="true"/>
<available file="test2.props" property="N65550.exists"/>
<fail message="file not found: test2.props" unless="N65550.exists"/>
<delete file="test2.props" verbose="true"/>
<available file="test3.props" property="N65553.exists"/>
<fail message="file not found: test3.props" unless="N65553.exists"/>
<delete file="test3.props" verbose="true"/>
</target>
</project>
Note:
- Properties in Ant are immutable, so I used the XSL generate-id() function to create a unique property name.
Software used
This example was tested with the following software versions:
$ ant -version
Apache Ant version 1.6.5 compiled on June 2 2005
$ java -version
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)