1? Ant
是什么?
Ant
是一種基于
Java
和
XML
的
build
工具。
2
下載、安裝
Ant
安裝
Ant
下載
.zip
文件,解壓縮到
c:\ant1.3(
后面引用為
%ANT_HOME%)
2.1
在你運行
Ant
之前需要做一些配置工作。
·
將
bin
目錄加入
PATH
環境變量。
·
設定
ANT_HOME
環境變量,指向你安裝
Ant
的目錄。在一些
OS
上,
Ant
的腳本可以猜測
ANT_HOME
(
Unix
和
Windos NT/2000
)-但最好不要依賴這一特性。
·
可選地,設定
J***A_HOME
環境變量(參考下面的高級小節),該變量應該指向你安裝
JDK
的目錄。
注意:不要將
Ant
的
ant.jar
文件放到
JDK/JRE
的
lib/ext
目錄下。
Ant
是個應用程序,而
lib/ext
目錄是為
JDK
擴展使用的(如
JCE
,
JSSE
擴展)。而且通過擴展裝入的類會有安全方面的限制。
2.2
運行
Ant
運行
Ant
非常簡單,當你正確地安裝
Ant
后,只要輸入
ant
就可以了。
n
沒有指定任何參數時,
Ant
會在當前目錄下查詢
build.xml
文件。如果找到了就用該文件作為
buildfile
。如果你用
-find
選項。
Ant
就會在上級目錄中尋找
buildfile
,直至到達文件系統的根。要想讓
Ant
使用其他的
buildfile
,可以用參數
-buildfile file
,這里
file
指定了你想使用的
buildfile
。
n
可以指定執行一個或多個
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
,執行缺省的
target
。
ant -buildfile test.xml
使用當前目錄下的
test.xml
運行
Ant
,執行缺省的
target
。
ant -buildfile test.xml dist
使用當前目錄下的
test.xml
運行
Ant
,執行一個叫做
dist
的
target
。
ant -buildfile test.xml -Dbuild=build/classes dist
使用當前目錄下的
test.xml
運行
Ant
,執行一個叫做
dist
的
target
,并設定
build
屬性的值為
build/classes
。
3
編寫
build.xml
Ant
的
buildfile
是用
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
時,使用
project
的
default
屬性所確定的
target
。
3.2 Targets
一個
target
可以依賴于其他的
target
。例如,你可能會有一個
target
用于編譯程序,一個
target
用于生成可執行文件。你在生成可執行文件之前必須先編譯通過,所以生成可執行文件的
target
依賴于編譯
target
。
Ant
會處理這種依賴關系。
然而,應當注意到,
Ant
的
depends
屬性只指定了
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
依賴于
B
,
B
依賴于
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"/>
如果沒有
if
或
unless
屬性,
target
總會被執行。
可選的
description
屬性可用來提供關于
target
的一行描述,這些描述可由
-projecthelp
命令行選項輸出。
將你的
tstamp task
在一個所謂的初始化
target
是很好的做法,其他的
target
依賴這個初始化
target
。要確保初始化
target
是出現在其他
target
依賴表中的第一個
target
。在本手冊中大多數的初始化
target
的名字是