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)-但最好不要依賴這一特性。
? 可選地,設定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也接受這種類型的引用。

4.1 File(Directory)類
4.1.1 Mkdir
n 創建一個目錄,如果他的父目錄不存在,也會被同時創建。
n 例子:
<mkdir dir="build/classes"/>
n 說明: 如果build不存在,也會被同時創建
4.1.2 Copy
n 拷貝一個(組)文件、目錄
n 例子:
1. 拷貝單個的文件:
<copy file="myfile.txt" tofile="mycopy.txt"/>
2. 拷貝單個的文件到指定目錄下
<copy file="myfile.txt" todir="../some/other/dir"/>
3. 拷貝一個目錄到另外一個目錄下
  <copy todir="../new/dir">
    <fileset dir="src_dir"/>
  </copy>
4. 拷貝一批文件到指定目錄下
  <copy todir="../dest/dir">
    <fileset dir="src_dir">
      <exclude name="**/*.java"/>
    </fileset>
  </copy>

  <copy todir="../dest/dir">
    <fileset dir="src_dir" excludes="**/*.java"/>
  </copy>
5. 拷貝一批文件到指定目錄下,將文件名后增加。Bak后綴
  <copy todir="../backup/dir">
    <fileset dir="src_dir"/>
    <mapper type="glob" from="*" to="*.bak"/>
  </copy>
6. 拷貝一組文件到指定目錄下,替換其中的@標簽@內容
  <copy todir="../backup/dir">
    <fileset dir="src_dir"/>
    <filterset>
      <filter token="TITLE" value="Foo Bar"/>
    </filterset>
  </copy>
4.1.3 Delete
n 刪除一個(組)文件或者目錄
n 例子
1. 刪除一個文件
<delete file="/lib/ant.jar"/>
2. 刪除指定目錄及其子目錄
  <delete dir="lib"/>
3. 刪除指定的一組文件
  <delete>
    <fileset dir="." includes="**/*.bak"/>
  </delete>
4. 刪除指定目錄及其子目錄,包括他自己
  <delete includeEmptyDirs="true">
    <fileset dir="build"/>
  </delete>
4.1.4 Move
n 移動或重命名一個(組)文件、目錄
n 例子:
1. 移動或重命名一個文件
<move file="file.orig" tofile="file.moved"/>
2. 移動或重命名一個文件到另一個文件夾下面
<move file="file.orig" todir="dir/to/move/to"/>
3. 將一個目錄移到另外一個目錄下
<move todir="new/dir/to/move/to">
<fileset dir="src/dir"/>
</move>
4. 將一組文件移動到另外的目錄下
<move todir="some/new/dir">
<fileset dir="my/src/dir">
<include name="**/*.jar"/>
<exclude name="**/ant.jar"/>
</fileset>
</move>
5. 移動文件過程中增加。Bak后綴
<move todir="my/src/dir">
<fileset dir="my/src/dir">
<exclude name="**/*.bak"/>
</fileset>
<mapper type="glob" from="*" to="*.bak"/>
</move>



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1245806



久久不醉