1? Ant
是什么?
Ant
是一種基于
Java
和
XML
的
build
工具。
2
下載、安裝
Ant
安裝
Ant
下載
.zip
文件,解壓縮到
c:\ant1.3(
后面引用為
%ANT_HOME%)
2.1
在你運行
Ant
之前需要做一些配置工作。
·
將
bin
目錄加入
PATH
環(huán)境變量。
·
設(shè)定
ANT_HOME
環(huán)境變量,指向你安裝
Ant
的目錄。在一些
OS
上,
Ant
的腳本可以猜測
ANT_HOME
(
Unix
和
Windos NT/2000
)-但最好不要依賴這一特性。
·
可選地,設(shè)定
J***A_HOME
環(huán)境變量(參考下面的高級小節(jié)),該變量應(yīng)該指向你安裝
JDK
的目錄。
注意:不要將
Ant
的
ant.jar
文件放到
JDK/JRE
的
lib/ext
目錄下。
Ant
是個應(yīng)用程序,而
lib/ext
目錄是為
JDK
擴(kuò)展使用的(如
JCE
,
JSSE
擴(kuò)展)。而且通過擴(kuò)展裝入的類會有安全方面的限制。
2.2
運行
Ant
運行
Ant
非常簡單,當(dāng)你正確地安裝
Ant
后,只要輸入
ant
就可以了。
n
沒有指定任何參數(shù)時,
Ant
會在當(dāng)前目錄下查詢
build.xml
文件。如果找到了就用該文件作為
buildfile
。如果你用
-find
選項。
Ant
就會在上級目錄中尋找
buildfile
,直至到達(dá)文件系統(tǒng)的根。要想讓
Ant
使用其他的
buildfile
,可以用參數(shù)
-buildfile file
,這里
file
指定了你想使用的
buildfile
。
n
可以指定執(zhí)行一個或多個
target
。當(dāng)省略
target
時,
Ant
使用標(biāo)簽
<project>
的
default
屬性所指定的
target
。
命令行選項總結(jié):
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
使用當(dāng)前目錄下的
build.xml
運行
Ant
,執(zhí)行缺省的
target
。
ant -buildfile test.xml
使用當(dāng)前目錄下的
test.xml
運行
Ant
,執(zhí)行缺省的
target
。
ant -buildfile test.xml dist
使用當(dāng)前目錄下的
test.xml
運行
Ant
,執(zhí)行一個叫做
dist
的
target
。
ant -buildfile test.xml -Dbuild=build/classes dist
使用當(dāng)前目錄下的
test.xml
運行
Ant
,執(zhí)行一個叫做
dist
的
target
,并設(shè)定
build
屬性的值為
build/classes
。
3
編寫
build.xml
Ant
的
buildfile
是用
XML
寫的。每個
buildfile
含有一個
project
。
buildfile
中每個
task
元素可以有一個
id
屬性,可以用這個
id
值引用指定的任務(wù)。這個值必須是唯一的。(詳情請參考下面的
Task
小節(jié))
3.1 Projects
project
有下面的屬性:
Attribute Description Required
name
項目名稱
. No
default
當(dāng)沒有指定
target
時使用的缺省
target Yes
basedir
用于計算所有其他路徑的基路徑。該屬性可以被
basedir property
覆蓋。當(dāng)覆蓋時,該屬性被忽略。如果屬性和
basedir property
都沒有設(shè)定,就使用
buildfile
文件的父目錄。
No
項目的描述以一個頂級的
<description>
元素的形式出現(xiàn)(參看
description
小節(jié))。
一個項目可以定義一個或多個
target
。一個
target
是一系列你想要執(zhí)行的。執(zhí)行
Ant
時,你可以選擇執(zhí)行那個
target
。當(dāng)沒有給定
target
時,使用
project
的
default
屬性所確定的
target
。
3.2 Targets
一個
target
可以依賴于其他的
target
。例如,你可能會有一個
target
用于編譯程序,一個
target
用于生成可執(zhí)行文件。你在生成可執(zhí)行文件之前必須先編譯通過,所以生成可執(zhí)行文件的
target
依賴于編譯
target
。
Ant
會處理這種依賴關(guān)系。
然而,應(yīng)當(dāng)注意到,
Ant
的
depends
屬性只指定了
target
應(yīng)該被執(zhí)行的順序-如果被依賴的
target
無法運行,這種
depends
對于指定了依賴關(guān)系的
target
就沒有影響。
Ant
會依照
depends
屬性中
target
出現(xiàn)的順序(從左到右)依次執(zhí)行每個
target
。然而,要記住的是只要某個
target
依賴于一個
target
,后者就會被先執(zhí)行。
<target name="A"/>
<target name="B" depends="A"/>
<target name="C" depends="B"/>
<target name="D" depends="C,B,A"/>
假定我們要執(zhí)行
target D
。從它的依賴屬性來看,你可能認(rèn)為先執(zhí)行
C
,然后
B
,最后
A
被執(zhí)行。錯了,
C
依賴于
B
,
B
依賴于
A
,所以先執(zhí)行
A
,然后
B
,然后
C
,最后
D
被執(zhí)行。
一個
target
只能被執(zhí)行一次,即時有多個
target
依賴于它(看上面的例子)。
如果(或如果不)某些屬性被設(shè)定,才執(zhí)行某個
target
。這樣,允許根據(jù)系統(tǒng)的狀態(tài)(
java version, OS,
命令行屬性定義等等)來更好地控制
build
的過程。要想讓一個
target
這樣做,你就應(yīng)該在
target
元素中,加入
if
(或
unless
)屬性,帶上
target
因該有所判斷的屬性。例如:
<target name="build-module-A" if="module-A-present"/>
<target name="build-own-fake-module-A" unless="module-A-present"/>
如果沒有
if
或
unless
屬性,
target
總會被執(zhí)行。
可選的
description
屬性可用來提供關(guān)于
target
的一行描述,這些描述可由
-projecthelp
命令行選項輸出。
將你的
tstamp task
在一個所謂的初始化
target
是很好的做法,其他的
target
依賴這個初始化
target
。要確保初始化
target
是出現(xiàn)在其他
target
依賴表中的第一個
target
。在本手冊中大多數(shù)的初始化
target
的名字是