ANT的安装/配置笔记

ANT的安装/配置笔记
作者:车东发表于:2003-03-0617:03最后更新于:2007-04-1211:04
版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本声明。
http://www.chedong.com/tech/ant.html
--------------------------------------------------------------------------------
内容摘要:
ant是一个基于JAVA的自动化脚本引擎,脚本格式为XML。除了做JAVA编译相关任务外,ANT还可以通过插件实现很多应用的调用。


ANT的基本概念:
ANT的安装:解包,设置路径
ANT的使用:最好的学习只不过是一个简单实用的例子起步……
ANT的基本概念:Java的Makefile
当一个代码项目大了以后,每次重新编译,打包,测试等都会变得非常复杂而且重复,因此c语言中有make脚本来帮助这些工作的批量完成。在Java中应用是平台无关性的,当然不会用平台相关的make脚本来完成这些批处理任务了,ANT本身就是这样一个流程脚本引擎,用于自动化调用程序完成项目的编译,打包,测试等。除了基于JAVA是平台无关的外,脚本的格式是基于XML的,比make脚本来说还要好维护一些。


每个ant脚本(缺省叫build.xml)中设置了一系列任务(target):比如对于一个一般的项目可能需要有以下任务。

任务1:usage打印本脚本的帮助信息(缺省)
任务2:clean
< -- init清空初始化环境
任务3:javadoc<--build<--init生成JAVADOC
任务4:jar<--build<--init生成JAR
任务5:all<--jar+javadoc<--build<--init完成以上所有任务:jarjavadoc
而多个任务之间往往又包含了一定了依赖关系:比如把整个应用打包任务(jar)的这个依赖于编译任务(build),而编译任务又依赖于整个环境初始化任务(init)等。

注:我看到很多项目的ant脚本中的命名基本上都是一致的,比如:编译一般叫build或者compile;打包一般叫jar或war;生成文档一般命名为javadoc或javadocs;执行全部任务all。在每个任务的中,ANT会根据配置调用一些外部应用并配以相应参数执行。虽然ANT可调用的外部应用种类非常丰富,但其实最常用的就2,3个:比如javacjavadocjar等。
ANT的安装
解包后在系统可执行路径中加入指向ant的bin的路径就可以了,比如可以在GNU/Linux上把以下配置加入/etc/profile中:
exportANT_HOME
=/home/ant
exportJAVA_HOME =/usr/java/j2sdk1.4.1
exportPATH =$PATH:$JAVA_HOME/bin:$ANT_HOME/bin

这样执行ant后,如果不指定配置文件ant会缺省找build.xml这个配置文件,并根据配置文件执行任务,缺省的任务设置可以指向最常用的任务,比如:build,或指向打印帮助信息:usage,告诉用户有那些脚本选项可以使用。


ANT的使用

最好的学习过程就是看懂那些opensource项目中的build.xml脚本,然后根据自己的需要简化成一个更简单的,ANT和APACHE上很多非常工程派的项目:简单易用,而且适应性非常强,因为这些项目的建立往往来源于开发人员日常最直接的需求。
以下是的一个WebLucene应用的例子:修改自JDOM的build.xml:

<projectdefault
="usage" basedir ="." >

<!-- =================================================================== -->
<!-- Initializationtarget -->
<!-- =================================================================== -->
< target name ="init" >
< tstamp />
< property file ="${basedir}/build.properties" />
< property name ="Name" value ="ProjectFullName" />
< property name ="name" value ="project_name" />
< property name ="version" value ="0.2" />
< property name ="year" value ="2003" />

< echo message ="-----------${Name}${version}[${year}]------------" />

< property name ="debug" value ="off" />
< property name ="optimize" value ="on" />
< property name ="deprecation" value ="on" />

< property name ="src.dir" value ="./src/WEB-INF/src" />
< property name ="lib.dir" value ="./src/WEB-INF/lib" />
< property name ="packages" value ="com.chedong.*,org.apache.lucene.*" />

< property name ="build.src" value ="./src/WEB-INF/build" />
< property name ="build.dest" value ="./src/WEB-INF/classes" />
< property name ="build.javadocs" value ="./src/doc" />

< path id ="classpath" >
< pathelement path ="${jsdk_jar}" />
< fileset dir ="${lib.dir}" >
< include name ="**/*.jar" />
</ fileset >
</ path >

< filter token ="year" value ="${year}" />
< filter token ="version" value ="${version}" />
< filter token ="date" value ="${TODAY}" />
< filter token ="log" value ="true" />
< filter token ="verbose" value ="true" />
</ target >

<!-- =================================================================== -->
<!-- Helponusage -->
<!-- =================================================================== -->
< target name ="usage" depends ="init" >
< echo message ="${Name}Buildfile" />
< echo message ="-------------------------------------------------------------" />
< echo message ="" />
< echo message ="availabletargetsare:" />
< echo message ="" />
< echo message ="jar-->generatesthe${name}.jarfile" />
< echo message ="build-->compilesthesourcecode" />
< echo message ="javadoc-->generatestheAPIdocumentation" />
< echo message ="clean-->cleansupthedirectory" />
< echo message ="" />
< echo message ="Pleaserenamebuild.properties.defaulttobuild.properties" />
< echo message ="andeditbuild.propertiestospecifyJSDK2.3classpath." />
< echo message ="" />
< echo message ="Seethecommentsinsidethebuild.xmlfileformoredetails." />
< echo message ="-------------------------------------------------------------" />
< echo message ="" />
< echo message ="" />
</ target >

