Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Ant's delete task fail when a directory exists and is not deleted but not when it doesn't exist at all

Tags:

ant

I have the following clean function in my build script and I'd like to know how I can improve it.

<target name="clean" description="Clean output directories.">
    <!-- Must not fail on error because it fails if directories don't exist.
         Is there really no better way to do this? -->
    <delete includeEmptyDirs="true" failonerror="false">
        <fileset dir="${main.build.directory}" />
        <fileset dir="dist" />
        <fileset dir="${documentation.build.directory}" />
        <fileset dir="/build-testing" />
    </delete>
</target>

Specifically regarding my comment, I'm unhappy with the fact that I can't run this on a fresh box because the directory structure hasn't been set up yet by the other targets. We run the build in such a way that it entirely recreates the structures necessary for testing and deployment every time to avoid stale class files and such. With the way that delete currently is set up, a failure to delete a file does not fail the build and I'd like it to. I don't want it to fail the build if the file doesn't exist though. If it doesn't exist then what I'm asking it to do has already happened.

Thoughts?

like image 833
Tim Visher Avatar asked Apr 15 '10 14:04

Tim Visher


2 Answers

via Michael's answer, which was 90% of what I needed but not quite all the way there.

The actual solution that I ended up with because of your answers is the following:

<target name="clean" description="Clean output directories.">
    <!-- Must not fail on error because it fails if directories don't exist.
         Is there really no better way to do this? -->
    <delete includeEmptyDirs="true" failonerror="false">
        <fileset dir="${main.build.directory}" />
        ...
    </delete>

    <available 
            file="${main.build.directory}"
            type="dir"
            property="delete-main-failure" /> ...

    <condition property="delete-failure">
        <and>
            <isset property="delete-main-failure" /> ...
        </and>
    </condition>
    <fail 
            if="delete-failure"
            message="Unable to delete previous build's directories." />
</target>

This meets my criteria that the code attempts to delete it and then fails if it still exists. It's super ugly though. The default behavior of the delete task strikes me as very odd. I suppose the rationale is that if you try to delete something and it isn't there then something must be wrong but it seems to me that the normal case would be that if it's not there you don't care because it's gone already while the odd case is that you needed it to be there but now it shouldn't be anymore at this specific stage in the build.

like image 162
Tim Visher Avatar answered Nov 06 '22 08:11

Tim Visher


I came here to ask the same question... it doesn't look like there is an elegant way to solve this. When I want to keep the code clean, I do it this way:

<mkdir dir="${main.build.directory}" />
<delete dir="${main.build.directory}" failonerror="true" />

I didn't think the delete task had an "if" property. Will have to check that out.

like image 44
Falconne Avatar answered Nov 06 '22 08:11

Falconne