ant中利用macrodef来定义可重用的task

先来看下macrodef的说明

This defines a new task using a <sequential> nested task as a template. Nested elements <attribute> and <element> are used to specify attributes and elements of the new task. These get substituted into the <sequential> task when the new task is run.

下面是ant doc的一个example
<macrodef name="test">  这里是macrodef的定义,定义了name属性
<attribute name="one"/> 参数定义,可以在macrodef外部调用
<attribute name="two" default="@{one}"/> 内部参数
<sequential>
实际执行的内容在sequential里
<echo>one=@{one} two=@{two}</echo>
</sequential>
</macrodef>

<test one="test"/> 外部调用

需要注意的是:
1、在整个build文件里macrodef是和target平级的。
2、macrodef可以调用其他macrodef,不可以调用target;target可以调用macrodef,也可以调用其他target
3、macrodef嵌套的时候,参数名称必须不同
比如上面的macrodef test,定义了one的参数
如果定义另外一个macrodef test2 也有一个参数,最好不要再叫one了,不然容易出现混乱


posted @ 2009-05-18 17:50  yiling  阅读(2822)  评论(0编辑  收藏  举报