Search code examples
antbuild

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


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?


Solution

  • Nice and clean solution below: Using ant-contribs.jar

    When using this solution, be sure to put the following line on top

    <!-- For <if> statements -->
    <taskdef resource="net/sf/antcontrib/antlib.xml" />
    

    <!-- Remove distribution directories and their content for a clean build --> <target name="clean" description="clean"> <if> <available file="${build}" type="dir" /> <then> <delete dir="${build}" /> </then> </if> </target>