一、概述
Antlib是以antlib为根元素的xml文件。antlib的元素是Ant定义任务,比如taskdef任务或者其他任何继承org.apache.tools.ant.taskdefs.AntlibDefinition的ant任务。当前与Ant绑定的声明有:typedef、taskdef、macrodef、presetdef、scriptdef。可以在antlib文件中一起定义一组task和type。
二、简单示例
假设example.xml文件中进行了如下定义:
<?xml version="1.0"?>
<antlib>
<typedef name="fs" classname="org.apache.tools.ant.types.FileSet" />
<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>
</antlib>
可以通过如下方式在构建脚本中使用antlib文件:
<typedef file="example.xml"/>
还可以通过typedef的其他属性来使用antlib文件,假设example.xml在含有类文件的run.jar中。可以通过下面的方式将任务或类型定义,并将其存放到命名空间
uri中。
<typedef resource="com/run/ant/example.xml" uri="examples:/com.run.ant"/>
使用方法如下:
<project default="test" xmlns:exps="examples:/com.run.ant">
<property name="param1" value="pv"/>
<typedef file="example.xml" uri="examples:/com.run.ant"/>
<exps:fs id="files" dir=".." includes="*/*.txt" />
<echo>${toString:files}</echo>
<exps:test attr1="$${param1}" attr2="$${param1}" />
<exps:test attr1="$${param1}">
<echo>do something...</echo>
</exps:test>
</project>