Ant 目标调用

标签 ant

仅当条件为真时,我才想调用 target backup.yes。

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

<antcall target="update.backup"/>

有没有办法做到这一点。

最佳答案

而不是 <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 次,并且有超过两打目标。我重写了整个构建无 <antcall/>并且通过使用真正的依赖设置,将构建时间减少了 75%。

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

假设您要执行目标 foo . (用户想要真正执行的目标),以及之前 foo被调用时,您想要进行备份,但前提是该目录确实存在。

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

如果目录存在,if关于 update.backup 的条款task 为真,任务将实际执行。否则,如果目录不存在,它将不会执行。

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

这不仅仅是一个副作用,而是内置于 Ant 的设计中。事实上,Ant Manual on Targets]( 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>

并指出:

Important: the if and unless attributes only enable or disable the target to which they are attached. They do not control whether or not targets that a conditional target depends upon get executed. In fact, they do not even get evaluated until the target is about to be executed, and all its predecessors have already run.

关于 Ant 目标调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15877714/

相关文章:

java - 使用 ANT 在一个非常大的项目中生成一个 .jar

Python 构建/发布系统

ant - 如何在 Gradle JAXB/XJC 中编译多个 xsd 文件?

javascript - ant进程中回显文件名

java - 无法将 maven 属性传递给 ant run 插件

ant - 如何使用 Ant 重命名文件和文件夹

java - 在 Mac OS X 上使用 JDK 1.6 运行 Ant

java - 在 Mac OS X 上运行 Ant 构建时出现 "Permission denied"错误

shell - 任务抛出错误=7 : Argument list too long

apache - 我需要在服务器中运行 DITA Toolkit 吗? Apache Ant/FOP 需要服务器才能运行吗?