Conditionally delete in Ant

I want to delete a directory if the "delete-compiled-dir" property is set to true. If the property is false, 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 an error message:

condition doesn't support the nested "delete" element.
+5
source share
4 answers

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

The goal will be fulfilled only when the property is set compilation-dir(any value, for example false).

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

, true, if. :

<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>
+4

:

if unless. , . ( true, ). , - :

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

:

$ ant -Ddelete.compiled.dir all

. . , jar, jar compile, .

Ant 1.9.1

Ant 1.9.1, Ant , . Namepsace <project>:

<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>

Ant -Contrib

-, Ant -Contrib. , , , .

<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>

, Ant -Contrib. , . , - Ant 1,8 1,7, .

+1

if you got a property, you can just use it in the target.

<target name="delete" if="${delete-compiled-dir}">
    <delete dir="${compilation-dir}" />
</target>
0
source

As with Ant 1.9.1, you can use the conventions for any task. Described here .

Add this namespace to the project element:

<project name="yourproject" xmlns:if="ant:if">

Then add this to your delete:

<delete dir="${compilation-dir}" if:true="${delete-compiled-dir}"/>
0
source

All Articles