Ant 攻略

1  Ant 是什么?
Ant 是一种基于 Java 和 XML 的 build 工具。

2 下载、安装 Ant
安装 Ant
下载 .zip 文件,解压缩到 c:\ant1.3( 后面引用为 %ANT_HOME%)

2.1
在你运行 Ant 之前需要做一些配置工作。
·
将 bin 目录加入 PATH 环境变量。
·
设定 ANT_HOME 环境变量,指向你安装 Ant 的目录。在一些 OS 上, Ant 的脚本可以猜测 ANT_HOME ( Unix 和 Windos NT/2000 )-但最好不要依赖这一特性。
·
可选地,设定 J***A_HOME 环境变量(参考下面的高级小节),该变量应该指向你安装 JDK 的目录。
注意:不要将 Ant 的 ant.jar 文件放到 JDK/JRE 的 lib/ext 目录下。 Ant 是个应用程序,而 lib/ext 目录是为 JDK 扩展使用的(如 JCE , JSSE 扩展)。而且通过扩展装入的类会有安全方面的限制。
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 ,执行一个叫做 dist 的 target 。
ant -buildfile test.xml -Dbuild=build/classes dist
使用当前目录下的 test.xml 运行 Ant ,执行一个叫做 dist 的 target ,并设定 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 时,使用 project 的 default 属性所确定的 target 。

3.2 Targets

一个 target 可以依赖于其他的 target 。例如,你可能会有一个 target 用于编译程序,一个 target 用于生成可执行文件。你在生成可执行文件之前必须先编译通过,所以生成可执行文件的 target 依赖于编译 target 。 Ant 会处理这种依赖关系。

然而,应当注意到, Ant 的 depends 属性只指定了 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 依赖于 B , B 依赖于 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"/>
如果没有 if 或 unless 属性, target 总会被执行。

可选的 description 属性可用来提供关于 target 的一行描述,这些描述可由 -projecthelp 命令行选项输出。

将你的 tstamp task 在一个所谓的初始化 target 是很好的做法,其他的 target 依赖这个初始化 target 。要确保初始化 target 是出现在其他 target 依赖表中的第一个 target 。在本手册中大多数的初始化 target 的名字是

posted on 2006-11-21 17:58 Timothy 阅读(308) 评论(0)  编辑  收藏 所属分类: J2EE开发环境

<2026年11月>
日一二三四五六
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

导航

统计

公告

语义Web : 网络就是计算机,软件就是服务!

常用链接

留言簿(4)

随笔档案(20)

文章分类(25)

文章档案(25)

新闻分类(4)

新闻档案(4)

相册

收藏夹(27)

个人主页

网络博客

最新随笔

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