Loop
The 'loop' task required ant-contrib package, please download from
http://ncu.dl.sourceforge.net/project/ant-contrib/ant-contrib/1.0b3/ant-contrib-1.0b3-bin.zip
Loop all .txt file in a folder, and take action on each file
<taskdef name="for" classname="net.sf.antcontrib.logic.ForTask" classpath="${lib.dir}/lib/ant-contrib-1.0b3.jar" />
<target name="loopfolder">
<for param="filename">
<path>=>
<fileset dir="${folder}" excludes="test.txt" includes="*.txt" />
</path>
<sequential>
<echo message="@{filename}"/>
</sequential>
</for>
</target>
Call Another Target
<taskdef name="for" classname="net.sf.antcontrib.logic.ForTask" classpath="${lib.dir}/lib/ant-contrib-1.0b3.jar" />
<target name="loopfolder">
<for param="filename">
<path>=>
<fileset dir="${folder}" excludes="test.txt" includes="*.txt" />
</path>
<sequential>
<echo message="@{filename}"/>
<antcall target="fileaction"/>
</sequential>
</for>
</target>
<target name="fileaction">
<property name="infile" value="${data.dir}/demo.txt"/>
<echo message="${infile}"/>
</target>
Call Another Target with parameters passing
<taskdef name="for" classname="net.sf.antcontrib.logic.ForTask" classpath="${lib.dir}/lib/ant-contrib-1.0b3.jar" />
<target name="loopfolder">
<for param="filename">
<path>=>
<fileset dir="${folder}" excludes="test.txt" includes="*.txt" />
</path>
<sequential>
<echo message="@{filename}"/>
<antcall target="fileaction">
<param name="infile" value="@{filename}"/>
</antcall>
</sequential>
</for>
</target>
<target name="fileaction">
<property name="infile" value="${data.dir}/demo.txt"/>
<echo message="${infile}"/>
</target>
If-Then-Else
The 'if' task required ant-contrib package, please refer to 'Loop' task
<taskdef name="if" classname="net.sf.antcontrib.logic.IfTask" classpath="${lib.dir}/lib/ant-contrib-1.0b3.jar" />
<if>
<filesmatch file1="${infile}" file2="${outfile}"/>
<then>
<echo message="File ${infile} and file ${outfile} are same" />
</then>
<else>
<echo message="WARNING: File ${infile} and file ${outfile} are same" />
</else>
</if>
Failed a build
<taskdef name="if" classname="net.sf.antcontrib.logic.IfTask" classpath="${lib.dir}/lib/ant-contrib-1.0b3.jar" />
<if>
<filesmatch file1="${infile}" file2="${outfile}"/>
<then>
<echo message="File ${infile} and file ${outfile} are same" />
</then>
<else>
<fail message="Build failed because file ${infile} and file ${outfile} are not matched." />
</if>
ant.project.name
This is global project name, its value is define in 'project' element.
<project name="TestProject" default="compile" basedir=".">
<property name="yourprojectname" value="${ant.project.name}"/>
...</project>
本文介绍如何使用Ant脚本进行文件循环处理,并通过条件判断执行不同操作。包括安装所需组件、配置循环任务、调用其他目标以及传递参数等关键步骤。
3746

被折叠的 条评论
为什么被折叠?



