Ant全攻略

1.Ant是什麼?
Ant
是一種基於JavaXMLbuild工具。


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

2.1
在你運行Ant以前須要作一些配置工做。

·
bin目錄加入PATH環境變量。
·
設定ANT_HOME環境變量,指向你安裝Ant的目錄。在一些OS上,Ant的腳本能夠猜想ANT_HOMEUnixWindos NT/2000)-但最好不要依賴這一特性。

·
可選地,設定JAVA_HOME環境變量(參考下面的高級小節),該變量應該指向你安裝JDK的目錄。
注意:不要將Antant.jar文件放到JDK/JRElib/ext目錄下。Ant是個應用程序,而lib/ext目錄是爲JDK擴展使用的(如JCEJSSE擴展)。並且經過擴展裝入的類會有安全方面的限制。
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,執行一個叫作disttarget
ant -buildfile test.xml -Dbuild=build/classes dist
使用當前目錄下的test.xml運行Ant,執行一個叫作disttarget,並設定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時,使用projectdefault屬性所肯定的target

3.2 Targets

一個target能夠依賴於其餘的target。例如,你可能會有一個target用於編譯程序,一個target用於生成可執行文件。你在生成可執行文件以前必須先編譯經過,因此生成可執行文件的target依賴於編譯targetAnt會處理這種依賴關係。

然而,應當注意到,Antdepends屬性只指定了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依賴於BB依賴於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"/>
若是沒有ifunless屬性,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" ... />
這裏nametask的名字,attributeN是屬性名,valueN是屬性值。

有一套內置的(built-intask,以及一些可選task,但你也能夠編寫本身的task

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

能夠給task賦一個id屬性:
<taskname id="taskID" ... />
這裏tasknametask的名字,而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

你能夠用":"";"做爲分隔符,指定相似PATHCLASSPATH的引用。Ant會把分隔符轉換爲當前系統所用的分隔符。

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

爲簡潔起見,classpath標籤支持本身的pathlocation屬性。因此:
<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

3.6 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也接受這種類型的引用。
================================================================

Ant中的內置任務

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>
  ================================================================
Java相關任務

4.2.1 Javac
n
編譯java原代碼
n
例子
1. <javac srcdir="${src}"
         destdir="${build}"
         classpath="xyz.jar"
         debug="on"
  />
編譯${src}目錄及其子目錄下的全部。Java文件,。Class文件將放在$build}指定的目錄下,classpath表示須要用到的類文件或者目錄,debug設置爲on表示輸出debug信息
2. <javac srcdir="${src}:${src2}"
         destdir="${build}"
         includes="mypackage/p1/**,mypackage/p2/**"
         excludes="mypackage/p1/testpackage/**"
         classpath="xyz.jar"
         debug="on"
  />
編譯${src}${src2}目錄及其子目錄下的全部。Java文件,可是package/p1/**,mypackage/p2/**將被編譯,而mypackage/p1/testpackage/**將不會被編譯。Class文件將放在$build}指定的目錄下,classpath表示須要用到的類文件或者目錄,debug設置爲on表示輸出debug信息
3. <property name="classpath" value=".;./xml-apis.jar;../lib/xbean.jar;./easypo.jar"/>

<javac srcdir="${src}"
         destdir="${src}"
classpath="${classpath}"
debug="on"
/>
路徑是在property中定義的
4.2.2 java
n
執行指定的java
n
例子:
1. <java classname="test.Main">
         <classpath>
           <pathelement location="dist/test.jar"/>
           <pathelement path="${java.class.path}"/>
         </classpath>
       </java>
classname
中指定要執行的類,classpath設定要使用的環境變量
2. <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>
================================================================
經過JDBC執行SQL語句

4.5
執行SQL語句

n
經過jdbc執行SQL語句
n
例子:
1. <sql
    driver="org.gjt.mm.mysql.Driver"
    url="jdbc:mysql://localhost:3306/mydb"
    userid="root"
    password="root"
    src="data.sql"
/>
2. <sql
    driver="org.database.jdbcDriver"
    url="jdbc:database-url"
    userid="sa"
    password="pass"
    src="data.sql"
    rdbms="oracle"
    version="8.1."
    >
</sql>
只有在oracle、版本是8.1的時候才執行
================================================================
發送郵件

4.6
發送郵件
n
使用SMTP服務器發送郵件
n
例子:
<mail mailhost="smtp.myisp.com" mailport="1025" subject="Test build">
  <from address="[email]me@myisp.com[/email]"/>
  <to address="[email]all@xyz.com[/email]"/>
  <message>The ${buildname} nightly build has completed</message>
  <fileset dir="dist">
    <includes name="**/*.zip"/>
  </fileset>
</mail>
l mailhost
SMTP服務器地址
l mailport
服務器端口
l subject
  主題
l from
    發送人地址
l to
      接受人地址
l message
發送的消息
l fileset
  設置附件
相關文章
相關標籤/搜索