第一章:
Getting Started
l
????????
Ant
默認的構建文件為
build.xml
l
????????
Ant
構建文件中的根
XML
元素為
project
,
project
可用屬性為:
Attribute
|
Description
|
Required
|
name
|
Defines the project name
|
No
|
default
|
The target to invoke if no target is explicitly specified
|
Yes
|
basedir
|
The base directory from which all relative paths are resolved
|
No
|
?
?
l
????????
Ant
工程
(project)
中由若干個構建目標
(target)
組成,構建目標是一組相關任務
(Tasks)
的集合,如編譯文件、創建目錄、打包等,構建目標之間可以建立依賴關系,構建目標執行之前必須先執行所有所依賴的構建目標。
Ant
工程通過
project
的
default
屬性指定默認的構建目標名稱。構建目標的
XML
元素為
target
,其可用的屬性為:
Attribute
|
Description
|
Required
|
name
|
Defines the target name
|
Yes
|
depends
|
Comma-separated list of targets to execute before this target
|
No
|
if
|
Name of a property needed to run this task
|
No
|
unless
|
Name of a property that can not be set before running this task
|
No
|
description
|
Description of this target's purpose
|
No
|
?
?
l
????????
構建任務
(Tasks)
是構建目標
(target)
的基本組成部分,是
Ant
構建的基本執行單元。
Ant
中存在很多內建的構建任務,如下表所示:
Task Name
|
Description
|
ant
|
Executes Ant
|
antcall
|
Executes a target from the current build file
|
antstructure
|
From a given build file, creates a DTD reflecting all of the tasks Ant currently knows about
|
apply [execon]
|
Invokes a native executable
|
available
|
Sets a Boolean value in a property according to the availability of desired resource
|
basename
|
Sets a property to the last element of a specified path in an effort to determine a file's name without directory structure
|
buildnumber
|
Manages build numbers
|
bunzip2
|
Expands GZip or BZip2 archives
|
bzip2
|
Packs GZip or BZip2 archives
|
checksum
|
Creates checksums for one or more files
|
chmod
|
Modifies file permissions on Unix
|
concat
|
Concatenates multiple files
|
condition
|
Checks the result of a condition and sets the result to in a property
|
copy [copydir, copyfile]
|
Copies files
|
cvs
|
Interacts with a CVS repository
|
cvschangelog
|
Converts a series of CVS change logs into an XML report
|
cvspass
|
Adds entries to a .cvspass file
|
cvstagdiff
|
Creates an XML report highlighting the differences between tags
|
cvsversion
|
Finds the CVS software version
|
defaultexcludes
|
Modifies
the list of default exclude patterns, affecting which files are
automatically excluded from processing by file-related tasks
|
delete [deltree]
|
Delete files and folders
|
dependset
|
Deletes target files that are older than new source files
|
dirname
|
Assigns a file's directory path to a property
|
ear
|
Extends the jar task to support handling files for an Enterprise Application archive (EAR)
|
echo
|
Echoes text to System.out or to a file
|
exec
|
Invokes a native executable
|
fail
|
Halts and exits a build by throwing a BuildException
|
filter
|
Sets a token filter that can be used by filter-related tasks such as copy
|
fixcrlf
|
Adds or remove tabs, carriage returns, linefeeds, and EOF characters from a set of files
|
genkey
|
Adds a new key to a given keystore
|
get
|
Retrieves files using FTP, HTTP, and more from a URL
|
gunzip
|
Unpacks a GZip file
|
gzip
|
Packs a GZip file
|
import
|
Allows the use of other Ant files
|
input
|
Displays a message and reads a line of input from the console, allowing for user input during the build process
|
jar
|
Creates a JAR archive similar to Java's jar command
|
java
|
Executes the Java interpreter to run a class or application
|
javac
|
Compiles the specified source file(s)
|
javadoc [javadoc2]
|
Invokes the javadoc tool to create documentation
|
loadfile
|
Sets a property file to the entire contents of a text file
|
loadproperties
|
Loans an entire property file into Ant properties
|
macrodef
|
Defines a new task as a macro built-up upon other tasks
|
mail
|
Sends SMTP mail messages
|
manifest
|
Creates an archive's manifest file
|
mkdir
|
Makes a new directory
|
move [rename]
|
Moves a file to another directory
|
parallel
|
Contains other Ant tasks that can be run simultaneously by multiple Java threads
|
patch
|
Uses the patch command (assuming it is on the path) to apply diff files to a source file (or files)
|
pathconvert
|
Converts paths between platforms
|
presetdef
|
Defines a new task based on an existing task with certain options preset as defaults
|
property
|
Sets one or more properties to new values
|
record
|
Runs a listener that records the logging output of the build process to a file
|
replace
|
Replaces a string with another in all files in a directory
|
rmic
|
Invokes the rmic compiler
|
sequential
|
A container task that can contain other Ant tasks and run them in sequence
|
signjar
|
Uses the JarSigner to securely sign ZIP and JAR archives
|
sleep
|
Suspends execution for a specified period of time
|
sql
|
Runs SQL statements against a database
|
subant
|
Runs Ant within all subdirectories of the project directory
|
sync
|
Synchronizes two directory trees
|
tar
|
Makes a new TAR archive
|
taskdef
|
Creates a new task definition and adds it to the current project
|
tempfile
|
Sets a temporary filename to an Ant property
|
tstamp
|
Sets time-based properties to the current time
|
typedef
|
Creates a new task or data type for use in the current project
|
unjar
|
Unpacks a JAR file
|
untar
|
Unpacks a TAR file
|
unwar
|
Unpacks a WAR file
|
unzip
|
Unpacks a ZIP file
|
uptodate
|
Sets a property value to true if a given target file is newer than a set of source files
|
waitfor
|
Halts a build and continues when specified conditions are met
|
war
|
Creates WAR archive files (an extension of the jar task)
|
whichresource
|
Locates a class or resource, either on the current class path or the system class path
|
xmlproperty
|
Loads Ant properties from an XML property file
|
xslt [style]
|
Transforms a set of documents via XSLT
|
zip
|
Creates and packs a new ZIP archive
|
?
?
Ant
除了核心任務
(Core Tasks)
之外還可以使用可選任務,但使用可選任務時需要額外的
jar
庫文件的支持,這些庫文件需要存放在
Ant
的
lib
目錄下。有了這些可選任務
Ant
可以做更多的事情,如
ftp
、編譯
.Net
工程,使用
ClearCase
等。
?
?
l
????????
工程屬性
(properties)
是除了構建目標
(target)
、構建任務
(tasks)
之外的另一個
Ant
支柱,工程屬性其實是一個名稱
-
值對,可以在構建任務的
XML
元素中使用,使用方式為:
${property-name}
。工程屬性的
XML
元素為
property
,其可用的
XML
屬性為:
Attribute
|
Description
|
Required
|
classpath
|
The classpath to use when looking for a resource.
|
No
|
classpathref
|
The classpath to use when looking for a resource, which can then be given as a reference to a path element later in the build file.
|
No
|
environment
|
The
prefix to use when retrieving environment variables. For example, if
you specify environment="env", you will be able to access
operating-system-specific environment variables as property names like
${env.PATH}.
|
A resource, file, url, or environment attribute is required when not using the name attribute.
|
file
|
The name of a property file to load values from.
|
A resource, file, url, or environment attribute is required when not using the name attribute.
|
location
|
Sets
the property to the absolute filename of the given file. If an absolute
path is supplied, it's left unchanged (with / and \ characters
converted for the current platforms). Otherwise, the supplied filename
is taken as a path relative to the project's base directory and then
expanded.
|
A value, location, or refid element is required when using the name attribute.
|
name
|
The name of the property to set.
|
No
|
prefix
|
The prefix to add to properties loaded from a file or a resource. A . is appended to the prefix if none is specified.
|
No
|
refid
|
A reference to a (previously) defined object.
|
A value, location, or refid element is required when using the name attribute.
|
resource
|
The resource name of the property file, used for searching the classpath.
|
A resource, file, url, or environment attribute is required when not using the name attribute.
|
url
|
The URL from which to read properties.
|
A resource, file, url, or environment attribute is required when not using the name attribute.
|
value
|
The value of this property.
|
A value, location, or refid element is required when using the name attribute.
|
?
?
l
????????
Ant
的內建屬性:
ant.file
Contains the absolute path of the build file
ant.java.version
Contains the JVM version Ant is using (can hold only 1.1, 1.2, 1.3, 1.4 and [as of Ant 1.6] 1.5)
ant.project.name
Holds the name of the project that is executing (set with the name attribute of project)
ant.version
Contains the version of Ant running
basedir
Holds the absolute path of the project's base directory (set with the basedir attribute of project)
l
????????
Ant
的運行:
ant [options] [target [target2 [target3] ...]]
,在
options
中可以指定要執行的構建文件,默認為
build.xml
,如果構建文件為
project.xml
,則相應用
ant
命令為
: ant –f project.xml
,
target
可以指定多個,之間用空格分隔,如果未指定則使用構建文件中的默認目標。
?
?
?
?
?
?
?
?
?
?
?
?
第二章:
Using Properties and Types
l
????????
Condition
任務用于控制其他任務的執行:在
Condition
任務中設置屬性值,而在其他任務中根據這些屬性值的內容決定是否執行,如:
<condition property="all.set">
???
????<and>
?????
????? <available file="file1.java"/>
?????
???????<available file="file2.java"/>
???
????</and>
</condition>
這個任務用于判斷
file1.java
和
file2.java
是否存在,如果都存在,則設置
all.set
屬性為
true
,否則為
false;
除了示例中出現的
condition
和
and
條件任務外還有
available
、
or
、
equals
等其他條件任務。
l
????????
在任務中添加
if
或
unless
可以決定任務是否執行,繼上例:
<target name="buildModule" if="all.set"/>
??????? . . .
</target>
這個任務當
all.set
為
true
時執行,否則不執行。
l
????????
fail
任務用于中斷構建,如:
<fail message="Could not find all files." unless="all.set" />
當
all.set
為
false
時中斷構建,并輸出
” Could not find all files.”
l
????????
從文件裝入屬性:
<property file="build.properties" />
l
????????
將文件內容作為單個屬性裝入:
<loadfile property="message" srcFile="message.txt"/>
l
????????
屬性可以設置多次,后頁的屬性會覆蓋前頁的屬性值。
l
????????
通過命令行設置屬性值:使用
-D
選項
ant -Dmessage="Compiling and compressing"
l
????????
使用環境變量作為屬性值,如:
<property environment="env" />
加上這句以后可以使用如下方式獲取環境變量
JAVA_HOME
的值:
${env.JAVA_HOME}
,其他環境變量也通過類似方式獲得。
l
????????
fileset
用于指定文件集,并設置到屬性中用于其他任務的構建,如文件復制、編譯等,
fileset
的示例如下:
<fileset dir="${source}">
??? <include name="**/*.java"/>
??? <exclude name="**/*test*"/>
</fileset>
include
用于包含文件,
exclude
用于排除文件,通配符
**
表示當前目錄或當前目錄下的所有子目錄。除了使用
include
和
exclude
之外還可以使用
filename
來指定加入文件集的特定文件,如:
<fileset dir="${source}">
??? <filename name="**/*.java"/>
??? <filename name="test.cpp"/>
</fileset>
l
????????
使用
fileset
的示例
:
<copy todir="../dest">
??? <fileset dir="src">
??????? <exclude name="**/*.java"/>
??? </fileset>
</copy>
這個任務將
fileset
中的文件復制到
../dest
目錄下
l
????????
相對于
fileset
還可以使用
dirset
表示目錄集,使用與
fileset
類同。
?
?
?
?
?
?
?
?
?
?
第三章:
Building Java Code
l
????????
javac
任務用于編譯
java
源代碼,如:
<javac srcdir="${src}"
??? debug="on"
??? destdir="${bin}"
??? classpath="common.jar"
/>
l
????????
input
任務可接收用戶輸入,如:
<target name="init">
??? <input
??????? message="Deleting bin directory OK?"
??????? validargs="y,n"
??????? addproperty="do.delete"
??? />
??? <condition property="do.abort">
??????? <equals arg1="n" arg2="${do.delete}"/>
?
??</condition>
??? <fail if="do.abort">Build aborted.</fail>
??? <delete dir="${output}" />
??? <mkdir dir="${output}" />
</target>
l
????????
在同一構建文件中可以使用
antcall
任務執行另一個構建任務,并可指定構建參數作為構建屬性,如:
<target name="main" depends="init, compile, compress">
?? <antcall target="displayMessage">
?????? <param name="msg" value="${message}"/>
?? </antcall>
</target>
<target name="displayMessage">
??? <echo message="msg=${msg}"/>
</target>
在本例中
main
任務調用了
displayMessage
任務,并設置
msg
參數作為調用屬性,在
displayMessage
任務中可以通過
${msg}
獲取該屬性值。
l
????????
使用
ant
任務執行另一
Ant
構建文件,如:
<ant antfile="subproject/subbuild.xml">
??? <property name="parameter" value="4096"/>
??? <property file="config/subproject/build.properties"/>
</ant>
本例中執行了另一以
subproject/subbuild.xml
為構建文件的
Ant
構建,將指定了構建的屬性值,除此之外還可指定構建目標等其他選項。
l
????????
使用
import
導入其他構建文件的內容,如:
<import file="shared.xml"/>
l
????????
使用
javadoc
任務創建
java
文檔,如:
<javadoc
???? sourcefiles="${src}/Project.java"
???? destdir="${docs}"
???? author="true"
???? version="true"
???? use="true"
???? windowtitle="Project API">
???? <doctitle><![CDATA[<h1>Project API</h1>]]></doctitle>
???? <bottom><![CDATA[<i>Copyright © 2005</i>]]></bottom>
</javadoc>
l
????????
使用
jar
任務創建
jar
包,如
:
<jar destfile="${output}/Project.jar" basedir="${output}"
???? includes="*.class" >
???? <manifest>
???????? <attribute name="Author" value="${user.name}"/>
???????? <section name="Shared">
???????????? <attribute name="Title" value="Example"/>
???????????? <attribute name="Vendor" value="MegaAntCo"/>
???????? </section>
???????? <section name="Copyright">
???????????? <attribute name="Copy" value="(C) MegaAntCo 2005"/>
???????? </section>
???? </manifest>
</jar>
l
????????
使用
buildnumber
任務讀取并創建構建序號,
buildnumber
從文件
(
默認為
build.number)
中讀取原構建序號并存到
build.number
屬性中,在成功構建后加
1
寫回到文件中。
Buildnumber
的
file
屬性用于指定存放構建序號的文件。
l
????????
使用
tstamp
任務設置時間屬性,有
3
個屬性被設置,分別為
DSTAMP
,格式為
”
yyyyMMdd”
;
TSTAMP
,格式為
”hhmm”
;
TODAY
,格式為
”MM dd yyyy”
。
Tstamp
的子任務
format
可以設置時間的格式,如:
<tstamp>
?? <format property="timestamp" pattern="MM/dd/yyyy hh:mm:ss"/>
</tstamp>
?
?
?
?
第四章:
Deploying Builds
l
????????
使用
tar
任務創建
tar
文件,如:
<tar
???? destfile="${output}/Project.tar.gz"
???? basedir="${output}"
???? includes="*.class"
??? compression="gzip"/>
l
????????
刪除已存在的文件:
<delete file="/lib/Project.jar"/>
l
????????
刪除已存在的目錄:
<delete dir="${dist}"/>
l
????????
創建目錄:
<mkdir dir="${dist}"/>
l
????????
copy
任務用于復制文件或目錄。
l
????????
move
任務用于移動文件或目錄。
l
????????
使用
ftp
發送或接收文件,如:
<ftp server="xxx.xxx.xxx.xxx" binary="true" verbose="true"
??????????? userid="${name}" password="${password}"
?????????? remotedir="/cgi-bin" action=”send” >
??? <fileset dir="${output}">
?????? <exclude name="*.java"/>
?????? <exclude name="*.class"/>
?????? <exclude name="*.txt"/>
?? </fileset>
</ftp>
ftp
的
action
包括:
put, get, del, list, chmod, mkdir and rmdir
l
????????
使用
telnet
,如:
<telnet userid="steven" password="let_me_in"
????????????????? server="xxx.xxx.xxx.xxx">
???? <read>/home/steven</read>
???? <write>ls</write>
</telnet>
l
????????
使用
SSH
,如
:
<sshexec host="xxx.xxx.xxx.xxx"
??? username="${name}"
??? password="${password}"
??? command="touch index.html"/>
l
????????
使用
email
,如:
<mail mailhost="smtp.isp.com" mailport="1025" subject="New Build">
??? <from address="developer@isp.com"/>
??? <replyto address="developer@isp.com"/>
??? <to address="list@xyz.com"/>
??? <message>Here is the new build.</message>
??? <fileset dir="dist">
??????? <includes name="**/*.tar.gz"/>
??? </fileset>
</mail>
l
????????
自動構建:可以使用
Windows
的計劃任務,或
Anthill
工具進行自動構建。
?
?
?
?
?
?
第六章:
Getting Source Code from CVS Repositories
l
????????
登入
(login)
:
<cvspass
?????? cvsroot=":pserver:steven@STEVE:/home/steven/repository"
??????? password="opensesame" />
l
????????
檢出
(check out)
:
<cvs package="GreetingApp" dest="${cvs.dir}" />
l
????????
更新
(update):
<cvs dest="${cvs.dir}" command="update"/>
l
????????
提交
(commit):
<cvs dest="${cvs.dir}/GreetingApp"
command="commit -m 'New Version'"/>
l
????????
創建更新日志
(change log)
:
<cvschangelog dir="${cvs.dir}/GreetingApp"
??????????
???? destfile="changelog.xml" />
?
?
?
?
第七章:
Executing External Programs
l
????????
執行
Java
代碼,如:
<java jar="${bin}/connect.jar"
??? fork="true"
??? failonerror="true"
??? maxmemory="512m"
??? >
??? <arg value="-q"/>
??? <classpath>
????
???<pathelement location="${bin}/connect.jar"/>
??????? <pathelement path="${java.class.path}"/>
??? </classpath>
</java>
l
????????
exec
任務用于執行系統命令或外部程序,如:
<exec dir="." executable="/bin/cpp.exe"
??????? failonerror="true">
??????????? <arg line="-c ${src}/*.cpp ${output}"/>
</exec>
l
????????
用
apply
任務執行批處理,
apply
與
exec
的差別是
apply
接收
fileset
,這樣可以對多個文件或輸入執行相同中的命令或程序。
l
????????
用
parallel
可執行多線程任務。