apache ant 中文手册
lgl669
2009-07-06
apache ant是一个基于java的编译工具。它从功能上类似于make,但它以克服了make的缺点。
为什么在make, gnumake, nmake, jam这些编译工具后,还要产生一个ant呢?因为这些编译工具都有一定的限制。ant的最初开发者在开发跨平台的应用时,发现以前的那些开发工具都有一定的限制。例如make工具,它是基于shell的,它判断一系列的条件,并像在shell下面那样执行编译命令,这样做的好处的:针对特定的平台(如 unix),可以容易的利用这些工具做编译;但同时,这也意味着,你的编译限制在固定的操作系统下,如只能用于unix。 同时,make文件也有一些先天的缺陷。好多人都会遇到恐怖的tab问题。ant的最初开发者多次说“我的命令不能执行因为我在tab前面加了一个空格!”。一些工具如jam一定程序上解决了这个问题,但仍有其它的格式问题。 ant与从基于命令的那些扩展开来的那些工具不同,ant是由java类扩展的。不用编写shell命令,而是配置基于xml的文件,形成多个任务的目标配置树。每一个任务都是通过一个实现了一个规定接口的java类来运行的。 ant缺少了一些直接执行shell命令的能力,如find . -name foo -exec rm {},但它给用户提供了跨平台的能力,可以在任何地方工作。实际上,ant也提供了命令execute用来执行shell命令,这就是它的<exec>任务,它允许执行基于操作系统的命令。 安装ant 下载ant。ant有两个版本,二进制版本和原代码版本。 要编译和使用ant,你必须要有一个兼容jaxp的xml解析器。 二进制版本中自己带有了最新的apache xerces2 xml解析器。如果你想用不同的兼容jaxp的解析器,你可以移去ant库目录下的xercesimpl.jar和xml-apis.jar两个文件。你可以要用的解析器放到ant的库目录下,也可以把jars文件放到系统的类路径中。 在ant的当前版本中,还需要安装jdk。下面的二进制片版本的安装后的目录结构。 ant 安装目录 ant/bin 包含了运行脚本 ant/lib ant运行所需的类库 ant/docs 文档 ant/etc 一些配置,如何通过任务的xml输出建立详细的报告, 移动编译后的文件,忽略错误警告 在运行前还要做一些设置: 1) 在path路径中加入ant/bin目录; 2) 设置ant_home为安装目录; 3) 通常还要设置java_home为java的安装路径。 例如下: set ant_home=c:\ant set java_home=c:\jdk1.5.1 set path=%path%;%ant_home%\bin 使用ant 1 写一个简单的编译文件 ant的编译文件是写成xml格式的,每一个编译文件包含一个project和至少一个 target。 targets包含了一些task元素。每一个task元素可以有一个id 属性 (attribute)可以被其它的引用。 2 projects 一个projext可以有如下的属性attribute: attribute 说明 是否必须 name 项目名称 no default 当没有target支持时,缺省使用的target yes. basedir 基目录,其它的目录都是相对于此. 这个attribute可以被"basedir" property 覆盖。当"basedir" property设置后,basedir attribute就不起作用了。如果两者都没有设置,则buildfile本身的父目录作为基目录。 no 根据需要,还可以设定项目的说明,在<description>元素中。 第一个project可以有一个或多个targets。一个target就是一系列的你要执行的任务组。在运行ant时, 你可以选择哪一个任务组被执行。如果没有给出执行的任务组,则project缺省的任务组被执行。 3 targets target可能依赖于其它的target而存在。你可能用一个target用来编译,用 另一个 target且用来布署。只有编译完成后,布署才可执行,这样布署就 依赖于编译。 要说明的是,ant的depends attribute只是规定了target之间的前后执行 关系,并不表示真正的依赖性。 依赖的次序是按照排列次序从左到右。但如果这些依赖本身之间还有依赖 关系,也可能在后面的先执行,如下面的例子: <target name="a"/> <target name="b" depends="a"/> <target name="c" depends="b"/> <target name="d" depends="c,b,a"/> 如果我们要运行target d。从它的依赖attribute列表上看,可能先执行c, 然后b,再后是a。其实这是不对的。c依赖于b,b依赖于a。因此是a先执行, 然是b,然后c,最后是d。 一个target只执行一次,虽然可能好多的target都依赖于它。如上例中所示。 target还可以根据它的attribute的设置情况来判定执行情况。这允许根据 系统的状况(如java版本,os,命令行属性定义等等)更好的控制编译过程。 这就引入if或unless属性(attribute)。 注意:ant只是检查属性(attribute)是否被设置,而不查它的值,即使属性 (attribute)值为空字符串,它仍认为这是一个存在的属性(attribute)。 例如: <target name="build-module-a" if="module-a-present"/> <target name="build-own-fake-module-a" unless="module-a-present"/> 在第一行中,如果module-a-present属性(attribute)被设置,则target执行。 在第二个中,如果module-a-present属性(attribute)被设置,同target不执行。 如果没有if或unless属性(attribute),则目标总会被执行。 可选的description属性(attribute)可以用来表示一行的target的说明,可以 在命令行中用-projecthelp参数显示出来。没有description属性(attribute) 的target被认为是内部的,并不显示出来,除非利用了-verbose或-debug参数。 初始化target,所有的其它target都依赖于它。通常都位于依赖列表的第一项。 在这个手册中,大都的初始化targets用init来命名。 target有如下的属性: 属性(attribute) 说明 是否必须 name target的名字 yes depends 依赖target列表 no if 要执行target,某属性(attribute)必须要被设置 no unless 要执行target,某属性(attribute)必须不能被设置 no description 有关target功能的简短说明 no target命名说明:可以使用任何xml编码支持的字母表中的字符,如空串,逗号, 空格。但最好避免使用它们,为了避免引起歧义,将来版本的ant不支持这些特殊 的字符。而各种ide中时target的命名规则,则跟具体的开发厂家相关。 以横线开头的target名,如”-restart”是有效的命名,但不能被用来在命令行 中执行。 4 tasks task是一段可执行的代码。 task可以有多种属性(attribute)(或参数,只要你喜欢), 属性(attribute)的值可能包含了对属性(attribute)的引用。 这些引用在task执行 以前要解析开。 task的通用结构如下: <name attribute1="value1" attribute2="value2" ... /> name是task的名字,attributen是属性(attribute)的名字,而valuen则是属性 (attribute)的值。 一些是内建的任务,而另一些是可选的任务,用户可自由定义。 所有的tasks都有共享task名字,属性(attribute)值将会在ant生成消息时用到。 task可以指定一个id属性(attribute):<taskname id="taskid" ... /> taskname是task的名字,taskid是一个独一无二的标识。你可以在脚本程序或 其它的task中通过这个名字引用相应的task对象。例如: <script ... > task1.setfoo("bar"); </script> 这样可以task1的foo属性值为bar;而在java程序中,则通过project.getreference ("task1")访问task1实例。 5 properties 项目可以有一系列的属性(property)。这些可能在编译文件通过property任务 (task)设置,或可能在ant外设置。一个属性(property)由名字和值对应起来。 名字是大小写敏感的。属性(property)可以用在task的(attribute)中。能够在 ”{property名}”的方式进行引用。例如,如果有一个builddir属性 (property),值为build,则在属性(attribute)中可以这样用:${builddir}/classes。 在运行时,它被转换成build/classes。 内建的properties ant提供了所有的系统属性的访问就像这些系统属性(property)已经定义在 <property>任务中一样。 例如:${os.name}表示操作系统的名称。 下面列出了一些内建属性(property): basedir 项目的绝对路径 (就像在<project>的basedir属性中定义中的一样) ant.file 编译文件的绝对路径 ant.version ant的版本 ant.project.name 正在执行的项目的名字,(就像在<project>的name属性中设置的那样). ant.java.version jvm的版本,其值可能是"1.1", "1.2", "1.3" and "1.4". 编译文件的例子: <project name="myproject" default="dist" basedir="."> <description> simple example build file </description> <!-- set global properties for this build --> <property name="src" location="src"/> <property name="build" location="build"/> <property name="dist" location="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" description="compile the source " > <!-- compile the java code from ${src} into ${build} --> <javac srcdir="${src}" destdir="${build}"/> </target> <target name="dist" depends="compile" description="generate the distribution" > <!-- 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" description="clean up" > <!-- delete the ${build} and ${dist} directory trees --> <delete dir="${build}"/> <delete dir="${dist}"/> </target> </project> 上面,我们把属性(property)定义在任何target的外部,<property>, <typedef>和<taskdef>这些task是特殊的task,可以在任何的target外部定义。 这样做时,它们会在任何target执行前被执行。其它任何的task不能target目录 外定义。 上面一些target给了description,这样,在使用projecthelp选项时,会列出 这些公共的 targets,而那些没有description的target则被认为是内部的, 并不显示。 最后,要使target工作,则source子目录要存在相应的位置。 注: 英文中有两个单词都是属性attribute, property,这里不好区分。因此, 实际翻译时,把原文的这两个单词也写了出来。 |
相关讨论
相关资源推荐
- mina-sshd:Apache MINA SSHD的镜像
- java反序列化漏洞解决方案
- 2019/1/22敲JAVA时小伙伴们遇到的坑
- Sun的Java插件有漏洞 影响操作系统
- 下个月,java要开启收费模式了,你怕了吗?
- java glassfish漏洞_Oracle Sun GlassFish/Java System Application Server远程身份验证绕过漏洞...
- Apache Mina - 简单的服务端和客户端的搭建
- apache mina实现sftp的文件上传与权限修改功能
- Apache MINA框架入门
- Java Socket框架Apache MINA:实现Socket服务器端