有条件地删除Ant

时间:2010-05-02 19:22:53

标签: java ant conditional-statements

如果属性“delete-compiled-dir”设置为true,我想删除目录。如果属性为false,则不执行它。现在我有

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

我收到错误消息:

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

4 个答案:

答案 0 :(得分:4)

您可以使用if向目标添加条件(请参阅manual)。

目标只会在设置属性compilation-dir时执行(对任何值,例如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>

答案 1 :(得分:1)

有几种方法可以做到这一点:

在目标实体

上使用条件

目标可以包含ifunless条件。目标将根据是否设置属性来执行。 (未设置为 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有conditional attributes可以添加到任务中。您需要在<project>实体中添加 Namepsace声明

<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的If Statement

哈哈,那个古怪的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,这仍然有用。

答案 2 :(得分:0)

如果您已获得该属性,则可以在目标中使用它。

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

答案 3 :(得分:0)

从Ant 1.9.1开始,您可以在任何任务上使用条件。描述here

将此命名空间添加到项目元素:

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

然后将其添加到您的删除:

<delete dir="${compilation-dir}" if:true="${delete-compiled-dir}"/>
相关问题