Ant: 使用外部的tasks

如何使用外部tasks

使用外部的tasks,做两步就行了:

  1. 把它们的实现放到Ant能找到的地方
  2. 声明它们

第一步让Ant找到外部的libraries有三种方法

 - 通过命令行参数 -lib
 - 放到 ${ant.home}/.ant/lib
 - 放到 ${ant.home}/lib

第二步声明有四种方法

1. 使用单独的指令来声明一个单独的task

<taskdef name="taskname" classname="ImplementationClass"/>
例如:

<taskdef name="for" classname="net.sf.antcontrib.logic.For" />

声明完后就可以<taskname ... /><for ... /> 来使用这个task了

2. 使用内容为一系列taskname-ImplementationClass对的properties-file来声明一些tasks

例如

<taskdef resource="net/sf/antcontrib/antcontrib.properties" />

声明完后就可以<taskname ... /><for ... /> 来使用其中的一个task了

假设antcontrib.jar 已经被放在${ant.home}/lib里,代码如下

<project default="printSth">
    <taskdef resource="net/sf/antcontrib/antcontrib.properties" />
    <property name="comm" value="print" />
    <target name="printSth">
        <if>
            <equals arg1="print" arg2="${comm}"/>
            <then>
                <echo>print sucessfully</echo>
            </then>
        </if>
    </target>
</project>

运行结果
这里写图片描述

如果没有放到${ant.home}/lib里,还要指定classpath

<taskdef resource="net/sf/antcontrib/antcontrib.properties">
    <classpath>
        <pathelement location="C:\Users\kwang\Desktop\tmp\ant-contrib-0.6.jar"/>
    </classpath>
</taskdef>

3. 使用内容为一系列<taskdef name="taskname" classname="ImplementationClass"/>的xml-file来声明一些tasks

例如

<taskdef resource="net/sf/antcontrib/antlib.xml" />

声明完后就可以<taskname ... /><for ... /> 来使用其中的一个task了

4. 使用名字为antlib.xml的xml-file,XML-namespace 和 antlib协议来声明一些tasks

例如

<project xmlns:ac="antlib:net.sf.antcontrib"/>

声明完后就可以<ac:taskname ... /><ac:if ... /> 来使用这个task了。
代码

<project default="printSth" xmlns:ac="antlib:net.sf.antcontrib">
    <property name="comm" value="print" />
    <target name="printSth">
        <ac:if>
            <equals arg1="print" arg2="${comm}"/>
            <then>
                <echo>print sucessfully</echo>
            </then>
        </ac:if>
    </target>
</project>

注:xmlns:ac 就是XML-namespace,ac是自己命名的,也可以是别的。
运行结果与本文章之前提到的结果一样

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值