Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally Delete in Ant

I want to delete the directory if the property "delete-compiled-dir" is set to true. If the property is false then do not execute it. Right now I have

<target name="deleted-after-compilation" depends="compile,jar">
        <condition property="${delete-compiled-dir}" value="true">
            <delete dir="${compilation-dir}" />
        </condition>
            <echo> Deleting Compiled Directory Classes </echo>
    </target>

I get the error message :

condition doesn't support the nested "delete" element.
like image 972
unj2 Avatar asked May 02 '10 19:05

unj2


2 Answers

You can add the condition to your target using if (see manual).

The target will only be executed when the property compilation-dir is set (to any value, e.g. false).

<target name="deleted-after-compilation" depends="compile,jar"
        if="${compilation-dir}">
    <delete dir="${compilation-dir}" />
    <echo> Deleting Compiled Directory Classes </echo>
</target>

To only execute it when a property is set to true, you need to set another property first and check this one in the if. You could add both as dependency the another target:

<target name="set-delete-property">
    <condition property="delete-compilation-dir">
        <istrue value="${compilation-dir}"/>
    </condition>
</target>

<target name="deleted-after-compilation"
      depends="compile,jar" if="${compilation-dir}">
    ....

<target name="some-target"
      depends="set-delete-property,deleted-after-compilation">
</target>
like image 69
Peter Lang Avatar answered Nov 08 '22 07:11

Peter Lang


There are a few ways to do this:

Use conditions on the target entity

Targets can contain if and unless conditions. The target will execute depending whether or not the property is set. (Not set to true, just set). This is a common way to see if you need to do something or not:

<target name="deleted.after.compilation" 
    if="delete.compiled.dir"
    depends="jar">
        <delete dir="${compilation-dir}" />
        <echo> Deleting Compiled Directory Classes </echo>
</target>

You can set the property on the command line:

$ ant -Ddelete.compiled.dir all

Note: I use periods as separators for the names of properties and targets. Also note that I only depend upon the target jar since jar is also dependent upon compile, there's no need to have them both.

Use Ant's 1.9.1 conditional clauses

As of Ant 1.9.1, Ant has conditional attributes that can be added to tasks. You need to add a Namepsace declaration in your <project> entity:

<project ... 
    xmlns:if="ant:if"
    xmlns:unless="ant:unless">

    <target name="deleted.after.compilation" 
        depends="jar">
        <delete dir="${compilation-dir}" if:true="${delete.compiled.dir}"/>
        <echo if:true="${delete.compiled.dir}"> Deleting Compiled Directory Classes </echo>
    </target>

Use Ant-Contrib's If Statement

Ha ha, that wacky Ant-Contrib library. No one knows who maintains it, and it hasn't been touched in years, but many people depend so heavily on it.

<project ...>
    <taskdef resource="net/sf/antcontrib/antlib.xml">
        <classpath>
            <fileset dir="${ivy.dir}/antcontrib">
                <include name="ant-contrib*.jar"/>
            </fileset>
        </classpath>
    </taskdef>

    <target name="deleted.after.compilation" 
        depends="jar">
        <if>
            <istrue value="${delete.compiled.dir}"/>
            <then>
                <delete dir="${compilation-dir}"/>
                <echo>Deleting Compiled Directory Classes </echo>
            </then?
        </if>
    </target>

You can see why Ant-Contrib is popular. It contains a lot of power, and we all know it. Plus, if someone is still using Ant 1.8 or 1.7, this will still work.

like image 22
David W. Avatar answered Nov 08 '22 08:11

David W.