Apache Ant
是一个基于
Java
的
build
工具
(
有翻译成“生成工具”,感觉怪怪,故直译
)
,
Ant
不是蚂蚁的意思,根据创始人
James Duncan Davidson
介绍,这是
Another Neat Tool
的首字母缩写。
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
生成工具在软件开发中用来将源代码和其他输入文件转换成可执行文件的形式,或者部署
Web
工程等。很多
IDE
都能支持这种直接的编译和部署,但有时当系统比较大的时候,或者系统需要部署在不同工作环境下的时候,
IDE
可能不大好使。使用
Ant
能够使得工程的编译和部署更简单,并且能确保在部署期间都使用精确相同的步骤,最最重要的是能够实现尽可能多的自动化,这在敏捷开发的过程中是十分重要的,前面说的自动部署等工作都是由它来完成的。
使用
Ant
之前,首先要下载并安装
Ant
,然后在工程根目录下面建立一个叫
build.xml
的文件,这是默认的名字,叫其它的名字也可以,默认是
build
。
Ant
没有定义它自己的自定义语法,它的生成文件是用
XML
编写的,就像前面说的
build.xml
,这个文件中要采用一些
Ant
能理解的预定义
XML
元素,当然,我们还可以自己定义新的元素来扩展
Ant
的功能。每个生成文件由单个
project
元素组成,该元素又包含一个或多个
target
元素。一个
target
是已定义的一个步骤,它执行任意数量的操作,比如编译一组源文件。然后这些
target
将根据需要被分
组到各个
target
元素中。
比如说,一个生成临时文件的
target
,一般会被初始化的
target
调用,也可能被
unittest
的
target
调用。
顶级
project
元素需要包含一个
default
属性,如果在
Ant
被调用时没有指定目标,这个属性将指定要执行的目标。然后需要使用
target
元素来定义该目标本身。下面是一个最基本的生成文件:
<?xml version="1.0"?>
<project default="init">
<target name="init">
</target>
</project>
下面介绍一下
Ant
工程文件中的一些常用元素:
1.
属性
<property name="SRC" value="../src"/>
Ant
的属性有点像
Java
中的变量,但又不是一般的常量,他们一经定义就不可改变,有点像是
final
。然后在工程文件中的其它地方引用的时候用
${SRC}
。
2.
定义依赖关系
<target name="init"/>
<target name="pre-compile" depends="init"/>
<target name="compile" depends="init,pre-compile"/>
上面这几个
Target
定义了他们之间的执行顺序,
compile
执行之前
init
和
pre-compile
要先执行,而
pre-comiple
执行之前,
init
要执行完毕,也就是
init
à
pre-compile
à
compile.
这里不用担心一个任务会被重复执行多次,依赖关系只是确保他们已经被执行,并不是一定会执行一遍。
3.
使用过滤功能
当我们需要将工程部署在不同的环境时,比如
Dev
,
QA
,
UAT
等,我们需要用不同的数据库连接,不同的文件路径等等,而一般情况下我们只建立了一个工程文件,所以这个时候就需要替换掉相应的值,如果手工处理的话,除了累晕了还有可能做错了。
Ant
的过滤功能可以实现自动替换。首先要定义好替换的变量:
<filterset id="filterSet" begintoken="%%" endtoken="%%">
<filter token="AS_HOME" value="${AS_HOME}" />
<filtersfile file="./filter.properties" />
</filterset>
这个
filterset
定义了将工程文件中的
%% AS_HOME %%
替换成前面定义的变量
${AS_HOME}
的值。除了在工程文件中单独定义,还可以在
properties
文件中定义,然后引用进来就可以。比如在拷贝配置文件的时候,通常需要替换一些值的,这时便可以使用这个
filterset
,
<copy toDir="./destFolder" >
<fileset dir="${template.config.dir}/app/filConfig" />
<filterset refid="filterSet" />
</copy>
4.
在
Ant
中使用宏
Macrodef
有时我们需要对重用一个
target
,但由于一些参数属性的不同,不用直接调用,使用我们需要用
macrodef
来定义可重用的
target
。
Example,
<macrodef name="test"> //
这里是
macrodef
的定义,定义了
name
属性
<attribute name="one"/> //
参数定义,可以在
macrodef
外部调用
<attribute name="two" default="@{one}"/> //
内部参数
<sequential> //
实际执行的内容在
sequential
里
<echo>one=@{one} two=@{two}</echo>
</sequential>
</macrodef>
调用的时候是这样子的:
<test one="test"/>
。需要注意的是:
1
)在整个工程文件中,
macrodef
跟
target
是同个
level
的;
2
)
macrodef
可以调用其它
macrodef,
但不能调用
target
;
target
可以调用其它
target
,也可以调用
macrodef
。
下面开始介绍一个完整的
Ant
工程大致的流程。
1) Define the initial properties
-- include properties files, which set the true value in it
-- create a "properties" target to define the set the values into variables
2) init-build: clean the dest folder and create the folder which would be used during deploy
3) Set project classpath: set some path with id for refer when compile class, like third party lib, etc.
4) Compile the dependent component: if the project is associated with other project, then we need to
compile the dependency project ahead.
5) Build classes to the dest build folder
6) Init-package, prepare configuration (including filter config files with filter.properties file), and copy it to
the dest folder
7) Package spring config, JSF config, toplink config, third party lib and other lib the project dependence,
project jar file into war.
8) Copy the project war package to OC4J
9) Prepare the config file which need to copy to OC4J, like app config file, template file
10) Copy the config file prepared in step 8 to OC4J.
11) Undeploy the application in OC4J.
12) Deploy the application into OC4J.
转载于:https://blog.51cto.com/lames/279589