Ant目标调用

时间:2013-04-08 11:18:36

标签: ant

我想在条件为真的情况下调用目标backup.yes。

<condition property="directory.found.yes">
<equals arg1="${directory.found}" arg2="true"/>
</condition>

<antcall target="update.backup"/>

有没有办法做到这一点。

2 个答案:

答案 0 :(得分:11)

而不是<antcall/>,请执行以下操作:

想象一下,您正在调用目标foo,并且您希望之前进行备份,但前提是存在该条件:

<target name="foo"
    depends="update.backup">
    <..../>
</target>

<target name="update.backup.test">
    <condition property="directory.found.yes">
         <equals arg1="${directory.found}" arg2="true"/>
    </condition>
</target>

<target name="update.backup"
    depends="update.backup.test"
    if="directory.found.yes">
    <.../>
</target>

<antcall/>的问题在于,当依赖关系矩阵Ant使用时,它被使用,并且它用于强制在另一个任务完成之前完成任务。如果真的被滥用,你最终会多次调用相同的任务。我在这里有一个项目,字面上称每个目标在10到14次之间,并且有超过24个目标。我重写了整个构建版本<antcall/>并使用真正的依赖项设置,将构建时间缩短了75%。

根据我的经验,90%的<antcall/>是由于目标依赖关系管理不善造成的。

假设您要执行目标foo。 (用户想要真正执行的目标),并且在调用foo之前,您希望进行备份,但前提是该目录实际存在。

在上面,调用了foo。这取决于update.backaup。调用目标update.backup,但它取决于update.backup.test,它将测试目录是否实际存在。

如果目录存在,if任务上的update.backup子句为true,则任务将实际执行。否则,如果目录不存在,则不会执行。

请注意,update.backup首先调用任何依赖项 之前它会检查if实体的unlesstarget参数上的属性是否为{检查。这允许目标在尝试执行之前调用测试。

这不仅仅是副作用,而是内置于Ant的设计中。事实上,“目标的Ant手册”(http://ant.apache.org/manual/targets.html)特别给出了一个非常相似的例子:

<target name="myTarget" depends="myTarget.check" if="myTarget.run">
    <echo>Files foo.txt and bar.txt are present.</echo>
</target>

<target name="myTarget.check">
    <condition property="myTarget.run">
        <and>
            <available file="foo.txt"/>
            <available file="bar.txt"/>
        </and>
    </condition>
</target>

并声明:

  

重要:if和unless属性仅启用或禁用它们所附加的目标。它们不控制条件目标所依赖的目标是否被执行。实际上,在目标即将执行之前,它们甚至都没有得到评估,而且它的所有前辈都已经运行了。

答案 1 :(得分:6)

您可以执行以下操作

在另一个目标中:

<antcall target="update.back">
    <param name="ok" value="${directory.found.yes}"/>
</antcall>

在update.backup目标中:

<target name="update.backup" if="ok">

但我认为您也可以使用if statement中的ant-contrib执行以下操作:

<if>
     <equals arg1="${directory.found.yes}" arg2="true" />
     <then>
           <antcall target="update.back" />
     </then>     
 </if>