使用Ant編譯Java工程(轉載)

 Ant是什麼? 
Ant是一種基於Java和XML的build工具。

2 下載、安裝Ant 
安裝Ant
下載.zip文件,解壓縮到c:/ant1.3(後面引用爲%ANT_HOME%)

2.1 在你運行Ant之前需要做一些配置工作。
2.1.1 將bin目錄加入PATH環境變量。 
2.1.2 設定ANT_HOME環境變量,指向你安裝Ant的目錄。在一些OS上,Ant的腳本可以猜測ANT_HOME(Unix和Windos NT/2000)-但最好不要依賴這一特性。 
2.1.3 可選地,設定JAVA_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的名字是"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" ... />
這裏name是task的名字,attributeN是屬性名,valueN是屬性值。

有一套內置的(built-in)task,以及一些可選task,但你也可以編寫自己的task。

所有的task都有一個task名字屬性。Ant用屬性值來產生日誌信息。

可以給task賦一個id屬性:
<taskname id="taskID" ... />
這裏taskname是task的名字,而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} into the MyProject-${DSTAMP}.jar file -->
<jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>
</target>

<target name="clean">
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>

</project>

3.5 Path-like Structures
你可以用":"和";"作爲分隔符,指定類似PATH和CLASSPATH的引用。Ant會把分隔符轉換爲當前系統所用的分隔符。

當需要指定類似路徑的值時,可以使用嵌套元素。一般的形式是
<classpath>
<pathelement path="${classpath}"/>
<pathelement location="lib/helper.jar"/>
</classpath>
location屬性指定了相對於project基目錄的一個文件和目錄,而path屬性接受逗號或分號分隔的一個位置列表。path屬性一般用作預定義的路徑--其他情況下,應該用多個location屬性。

爲簡潔起見,classpath標籤支持自己的path和location屬性。所以:
<classpath>
<pathelement path="${classpath}"/>
</classpath>
可以被簡寫作:
<classpath path="${classpath}"/>
也可通過<fileset>元素指定路徑。構成一個fileset的多個文件加入path-like structure的順序是未定的。
<classpath>
<pathelement path="${classpath}"/>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
<pathelement location="classes"/>
</classpath>
上面的例子構造了一個路徑值包括:${classpath}的路徑,跟着lib目錄下的所有jar文件,接着是classes目錄。

如果你想在多個task中使用相同的path-like structure,你可以用<path>元素定義他們(與target同級),然後通過id屬性引用--參考Referencs例子。

path-like structure可能包括對另一個path-like structurede的引用(通過嵌套<path>元素):
<path id="base.path">
<pathelement path="${classpath}"/>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
<pathelement location="classes"/>
</path>
<path id="tests.path">
<path refid="base.path"/>
<pathelement location="testclasses"/>
</path>
前面所提的關於<classpath>的簡潔寫法對於<path>也是有效的,如:
<path id="tests.path">
  <path refid="base.path"/>
<pathelement location="testclasses"/>
</path>
可寫成:
<path id="base.path" path="${classpath}"/>
命令行變量

有些task可接受參數,並將其傳遞給另一個進程。爲了能在變量中包含空格字符,可使用嵌套的arg元素。
Attribute Description Required
value 一個命令行變量;可包含空格字符。 只能用一個
line 空格分隔的命令行變量列表。 
file 作爲命令行變量的文件名;會被文件的絕對名替代。 
path 一個作爲單個命令行變量的path-like的字符串;或作爲分隔符,Ant會將其轉變爲特定平臺的分隔符。 

例子
<arg value="-l -a"/>
是一個含有空格的單個的命令行變量。
<arg line="-l -a"/>
是兩個空格分隔的命令行變量。
<arg path="/dir;/dir2:/dir3"/>
是一個命令行變量,其值在DOS系統上爲/dir;/dir2;/dir3;在Unix系統上爲/dir:/dir2:/dir3 。

References

buildfile元素的id屬性可用來引用這些元素。如果你需要一遍遍的複製相同的XML代碼塊,這一屬性就很有用--如多次使用<classpath>結構。

下面的例子:
<project ... >
<target ... >    
<rmic ...>      
<classpath>        
<pathelement location="lib/"/>        
<pathelement path="${java.class.path}/"/>        
<pathelement path="${additional.path}"/>      
</classpath>    
</rmic>  
</target>
<target ... >
<javac ...>
<classpath>
<pathelement location="lib/"/>
<pathelement path="${java.class.path}/"/>
<pathelement path="${additional.path}"/>
</classpath>
</javac>
</target>
</project>
可以寫成如下形式:
<project ... > 
<path id="project.class.path">  
<pathelement location="lib/"/>
<pathelement path="${java.class.path}/"/>   
<pathelement path="${additional.path}"/> 
</path>
<target ... >
<rmic ...>
<classpath refid="project.class.path"/>
</rmic>
</target>
<target ... > 
<javac ...>
<classpath refid="project.class.path"/>
</javac>
</target>
</project>
所有使用PatternSets, FileSets 或 path-like structures嵌套元素的task也接受這種類型的引用。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章