1 ant是什麼?
Apache
ant 是一個基於 Java的生成工具。生成工具在軟體發展中用來將源代碼和其他輸入檔轉換為可執行檔的形式(也有可能轉換為可安裝的產品映射形式)。隨著應用程式的生成過程變得更加複雜,確保在每次生成期間都使用精確相同的生成步驟,同時實現盡可能多的自動化,以便及時產生一致的生成版本
2
下載、安裝ant 安裝ant下載.zip檔,解壓縮到c:ant1.3(後面引用為%ant_HOME%)

2.1
在你運行ant之前需要做一些配置工作。
bin目錄加入PATH環境變數。
設定ant_HOME環境變數,指向你安裝ant的目錄。在一些OS上,ant的腳本可以猜測ant_HOMEUnixWindos NT/2000)-但最好不要依賴這一特性。
可選地,設定JAVA_HOME環境變數(參考下面的高級小節),該變數應該指向你安裝JDK的目錄。注意:不要將antant.jar檔放到JDK/JRElib/ext目錄下。ant是個應用程式,而lib/ext目錄是為JDK擴展使用的(如JCEJSSE擴展)。而且通過擴展裝入的類會有安全方面的限制。
2.2
運行ant
運行ant非常簡單,當你正確地安裝ant後,只要輸入ant就可以了。
沒有指定任何參數時,ant會在當前目錄下查詢build.xml文件。如果找到了就用該檔作為buildfile。如果你用 -find 選項。ant就會在上級目錄中尋找buildfile,直至到達檔系統的根。要想讓ant使用其他的buildfile,可以用參數 -buildfile file,這裏file指定了你想使用的buildfile
可以指定執行一個或多個target。當省略target時,ant使用標籤<project>default屬性所指定的target

命令行選項總結:ant [options] [target [target2 [target3] ...]]
Options:
-help print this message
-projecthelp print project help information
-version print the version information and exit
-quiet be extra quiet
-verbose be extra verbose
-debug print debugging information
-emacs produce logging information without adornments
-logfile file use given file for log output
-logger classname the class that is to perform logging
-listener classname add an instance of class as a project listener
-buildfile file use specified buildfile
-find file search for buildfile towards the root of the filesystem and use the first one found
-Dproperty=value set property to value
例子ant使用當前目錄下的build.xml運行ant,執行缺省的targetant -buildfile test.xml使用當前目錄下的test.xml運行ant,執行缺省的targetant -buildfile test.xml dist使用當前目錄下的test.xml運行ant,執行一個叫做disttargetant -buildfile test.xml -Dbuild=build/classes dist使用當前目錄下的test.xml運行ant,執行一個叫做disttarget,並設定build屬性的值為build/classes

3
編寫build.xml
antbuildfile是用XML寫的。每個buildfile含有一個project

buildfile
中每個task元素可以有一個id屬性,可以用這個id值引用指定的任務。這個值必須是唯一的。(詳情請參考下面的Task小節)

3.1 Projects

project
有下麵的屬性:
Attribute Description Required
name
項目名稱. No
default
當沒有指定target時使用的缺省target Yes
basedir
用於計算所有其他路徑的基路徑。該屬性可以被basedir property覆蓋。當覆蓋時,該屬性被忽略。如果屬性和basedir property都沒有設定,就使用buildfile檔的父目錄。 No專案的描述以一個頂級的<description>元素的形式出現(參看description小節)。
一個專案可以定義一個或多個target。一個target是一系列你想要執行的。執行ant時,你可以選擇執行那個target。當沒有給定target時,使用projectdefault屬性所確定的target

