James Raitsev
James Raitsev

Reputation: 96581

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?

Upvotes: 39

Views: 60877

Answers (7)

The CTO
The CTO

Reputation: 91

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>

Upvotes: 2

TuciBeyin
TuciBeyin

Reputation: 11

Here is the answer :

    <target name="delete.target.directory">  
        <delete includeemptydirs="true" verbose="false" if="${(directory::exists(directory))}">
            <fileset basedir="${directory}">  
                <include name="**/*.*" />  
            </fileset> 
        </delete>
    </target> 

Upvotes: 1

Gangnus
Gangnus

Reputation: 24484

You can do it by ordering to delete a list of files with names equal to the name you need. It is much easier and direct than to create a special target. And you needn't any additional tools, just pure Ant.

    <delete>
        <fileset includes="name or names of file or files you need to delete"/>
    </delete>

http://ant.apache.org/manual/Types/fileset.html

Upvotes: 0

ekangas
ekangas

Reputation: 863

For this specific case, I'm not going to answer the question "how to find if a directory exists", because that's already been answered, but I'm just going to point out that in your clean task you can use failonerror="false" to keep the ant task from exiting. This should be suitable in a clean task because if there's nothing to clean, it should not be a problem.

    <target name="clean" description="clean">
        <delete dir="${build}" failonerror="false"/>
        ....
        <delete dir="${report}" failonerror="false"/>
    </target>

This is useful if you don't want to install ant-contrib or can't for some reason.

Upvotes: 44

James Raitsev
James Raitsev

Reputation: 96581

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>

Upvotes: 15

Rebse
Rebse

Reputation: 10377

with vanilla ant you would use something like =

 <target name="check">
  <condition property="deldir">
    <available file="${somedir}" type="dir"/>
  </condition>
 </target>

 <target name="deldir" depends="check" if="deldir">
 <delete dir="${somedir}"/>
    <!-- .. -->
 </target>

else see = Ant check existence for a set of files
for a similar question

Upvotes: 18

pjeong
pjeong

Reputation: 39

Check out the available task.

Here's a similar question.

Do I have a way to check the existence of a directory in Ant (not a file)?

Upvotes: 3

Related Questions