Ant build文件结构的主体是<project>,下面包含至少一个<target>,Target包含task元素,每个task元素可以有一个id属性,可以通过这个id来引用到。
Project
project中有三个属性:
name:项目的名字
default: 当没有指定target时,默认执行的target的名字
basedir:build文件中参考的根目录,如果没有指定绝对路径,那么根目录以basedir为准
Targets
一个target可以依赖与其它的target,你可能会有一个target用于编译,一个target用于distributable,你只能先编译才能distributable,因此distributable依赖于编译。
Tasks
一个task是可以执行的代码块。
一个task可以有多个属性或参数,属性的值作为配置的一个参考,这些参考在task执行之前解析。
Tasks有一个通用的结构:
<name attribute1="value1"attribute2="value2" ... />
这里的name是task的名称,attaributeN 是属性的名字,valueN是属性的值。
buildfile简单示例
<projectname="MyProject" default="dist" basedir=".">
<description>
simple example build file
</description>
<!-- set global properties for this build-->
<property name="src"location="src"/>
<property name="build"location="build"/>
<property name="dist"location="dist"/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directorystructure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="compile"depends="init"
description="compile thesource">
<!-- Compile the java code from ${src}into ${build} -->
<javac srcdir="${src}"destdir="${build}"/>
</target>
<target name="dist"depends="compile"
description="generate thedistribution">
<!-- Create the distribution directory-->
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into theMyProject-${DSTAMP}.jar file -->
<jarjarfile="${dist}/lib/MyProject-${DSTAMP}.jar"basedir="${build}"/>
</target>
<target name="clean"
description="clean up">
<!-- Delete the ${build} and ${dist}directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
</project>
具体各元素的属性和含义请参考:http://ant.apache.org/manual/index.html