我对 ANT 脚本非常陌生,正在使用它在我的项目中自动执行日常构建。我更多地在脚本(XML 文件)的意义上使用它并聚合预先存在的功能并提出构建过程。
我觉得我对 antcall/target 概念有一些基本的理解问题。特别是当antcall使用参数进行时,像C++一样,有没有办法调用目标将参数作为引用传递?以便调用者可以检索目标中更改的值?
在下面的示例中,我想检查两个文件是否相同并显示结果,但对于下面的示例,我将得到输出
相同的文件:${isFileName}
示例:
< target name="checkFileAreSame">
< condition property="isFileSame">
< filesmatch file1="a.txt" file2="b.txt"/>
< /condition >
< /target >
< target name="Maintask">
< antcall target="checkFileAreSame">
< param name="isFileSame" value="false">
< /antcall >
< echo message="Are files Same : ${isFileSame}"/>
< /target >
感谢您提前提供信息。
最佳答案
Ant properties are immutable - 一旦设置,它们的值就不能更改。
使用 antcall
task 时请注意:
The called target(s) are run in a new project; be aware that this means properties, references, etc. set by called targets will not persist back to the calling project.
但还要注意,使用“antcall”param
属性传递给被调用目标的属性在被调用目标中是不可变的。
这意味着在给出的“条件”任务示例中:
<condition property="isFileSame">
<filesmatch file1="a.txt" file2="b.txt"/>
</condition>
调用者已将 isFileSame
属性设置为 false
,因此无论文件比较如何,该属性都将保持为 false。
更常见的是 declare dependencies between targets以这种方式:
<target name="checkFileAreSame">
<condition property="isFileSame">
<filesmatch file1="a.txt" file2="b.txt"/>
</condition>
</target>
<target name="Maintask" depends="checkFileAreSame">
<echo message="Are files Same : ${isFileSame}"/>
</target>
Ant 将确定“Maintask”的调用图要求首先运行“checkFileAreSame”。
关于apache - 通过引用传递 ANT 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5006551/