ANT参数通过引用传递

时间:2011-02-15 16:40:54

标签: apache ant parameter-passing


我是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 >


感谢您事先提出的​​意见。

2 个答案:

答案 0 :(得分:2)

Ant properties are immutable - 一旦设置,就无法更改其值。

使用antcall task时注意:

  

被叫目标以新的方式运行   项目;请注意,这意味着   由...设置的属性,引用等   被叫目标不会持久   到调用项目。

但是请注意,使用'antcall'param属性传递给被调用目标的属性在被调用目标中是不可变的。 这意味着在'条件'任务示例中给出:

<condition property="isFileSame">
    <filesmatch file1="a.txt" file2="b.txt"/>
</condition>

调用者已将isFileSame属性设置为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”。

答案 1 :(得分:2)

听起来你需要ant-contrib包中的AntCallBack任务(与核心Ant分开)。

Ant-contrib是一个非常有用的库,如果你对Ant的“声明式”流式不满意(比如很多)。

见这里:

http://ant-contrib.sourceforge.net/

http://ant-contrib.sourceforge.net/tasks/tasks/index.html

http://ant-contrib.sourceforge.net/tasks/tasks/antcallback_task.html