Ant类型之Antlib

一、概述

        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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值