前言
Apache Ant,是一個將軟件編譯、測試、部署等步驟聯繫在一起加以自動化的一個工具,大多用於Java環境中的軟件開發。由Apache軟件基金會所提供。
Ant是純Java語言編寫的,所以具有很好的跨平臺性。
1 下載和安裝
1.1 下載
ant的官方下載地址:http://ant.apache.org/bindownload.cgi
進入頁面後,在下圖的紅色方框中可以下載最新版本。筆者下載的版本是 apache-ant-1.9.4。
1.2 配置環境變量
配置環境變量(我的電腦 -> 屬性 -> 高級 -> 環境變量)。
設置ant環境變量:
path C:/ apache-ant-1.9.4/bin
classpath C:/apache-ant-1.9.4/lib
1.3 驗證
執行構建文件
輸入如下命令:ant
如果出現如下內容,說明安裝成功:
Buildfile: build.xml does not exist!注意:因爲ant默認運行build.xml文件,這個文件需要我們創建。
Build failed
如果不想命名爲build.xml,運行時可以使用 ant -buildfile test.xml 命令指明要運行的構建文件。
輸入 ant -version,可以查看版本信息。
但如果出現 'ant' 不是內部或外部命令,也不是可運行的程序或批處理文件
說明安裝失敗:(可以重複前述步驟,直至安裝成功。)
2 例子
在安裝和配置成功後,我們就可以使用ant了。
爲了讓讀者對ant有一個直觀的認識,首先以Ant官方手冊上的一個簡單例子做一個說明。
以下是一個build.xml文件的內容:
<description>
simple example build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="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"
description="compile the source " >
<!-- Compile the java code from srcinto{build} -->
<javac srcdir="${src}" destdir="${build}"/>
</target>
<target name="dist" depends="compile"
description="generate the distribution" >
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib"/>
<!-- Put everything in buildintotheMyProject−{DSTAMP}.jar file -->
<jar jarfile="dist/lib/MyProject−{DSTAMP}.jar" basedir="${build}"/>
</target>
<target name="clean"
description="clean up" >
<!-- Delete the buildand{dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
</project>
在這個xml文件中,有幾個target標籤,每個target對應一個執行目標。
我們將這個build.xml放在 D:\Temp\ant_test 路徑下,然後在dos界面下進行測試。
ant init
在 D:\Temp\ant_test 路徑下創建了一個build目錄,執行成功。
ant compile
提示錯誤,原來是在build.xml的所在目錄下找不到src目錄。好的,我們直接創建一個src目錄,然後再次嘗試。
這次,執行成功。
ant dist
一個細節
細心的讀者,想必已經發現一個問題——在執行 ant compile 和 ant dist 命令的時候把前面的命令也執行了。這是爲什麼呢?
請留意一下build.xml中的內容。有部分 target 標籤中含有 depends 關鍵字。
3 關鍵元素
Ant的構件文件都是XML格式的。每個構件文件包含一個project元素和至少一個target。
target元素可以包含多個task元素。
3.1 Project 元素
project 元素是構建文件的根元素。
一個 project 元素可以有多個 target 元素,一個 target 元素可以有多個 task。
在上節的例子中,project標籤裏有三個屬性。
name屬性,指示 project 元素的名字。例子中的名字就是 MyProject。
default屬性,指示這個 project 默認執行的 target。在本文的例子中,默認執行的 target 爲 dist。
如果我們輸入命令 ant 時,不指定 target 參數,默認會執行 dist 這個 target。
basedir屬性,指定根路徑的位置。該屬性沒有指定時,使用Ant的構件文件的所在目錄作爲根目錄。
3.2 Target 元素
target 元素是 task 的容器,也就是 Ant 的一個基本執行單元。
以上節例子中的 compile 來舉例。
<!-- Compile the java code from srcinto{build} -->
<javac srcdir="${src}" destdir="${build}"/>
</target>
name屬性,指示target元素的名稱。
這個屬性在一個project元素中必須是唯一的。這很好理解,如果出現重複,Ant就不知道具體該執行哪個 target 了。
depends屬性,指示依賴的 target,當前的 target 必須在依賴的 target 之後執行。
description屬性,是關於 target 的簡短說明。
此外,還有其他幾個未出現在構建文件中的屬性。
unless屬性,正好和 if屬性相反,驗證指定的屬性是否存在,若存在,所在target將不會被執行。
extensionOf屬性,添加當前 target 到 extension-point 依賴列表。——Ant1.8.0新特性。
請看以下實例:extension-point 元素和 target 元素十分類似,都可以指定依賴的target。但是不同的是,extension-point 中不能包含任何 task。
...
</target>
<extension-point name="ready-to-compile" depends="create-directory-layout"/>
<target name="compile" depends="ready-to-compile">
...
</target>
調用target順序: create-directory-layout --> 'empty slot' --> compile
...
</target>
3.3 Task 元素
task是一段可以被執行的代碼。
一個task可以有多個屬性, 一個屬性可以包含對一個 property 的引用。
task的通常結構爲
其中,name 是 task 的名字, attributeN 是屬性名, valueN 是這個屬性的值。
還是以 compile 做爲例子:
<!-- Compile the java code from srcinto{build} -->
<javac srcdir="${src}" destdir="${build}"/>
</target>
在 compile 這個 target 標籤中包含了一個任務。
這個任務的動作是:執行JAVA編譯,編譯src下的代碼,並把編譯生成的文件放在build目錄中。
常用task
<arg value="-h" />
<classpath>
<pathelement location="dist/test.jar" />
</classpath>
<echo>You are using version ${java.version} of Java! This message spans two lines.</echo>
<fileset dir="${srcdir}">
<include name="**/*.java"/>
</fileset>
<filterset>
<filter token="VERSION" value="${app.version}"/>
</filterset>
<delete dir="lib" />
<delete>
<fileset dir="." includes="**/*.bak" />
<fileset dir="my/src/dir">
<include name="**/*.jar" />
<exclude name="**/ant.jar" />
</fileset>
3.4 Property 元素
project的屬性可以通過property元素來設定,也可在Ant之外設定。若要在外部引入某文件,例如build.properties文件,可以通過如下內容將其引入:<property file=” build.properties”/>。
property元素可用作 task 的屬性值。在task中是通過將屬性名放在“${”和“}”之間,並放在task屬性值的位置來實現的。
例如 complile 例子中,使用了前面定義的 src 作爲源目錄。
Ant提供了一些內置的屬性,它能得到的系統屬性的列表與Java文檔中System.getPropertis()方法得到的屬性一致,這些系統屬性可參考sun網站的說明。
3.5 extension-point元素
和 target 元素十分類似,都可以指定依賴的target。但是不同的是,extension-point 中不能包含任何 task。——Ant1.8.0新增特性。在 target元素中的例子裏已提到過,不再贅述。
參考資料
[1] ant官方手冊:http://ant.apache.org/manual/index.html