3.2 Targets
一個target可以依賴於其他的target。例如,你可能會有一個target用於編譯程序,一個target用於生成可執行檔。你在生成可執行檔之前必須先編譯通過,所以生成可執行檔的target依賴於編譯targetant會處理這種依賴關係。
然而,應當注意到,antdepends屬性只指定了target應該被執行的順序-如果被依賴的target無法運行,這種depends對於指定了依賴關係的target就沒有影響。
ant會依照depends屬性中target出現的順序(從左到右)依次執行每個target。然而,要記住的是只要某個target依賴於一個target,後者就會被先執行。
<target name="A"/>
<target name="B" depends="A"/>
<target name="C" depends="B"/>
<target name="D" depends="C,B,A"/>
假定我們要執行target D。從它的依賴屬性來看,你可能認為先執行C,然後B,最後A被執行。錯了,C依賴於BB依賴於A,所以先執行A,然後B,然後C,最後D被執行。
一個target只能被執行一次,即時有多個target依賴於它(看上面的例子)。
如果(或如果不)某些屬性被設定,才執行某個target。這樣,允許根據系統的狀態(java version, OS, 命令行屬性定義等等)來更好地控制build的過程。要想讓一個target這樣做,你就應該在target元素中,加入if(或unless)屬性,帶上target因該有所判斷的屬性。例如:
<target name="build-module-A" if="module-A-present"/>
<target name="build-own-fake-module-A" unless="module-A-present"/>
如果沒有ifunless屬性,target總會被執行。
可選的description屬性可用來提供關於target的一行描述,這些描述可由-projecthelp命令行選項輸出。
將你的tstamp task在一個所謂的初始化target是很好的做法,其他的target依賴這個初始化target。要確保初始化target是出現在其他target依賴表中的第一個target。在本手冊中大多數的初始化target的名字是"init"

target
有下麵的屬性:
Attribute Description Required
name target
的名字 Yes
depends
用逗號分隔的target的名字列表,也就是依賴表。 No
if
執行target所需要設定的屬性名。 No
unless
執行target需要清除設定的屬性名。 No
description
關於target功能的簡短描述。 No

3.3 Tasks
一個task是一段可執行的代碼。
一個task可以有多個屬性(如果你願意的話,可以將其稱之為變數)。屬性只可能包含對property的引用。這些引用會在task執行前被解析。
下面是Task的一般構造形式:
<name attribute1="value1" attribute2="value2" ... />
這裏nametask的名字,attributeN是屬性名,valueN是屬性值。
有一套內置的(built-intask,以及一些可選task,但你也可以編寫自己的task
所有的task都有一個task名字屬性。ant用屬性值來產生日誌資訊。
可以給task賦一個id屬性:
<taskname id="taskID" ... />
這裏tasknametask的名字,而taskID是這個task的唯一識別字。通過這個識別字,你可以在腳本中引用相應的task。例如,在腳本中你可以這樣:
<script ... >
task1.setFoo("bar");
</script>
設定某個task實例的foo屬性。在另一個task中(用java編寫),你可以利用下面的語句存取相應的實例。
project.getReference("task1").
注意1:如果task1還沒有運行,就不會被生效(例如:不設定屬性),如果你在隨後配置它,你所作的一切都會被覆蓋。
注意2:未來的ant版本可能不會相容這裏所提的屬性,因為很有可能根本沒有task實例,只有proxies

3.4 Properties
一個project可以有很多的properties。可以在buildfile中用property task來設定,或在ant之外設定。一個property有一個名字和一個值。property可用於task的屬性值。這是通過將屬性名放在"${""}"之間並放在屬性值的位置來實現的。例如如果有一個property builddir的值是"build",這個property就可用於屬性值:${builddir}/classes。這個值就可被解析為build/classes
內置屬性
如果你使用了<property> task 定義了所有的系統屬性,ant允許你使用這些屬性。例如,${os.name}對應作業系統的名字。
要想得到系統屬性的列表可參考the Javadoc of System.getProperties
除了Java的系統屬性,ant還定義了一些自己的內置屬性:
basedir project
基目錄的絕對路徑 (<project>basedir屬性一樣)ant.file buildfile的絕對路徑。ant.version ant的版本。ant.project.name 當前執行的project的名字;由<project>name屬性設定.ant.java.version ant檢測到的JVM的版本; 目前的值有"1.1", "1.2", "1.3" and "1.4".
例子
<project name="MyProject" default="dist" basedir=".">

<!-- set global properties for this build -->
<property name="src" value="."/>
<property name="build" value="build"/>
<property name="dist" value="dist"/>

<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>

<target name="compile" depends="init">
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}"/>
</target>

<target name="dist" depends="compile">
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} in