一、安装Ant
1、 先将Ant解压到一个目录,假如解压到D:\ant
2、 设置环境变量
ANT_HOME=d:\ant
PATH=%PATH%;%ANT_HOME%\bin
二、使用Ant
Ant的构建文件是用XML格式书写的,每个build文件包含一个project和至少一个默认的target。
1、 Project
Project有三个属性name,default,basedir
Name:Project的名字
Default:build文件运行时默认的target
Basedir:进行项目构建的根目录,如果没有设置此项,则默认与build文件同目录
2、 Target
一个Target可以依赖于其它多个Target,
想要执行B必需先执行A
Target的属性:name,depends,if,unless,description
Name:Target的名字
Depends:执行当前Target时需要依赖的Target
If:这个属性的名字必需设置,当前的Target才能执行
Unless:这个属性的名字必需不能设置,当前的Target才能执行
Description:对当前Target的一段描述
3、 Tasks
具体需求执行的任务,这个就有很多了,如:WAR, EAR, JAVAC, JAVA, JAR, COPY, COPYDIR, COPYFILE, MKDIR, MOVE, DELETE, ECHO, EXEC, UNZIP, ZIP, TAR, UNJAR, UNTAR, UNWAR, SCP, FTP, TELNET, 等等,以下是各Task的属性介绍:
(1) Javac:编译Java源文件
Srcdir:Java文件的目录
Destdir:Class文件的保存目录
Includes:需要包含哪些文件
Excludes:不包含哪些文件
Classpath:编译时需要引用的classpath
Debug:编译时是否包含debug信息
(2) Java:运行class文件
Classname:需要执行的class文件名
Jar:需要执行的jar文件,必须包含程序入口类,有Main方法的类
Args:执行class需要的参数
Classpath:需要使用的classpath
(3) Jar:将多个class文件打成一个jar包
Destfile:需要创建的jar文件名
Basedir:文件的来源
Includes:需要包含哪些文件
Excludes:不包含哪些文件
includes="mypackage/test/**"
excludes="**/Test.class"
/>
(4) War:将文件打包成War文件
Destfile:需要创建的war文件名
Webxml:web.xml文件的路径及文件名
Basedir:文件的来源
Includes:需要包含哪些文件
Excludes:不包含哪些文件
(5) Ear:将文件打包成Ear文件
Destfile:需要创建的ear文件名
appxml:META-INF/application.xml文件的路径及文件名
Basedir:文件的来源
Includes:需要包含哪些文件
Excludes:不包含哪些文件
(6) Mkdir:创建一个目录
(7) Delete:删除一个文件,或文件夹及其包含的文件
File:需要删除的文件名
Dir:需要删除的目录
4、 Properties
一个Project可以设置多个Property,可以在build文件内使用,也可以通过Ant命令使用
(1) 在build文件内
(2) 通过Ant命令,使用选项为:-Dproperty=value
执行Ant命令:ant –Dbuild=aa 则location的值就变为aa了
设置Property的六种方式
(1) 通过name,value的属性设置
(2) 通过name,refid的属性设置
(3) 通过file,url,resource属性设置,foo.properties是键值对的属性文件
(4) 通过environment属性设置,获得环境变量
三、运行Ant
ant [options] [target [target2 [target3] ...]]
Options:
-help, -h print this message
-projecthelp, -p print project help information
-version print the version information and exit
-diagnostics print information that might be helpful to
diagnose or report problems.
-quiet, -q be extra quiet
-verbose, -v be extra verbose
-debug, -d print debugging information
-emacs, -e produce logging information without adornments
-lib
-logfile
-l
-logger
-listener
-noinput do not allow interactive input
-buildfile
-file
-f
-D
-keep-going, -k execute all targets that do not depend
on failed target(s)
-propertyfile
properties taking precedence
-inputhandler
-find
-s
-nice number A niceness value for the main thread:
1 (lowest) to 10 (highest); 5 is the default
-nouserlib Run ant without using the jar files from ${user.home}/.ant/lib
-noclasspath Run ant without using CLASSPATH
如:ant -buildfile test.xml -Dbuild=build/classes dist