<?xml version="1.0" encoding="UTF-8"?> <project name="HelloWorld" default="run" basedir="."> <!-- global vars --> <property name="src" value="src"/> <property name="dest" value="classes"/> <property name="hello_jar" value="hello1.jar"/> <!-- prepare deploying dir structure --> <target name="init"> <mkdir dir="${dest}"/> </target> <target name="compile" depends="init"> <javac srcdir="${src}" destdir="${dest}"/> </target> <target name="build" depends="compile"> <jar jarfile="${hello_jar}" basedir="${dest}"/> </target> <target name="run" depends="build"> <java classname="test.ant.HelloWorld" classpath="${hello_jar}"/> </target> <target name="clean"> <delete dir="${dest}"/> <delete dir="${hello_jar}"/> </target> <!-- demonstrate target dependencies --> <target name="rerun" depends="clean,run"> <ant target="clean"/> <ant target="run"/> </target> </project>
● 每个buildfile包含一个project和N个target(N≥1)。
● 每个target包含多个task元素。
● 每个task元素有一个唯一的ID。<taskname id="taskID" ... />
<project name="Demo" default="dist" basedir="."> <!-- name default basedir 都是project的attribute --> <description> This buildfile is used to build the Foo subproject within the large, complex Demo project. </description> <!-- set global properties for this build --> ......
● 从Ant 1.6开始,所有的task都可以在target外面进行声明。也就是说,在任何target还没有执行前,这些task就已经计算过了。所以,如果脚本写得不仔细的话,有可能出现无限循环。比如使用了antcall。
● 给target加上description,那么用projecthelp时,就会把该target作为public target而列给用户看。
<target name="cpy" depends="init" description="copy all res to working dir.">
● filtering copy
<filter token="year" value="2000"/> <copy todir="${dest}" filtering="true"> <fileset dir="${src.dir}"/> </copy>
● target运行条件if/unless如果要检查多项,那么可以把这些东西统一到一个独立的target里:
<target name="target.check"> <condition property="foobar.exist"> <and> <available file="${src}/foo.txt"/> <available file="${src}/bar.txt"/> </and> </condition> </target> <target name="run" depends="target.check" if="foobar.exist"> <echo>Files foo.txt and bar.txt are present.</echo> </target>