有条件地在Ant中删除

5

如果属性"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.

什么问题?你有任何问题吗? - Romain Hippeau
是的。该条件显然不支持删除元素。 - unj2
4个回答

4

您可以使用if(请参见手册)向目标添加条件。

只有在设置属性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>

如果该属性为false,我不希望它执行。 - unj2
我目前无法测试,但它应该像那样工作。 - Peter Lang

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

在目标实体上使用条件

目标可以包含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具有可以添加到任务中的条件属性。您需要在<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 的 If 语句

哈哈,那个古怪的 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

从 Ant 1.9.1 开始,你可以在任何任务中使用条件语句。 在这里有描述。

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

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

然后将此添加到您的删除操作中:

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

0
如果您已经获取了属性,可以直接在目标中使用它。
<target name="delete" if="${delete-compiled-dir}">
    <delete dir="${compilation-dir}" />
</target>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接