Ant核心任务
1.ant:执行多个构件文件
Ant 任务包括antfile、dir、target、output、inheritAll、inheritRefs
编写projectA.xml构件文件如下:
<project name="projectA" default="callProjectB">
<target name="callProjectB">
<echo message="In projectA calling projectB"/>
<ant antfile="subfile/projectB.xml" target="target2"/>
</target>
</project>
被调用的projectB.xml构件文件的内容如下:
<project name="projectB" default="init">
<target name="init">
<echo message="In projectB"/>
</target>
<target name="target2">
<echo message="In projectB,execute target2"/>
</target>
</project>
inheritAll属性指定被调用的构件文件可以使用当前构件文件中的属性,默认为true
<ant antfile="subfile/projectB.xml" inheritAll="true"/>
inheritRefs属性指定被调用的构件文件可以引用当前构件中的reference任务
<path id="path1">...</path>
<ant antfile="subfile/projectB.xml" inheritRefs="true">
<reference refid="path1" torefid="path2"/>
</ant>
把当前构件文件中的path1属性传递给被调用的构件中使用,
在被调用project中通过path2引用这个属性
----------------------------------------------------------
2.antcall:执行过程中调用并执行其他target
<project name="antcallSample" default="targetA">
<target name="init">
<echo message="In init target"/>
</target>
<target name="targetA">
<property name="prop" value="prop property"/>
<antcall target="targetB">
<param name="path1" value="path1 param"/>
</antcall>
<echo message="After call targetB"/>
</target>
<target name="targetB" depends="init">
<echo message="In targetB"/>
<echo message="path1=${path1}"/>
<echo message="prop=${prop}"/>
</target>
</project>
----------------------------------------------------------
3.apply/execon:通过Ant工具执行系统命令
----------------------------------------------------------
4.chmod:改变Linux/UNIX系统的文件权限
----------------------------------------------------------
5.copy:对文件和目录进行复制
<copy file="myfile.txt" tofile="mycopy.txt"/>
在当前目录复制myfile.txt,并把复制后的文件命名为mycopy.txt
<copy file="myfile.txt" todir="../some/other/dir"/>
把当前目录下的myfile.txt到与当前目录同级的some目录的/other/dir子目录下
<copy todir="../new/dir">
<fileset dir="src_dir">
<exclude name="/*/*//*.java"/>
</fileset>
<globmapper from="/*" to=".bak"/>
</copy>
把src_dir目录及其子目录下的所有非Java文件复制到../backup/dir目录下,并重命名为bak文件
<copy todir="../backup/dir">
<fileset dir="src_dir"/>
<filterset>
<filter token="TITLE" value="Foo Bar"/>
</filterset>
</copy>
把src_dir目录下的所有文件复制到../backup/dir目录,并在所有文件中查找并替换@TITLE@为Foo Bar
----------------------------------------------------------
6.delete:对文件和目录进行删除
<delete file="/lib/ant.jar"/>
<delete dir="lib"/>
<delete includeEmptyDirs="true">
<fileset dir="." includes="/*/*//*.bak"/>
</delete>
删除当前目录及其子目录下的所有.bak文件,同时删除所有空目录
----------------------------------------------------------
7.echo:输出系统信息
----------------------------------------------------------
8.mkdir:创建目录
<property name="dist" value="dist"/>
<mkdir dir="${dist}"/>
----------------------------------------------------------
9.move:移动文件和目录
<move file="file.orig" tofile="file.moved"/>
<move file="file.orig" todir="dir/to/move/to"/>
<move todir="new/dir/to/move/to">
<fileset dir="src/dir"/>
</move>
<move todir="my/src/dir" includeemptydirs="false">
<fileset dir="my/src/dir">
<exclude name="/*/*//*.bak"/>
</fileset>
<globmapper from="/*" to="/*"/>
</move>
把my/src/dir目录下的所有非.bak文件重命名为.bak的备份文件
----------------------------------------------------------
10. zip:创建zip文件
<zip destfile="${dist}/manual.zip" basedir="htdocs/manual" includes="api//*/*//*.html" excludes="/*/*/todo.html"/>
打包htdocs/manual目录下的文件,并命名为manual.zip。
在这个zip文件中只包含htdocs/manual/api目录或其子目录下的所有文件,但不包含其中文件名为todo.html的文件。
<zip destfile="${dist}/manual.zip">
<zipfileset dir="htdocs/manual" prefix="docs/user-guide"/>
<zipfileset dir="." prefix="ChangeLog27.txt" fullpath="docs/ChangeLog.txt"/>
<zipfileset src="example.zip" mce_src="example.zip" includes="/*/*//*.html" prefix="docs/examples"/>
</zip>
把htdocs/manual目录下的所有文件及其子目录打包到docs/user-guide目录下;
把当前目录下的ChangeLog27.txt文件打包为docs/ChangeLog.txt;
同时把example.zip下的所有.html文件打包到docs/examples下。
<zip destfile="${dist}/manual.zip">
<zipfileset dir="htdocs/manual" prefix="docs/user-guide"/>
<zipgroupfileset dir="." includes="example/*.zip"/>
</zip>
把htdocs/manual下的所有目录及文件打包到docs/user-guide下;
同时包含所有zip文件名为example/*.zip的文件。
----------------------------------------------------------
11.loadproperties:加载属性文件
copy.properties文件内容:
copy.src=src
copy.dest=dest
<project name="loadpropertiesSample" default="copyFile">
<loadproperties srcFile="copy.properties">
<filterchain>
<linecontains>
<contains value=".copy"/>
</linecontains>
</filterchain>
</loadproperties>
<target name="copyFile">
<copy todir="${copy.dest}">
<fileset dir="${copy.src}"/>
</copy>
</target>
</project>
----------------------------------------------------------
12.tstamp
<project name="timestamp" default="timestampTarget">
<tstamp>
<format property="TODAY_UK" pattern="d-MMMM-yyyy" locale="en"/>
</tstamp>
<tstamp>
<format property="TODAY_CN" pattern="d-MMMM-yyyy" locale="zh"/>
</tstamp>
<tstamp>
<format property="touch.time" pattern="MM/dd/yyyy hh:mm aa" offset="-5" unit="hour"/>
</tstamp>
<target name="timestampTarget">
<echo message="${DSTAMP}"/>
<echo message="${TODAY_UK}"/>
<echo message="${TODAY_CN}"/>
<echo message="${touch.time}"/>
</target>
</project>
通过${DSTAMP}获取Ant默认的日期格式。结果为20060708。
设定日期格式为d-MMMM-yyyy且使用英文语言。结果为8-July-2006。
设定日期格式为d-MMMM-yyyy且使用中文语言。结果为8-七月-2006。
设定日期格式为MM/dd/yyyy hh:mm aa,同时设定时间为当前时间减去5个小时。结果为07/08/2006 04:46 下午,执行时间21:46。