ant-contrib 扩展ant 功能 for、foreach、if

ant使用for、if等功能,需要下载一个扩展包放到ant的lib目录。

1. 下载扩展包 ant-contrib-1.0b3-bin.zip

2. 解压后进入目录,有一个ant-contrib-1.0b3.jar,复制到${ant安装目录}/lib 下。

3. 编译脚本,ant -file ${filename} ${targetname} 执行 各个target

<?xml version="1.0" encoding="UTF-8" ?>
<project default="foreachtest" basedir="." >
        <property name="ant-contrib.jar" location="/usr/apache-ant/lib/ant-contrib-1.0b3.jar" />
        <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="${ant-contrib.jar}" />

<!-- ################ foreach test ########################## -->
        <target name="loop" >
                <echo> ${file.name} </echo>
        </target>

        <!-- 把paths中的文件名循环作为参数传给loop -->
        <target name="foreachtesta">
                <foreach list="" target="loop" param="file.name" delimiter=",">
                        <path location="./" >
                                <fileset dir="." includes="*" />
                        </path>
                </foreach>
        </target>
        <target name="foreachtestc">
                <!-- fileset is deprecated -->
                <foreach list="" target="loop" param="file.name" delimiter=",">
                        <fileset dir="." includes="*" />
                </foreach>
        </target>

        <!-- 把list中的数循环传给 number-->
        <target name="foreachtestb">
                <foreach list="1,2,3,6,5" target="number"  param="number" delimiter=",">
                </foreach>
        </target>

        <target name="number">
                <echo> ${number} </echo>
        </target>

<!-- ################ if test ########################## -->
        <property name="display" value="aa" />
        <target name="iftest">
                <if>
                        <equals arg1="aa" arg2="${display}" />
                        <then>
                                <echo>aa</echo>
                                <!-- 若符合判断条件,即条件成功,使用antcall命令来执行某个target -->
                                <antcall target="calltarget" />
                        </then>
                <elseif>
                        <equals arg1="bb" arg2="${display}" />
                        <then>
                                <echo>bb</echo>
                        </then>
                </elseif>
                <else>
                        <echo>else</echo>
                </else>
                </if>
        </target>

        <target name="calltarget">
                <echo>call</echo>
        </target>

<!-- ################ for test (未解决:failed to create task or type for) ########################### -->
        <target name="fortest">
                <for list="1,2" param="number"  >
                        <sequential>
                                <!-- <for> makes use of ant's macrodef task, so the @{} notation is used for parameter substition -->
                                <echo>@{number}</echo>
                        </sequential>
                        <!-- sequential 循环体,可写多个 -->
                        <sequential>
                                <echo>other@{number}</echo>
                        </sequential>
                </for>
        </target>
</project>


ant 官网

ant task api

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值