您的位置:首页 > 其它

Ant内置任务之macrodef

2018-03-02 17:52 435 查看

一、概述

macrodef是Ant内置任务,使用sequential嵌套元素作为模版来定义新的任务。嵌套attribute和element元素来定义新任务的属性和元素。当新任务运行时,这些属性会代入到sequential任务中。

二、属性

name:新定义的名字。

uri:存有定义的uri。

description:对macrodef的描述

backtrace:ant1.7起,当运行宏检测到错误时,如果设置为true,会进行错误回溯,否则不会。默认为true。

嵌套元素:

attribute:定义新任务的属性,属性的值将被替换到模版任务中。如果attribute属性不设置默认值,那么就是必需的属性。attribute通过使用类似Ant属性符号的表示法@{attributename}的方式放置到模版任务的主体中。@@用于转义@,与属性的$$类似,使用@@{x}会将@{x}替换到模版任务中,而不替换x的值。attribute具有以下属性:

name:新属性的名字,不区分大小写。

default:属性的默认值。

description:ant1.6.1起,属性的描述。

doubleexpanding:ant1.8.3起,属性中的属性引用是否扩展两次或只是一次。默认为true,也就是说如果传入attribute的属性中包含${},则会在@{}之前先进行一次扩展,然后才会传入@{}中再次扩展。

element:定义新任务的嵌套元素,任务实例的嵌套元素的内容会被放置到模版任务的标记名称处。element具有以下属性:

name:元素名,不区分大小写。

optional:嵌套元素是否是可选的,如果为true则为可选的,默认为false。

implicit:ant1.6.2起,嵌套元素是否是隐式的,如果为true则为隐式的,默认为false。如果为true,那么所有macrodef中嵌套的元素都会被放置到模版任务中这个隐式嵌套元素所在处。使用隐式元素时,macrodef中只能定义一个element元素。

description:ant1.6.1起,元素内容的描述。

text:ant1.6.1起,用于指定宏调用文本内容的处理。如果text元素不存在,那么宏调用时任何嵌套的文本都会产生一个错误。如果macrodef嵌套了text元素,那么text元素的名字会做为宏调用时嵌套文本的属性名。

name:文本属性名。

optional:嵌套文本是否是可选的,如果为true则为可选的,默认为false。

trim:如果为true,嵌套文本会去空格,默认为false。

description:嵌套文本的描述。

三、简单示例

<project>
<property name="param1" value="pv"/>

<macrodef name="test" >
<attribute name="attr1" doubleexpanding="false"/>
<attribute name="attr2" default="value2" />
<element name="dosomething" optional="true"  implicit="true"/>
<text name="nestedtext" optional="true"/>
<sequential>
<echo >attr1 with doubleexpanding false is @{attr1}</echo>
<dosomething/>
<echo >attr2 with doubleexpanding true is @{attr2}</echo>
<echo>nestedtext is @{nestedtext}</echo>
<echo>=======================</echo>
</sequential>
</macrodef>

<macrodef name="test1" >
<attribute name="attr1" doubleexpanding="false"/>
<attribute name="attr2" default="value2" />
<element name="dosomething" optional="true" />
<element name="doanotherthing" optional="true" />
<text name="nestedtext" optional="true" trim="true"/>
<sequential>
<echo >attr1 with doubleexpanding false is @{attr1}</echo>
<dosomething/>
<echo >attr2 with doubleexpanding true is @{attr2}</echo>
<doanotherthing/>
<echo>nestedtext is @{nestedtext}</echo>
<echo>=======================</echo>
</sequential>
</macrodef>

<test attr1="$${param1}" attr2="$${param1}" />
<test attr1="$${param1}" >
<echo>do something...</echo>
</test>
<test1 attr1="$${param1}" >
<dosomething>
<echo>do something...</echo>
</dosomething>
<doanotherthing>
<echo>do doanotherthing...</echo>
</doanotherthing>
nested content
</test1>

</project>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息