开发者

Conditionally Delete in Ant

开发者 https://www.devze.com 2022-12-29 11:41 出处:网络
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

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 Directo开发者_C百科ry Classes </echo>
    </target>

I get the error message :

condition doesn't support the nested "delete" element.


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>


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.


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

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


As of Ant 1.9.1, you can use conditionals on any task. Described here.

Add this namespace to your 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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号