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?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With