Ant目标调用

jer*_*ohn 8 ant

我想在条件为真时调用目标backup.yes.

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

<antcall target="update.backup"/>
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点.

Dav*_* W. 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>
Run Code Online (Sandbox Code Playgroud)

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

根据我的经验,90%<antcall/>是由于糟糕的目标依赖管理.

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

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

如果该目录存在,则ifupdate.backup任务的子句为true,并且该任务将实际执行.否则,如果目录不存在,则不会执行.

请注意,update.backup首先调用任何依赖之前它会检查该属性是否ifunless参数的target实体进行检查.这允许目标在尝试执行之前调用测试.

这不仅仅是副作用,而是内置于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>
Run Code Online (Sandbox Code Playgroud)

并指出:

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

  • 应该不惜一切代价避免+1 antcall - 打开一个新的项目范围,打破流程(取决于被调用目标的目标也将被调用..),属性不会被传递回调用目标..等等邪恶的根源,维护这样的脚本是PITA.最后,当ant> = 1.6 =>使用macrodef时,不再需要antcall. (3认同)

Vir*_*oll 6

您可以执行以下操作

在另一个目标:

<antcall target="update.back">
    <param name="ok" value="${directory.found.yes}"/>
</antcall>
Run Code Online (Sandbox Code Playgroud)

并在update.backup目标中:

<target name="update.backup" if="ok">
Run Code Online (Sandbox Code Playgroud)

但是,我想你也能做到使用下面的if语句蚂蚁的contrib:

<if>
     <equals arg1="${directory.found.yes}" arg2="true" />
     <then>
           <antcall target="update.back" />
     </then>     
 </if>
Run Code Online (Sandbox Code Playgroud)