tags:

views:

910

answers:

2

I have tho 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?

Actual Solution via: Michael's answer 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.

+3  A: 

You probably want to add a property for if that directory exists and make the delete task depend on it:

<target name="clean" description="Clean output directories.">
    <available file="${main.build.directory}" type="dir" property="build-dir-exists" />
    <delete includeEmptyDirs="true" failonerror="false" if="build-dir-exists">
        ...
Michael Mrozek
A: 

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.

Falconne