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>