Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

build

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?

like image 792
James Raitsev Avatar asked Jun 13 '11 19:06

James Raitsev


People also ask

Which one is true code for delete the directory in ant?

This task is used to delete a single file, directory or subdirectories. We can also delete set of files by specifying set of files. It does not remove empty directory by default, we need to use includeEmptyDirs attribute to remove that directory.

How do you delete a directory if exists in Linux?

Why not just use rm -rf /some/dir ? That will remove the directory if it's present, otherwise do nothing. Unlike rm -r /some/dir this flavor of the command won't crash if the folder doesn't exist.


2 Answers

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.

like image 185
ekangas Avatar answered Sep 28 '22 18:09

ekangas


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

like image 41
Rebse Avatar answered Sep 28 '22 20:09

Rebse