How to check if directory exists before deleting it, using ANT?

Viewed 58496

Using ANT, how can i make sure that directory exists before attempting to remove it?

As part of my current clean task, i

<target name="clean" description="clean">
    <delete dir="${build}" />
    <delete dir="${bin}" />
    <delete dir="${dist}/myrunner.${version}.jar" />
    <delete dir="${doc}" />
    <delete dir="${report}" />
</target>

This works well, however (obviously) remove happens when there is something to remove.

Using ANT, how can i check if directory exist?

7 Answers

You can use this brute force technique as pure ant, which is specially useful in maven where the additional conditionals aren't available.
The mkdir will do nothing if the directory already exists, and the delete will fail if the folder cannot be deleted for some reason. The condition of the file not being there is common in a build script where one of the build steps fail but the initial clean was performed.

<target name="clean">
    <mkdir dir="${project.basedir}/dist" />
    <delete dir="${project.basedir}/dist" />
</target>
Related