<!-- =================================================================== -->
<!-- Preparesthesourcecode -->
<!-- =================================================================== -->
< target name ="prepare-src" depends ="init" >
<!-- createdirectories -->
< mkdir dir ="${build.src}" />
< mkdir dir ="${build.dest}" />

<!-- copysrcfiles -->
< copy todir ="${build.src}" >
< fileset dir ="${src.dir}" />
</ copy >
</ target >

<!-- =================================================================== -->
<!-- Compilesthesourcedirectory -->
<!-- =================================================================== -->
< target name ="build" depends ="prepare-src" >
< javac srcdir ="${build.src}"
destdir
="${build.dest}"
debug
="${debug}"
optimize
="${optimize}" >
< classpath refid ="classpath" />
</ javac >
</ target >

<!-- =================================================================== -->
<!-- Createstheclasspackage -->
<!-- =================================================================== -->
< target name ="jar" depends ="build" >
< jar jarfile ="${lib.dir}/${name}.jar"
basedir
="${build.dest}"
includes
="**" />
</ target >

<!-- =================================================================== -->
<!-- CreatestheAPIdocumentation -->
<!-- =================================================================== -->
< target name ="javadoc" depends ="build" >
< mkdir dir ="${build.javadocs}" />
< javadoc packagenames ="${packages}"
sourcepath
="${build.src}"
destdir
="${build.javadocs}"
author
="true"
version
="true"
use
="true"
splitindex
="true"
windowtitle
="${Name}API"
doctitle
="${Name}" >
< classpath refid ="classpath" />
</ javadoc >
</ target >

<!-- =================================================================== -->
<!-- Cleantargets -->
<!-- =================================================================== -->
< target name ="clean" depends ="init" >
< delete dir ="${build.src}" />
< delete dir ="${build.dest}/org" />
< delete dir ="${build.dest}/com" />
< delete >
< fileset dir ="${build.dest}" includes ="**/*.class" />
</ delete >
</ target >
</ project >
<!-- Endoffile -->

缺省任务:usage打印帮助文档,告诉有那些任务选项:可用的有build,jar,javadoc和clean.

初始化环境变量:init
所有任务都基于一些基本环境变量的设置初始化完成,是后续其他任务的基础,在环境初始化过程中,有2点比较可以方便设置:

1除了使用却缺省的property设置了JAVA源路径和输出路径外,引用了一个外部的build.properties文件中的设置,
< property file ="${basedir}/build.properties" />
这样大部分简单配置用户只要会看懂build.properties就可以了,毕竟XML比起keyvalue的属性文件还是要可读性差一些。用build.properties也可以方便其他用户从编译的细节中解放出来。

2CLASSPATH设置:使用了其中的:
< path id ="classpath" >
< pathelement path ="${jsdk_jar}" />
< fileset dir ="${lib.dir}" >
< include name ="**/*.jar" />
</ fileset >
</ path >
则相当于设置了:CLASSPATH=/path/to/resin/lib/jsdk23.jar;/path/to/project/lib/*.jar;

文件复制:prepare-src
创建临时SRC存放目录和输出目录。
<!-- =================================================================== -->
<!-- Preparesthesourcecode -->
<!-- =================================================================== -->
< target name ="prepare-src" depends ="init" >
<!-- createdirectories -->
< mkdir dir ="${build.src}" />
< mkdir dir ="${build.dest}" />

<!-- copysrcfiles -->
< copy todir ="${build.src}" >
< fileset dir ="${src.dir}" />
</ copy >
</ target >

编译任务:build
编译时的CLASSPATH环境通过一下方式找到引用一个path对象
< classpath refid ="classpath" />

打包任务:jar
对应用打包生成项目所写名的.jar文件
<!-- =================================================================== -->
<!-- Createstheclasspackage -->
<!-- =================================================================== -->
< target name ="jar" depends ="build" >
< jar jarfile ="${lib.dir}/${name}.jar"
basedir
="${build.dest}"
includes
="**" />
</ target >

生成JAVADOC文档任务:javadoc
<!-- =================================================================== -->
<!-- CreatestheAPIdocumentation -->
<!-- =================================================================== -->
< target name ="javadoc" depends ="build" >
< mkdir dir ="${build.javadocs}" />
< javadoc packagenames ="${packages}"
sourcepath
="${build.src}"
destdir
="${build.javadocs}"
author
="true"
version
="true"
use
="true"
splitindex
="true"
windowtitle
="${Name}API"
doctitle
="${Name}" >
< classpath
分享到:
评论

你可能感兴趣的:(Web,xml,ant,脚本,Lucene)