??xml version="1.0" encoding="utf-8" standalone="yes"?> 内容摘要Q?br>ant是一个基于JAVA的自动化脚本引擎Q脚本格式ؓ(f)XML。除?jin)做JAVA~译相关d外,ANTq可以通过插g实现很多应用的调用?br> 当一个代码项目大?jin)以后,每次重新~译Q打包,试{都?x)变得非常复杂而且重复Q因此c语言中有make脚本来帮助这些工作的扚w完成。在Java 中应用是q_无关性的Q当然不?x)用q_相关的make脚本来完成这些批处理d?jin),ANT本n是q样一个流E脚本引擎,用于自动化调用程序完成项目的~译Q打包,试{。除?jin)基于JAVA是^台无关的外,脚本的格式是ZXML的,比make脚本来说q要好维护一些?br> 每个ant脚本Q缺省叫build.xmlQ中讄?jin)一pdd(target)Q比如对于一个一般的目可能需要有以下d?/p>
Jakarta ANT:
ANT的基本概念:(x)Java的Makefile
而多个Q务之间往往又包含了(jin)一定了(jin)依赖关系Q比如把整个应用打包d(jar)的这个依赖于~译d(build)Q而编译Q务又依赖于整个环境初始化d(init){?br>
注:(x)我看到很多项目的ant脚本中的命名基本上都是一致的Q比如:(x)~译一般叫build或者compileQ打包一般叫jar或warQ生成文一般命名ؓ(f)javadoc或javadocsQ执行全部Q务all。在每个d的中QANT?x)根据配|调用一些外部应用ƈ配以相应参数执行。虽然ANT可调用的外部应用U类非常丰富Q但其实最常用的就2Q?个:(x)比如javac javadoc jar{?
ANT的安?/h2>
解包后在pȝ可执行\径中加入指向ant的bin的\径就可以?jin),比如可以在GNU/Linux上把以下配置加入/etc/profile中:(x)
export ANT_HOME=/home/ant
export JAVA_HOME=/usr/java/j2sdk1.4.1
export PATH=$PATH:$JAVA_HOME/bin:$ANT_HOME/bin
q样执行ant 后,如果不指定配|文件ant?x)缺省找build.xmlq个配置文gQƈҎ(gu)配置文g执行dQ缺省的d讄可以指向最常用的Q务,比如Q?buildQ或指向打印帮助信息QusageQ告诉用h那些脚本选项可以使用?br>
ANT的?br>
<project default="usage" basedir=".">
<!-- =================================================================== -->
<!-- Initialization target -->
<!-- =================================================================== -->
<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>
<!-- =================================================================== -->
<!-- Help on usage -->
<!-- =================================================================== -->
<target name="usage" depends="init">
<echo message="${Name} Build file"/>
<echo message="-------------------------------------------------------------"/>
<echo message=""/>
<echo message=" available targets are:"/>
<echo message=""/>
<echo message=" jar --> generates the ${name}.jar file"/>
<echo message=" build --> compiles the source code"/>
<echo message=" javadoc --> generates the API documentation"/>
<echo message=" clean --> cleans up the directory"/>
<echo message=""/>
<echo message=" Please rename build.properties.default to build.properties"/>
<echo message=" and edit build.properties to specify JSDK 2.3 classpath."/>
<echo message=""/>
<echo message=" See the comments inside the build.xml file for more details."/>
<echo message="-------------------------------------------------------------"/>
<echo message=""/>
<echo message=""/>
</target>
<!-- =================================================================== -->
<!-- Prepares the source code -->
<!-- =================================================================== -->
<target name="prepare-src" depends="init">
<!-- create directories -->
<mkdir dir="${build.src}"/>
<mkdir dir="${build.dest}"/>
<!-- copy src files -->
<copy todir="${build.src}">
<fileset dir="${src.dir}"/>
</copy>
</target>
<!-- =================================================================== -->
<!-- Compiles the source directory -->
<!-- =================================================================== -->
<target name="build" depends="prepare-src">
<javac srcdir="${build.src}"
destdir="${build.dest}"
debug="${debug}"
optimize="${optimize}">
<classpath refid="classpath"/>
</javac>
</target>
<!-- =================================================================== -->
<!-- Creates the class package -->
<!-- =================================================================== -->
<target name="jar" depends="build">
<jar jarfile="${lib.dir}/${name}.jar"
basedir="${build.dest}"
includes="**"/>
</target>
<!-- =================================================================== -->
<!-- Creates the API documentation -->
<!-- =================================================================== -->
<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>
<!-- =================================================================== -->
<!-- Clean targets -->
<!-- =================================================================== -->
<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>
<!-- End of file -->
~省dQusage 打印帮助文Q告诉有那些d选项Q可用的有build, jar, javadoc和clean.
初始化环境变量:(x)init
所有Q务都Z一些基本环境变量的讄初始化完成,是后l其他Q务的基础Q在环境初始化过E中Q有2Ҏ(gu)较可以方便设|:(x)
1 除了(jin)使用却缺省的property讄?jin)JAVA源\径和输出路径外,引用?jin)一个外部的build.properties文g中的讄Q?br><property file="${basedir}/build.properties" />
q样大部分简单配|用户只要会(x)看懂build.properties可以了(jin)Q毕竟XML比vkey value的属性文件还是要可读性差一些。用build.properties也可以方便其他用户从~译的细节中解放出来?br>
2 CLASSPATH讄Q用了(jin)其中的:(x)
<path id="classpath">
<pathelement path="${jsdk_jar}"/>
<fileset dir="${lib.dir}">
<include name="**/*.jar"/>
</fileset>
</path>
则相当于讄?jin)?x)CLASSPATH=/path/to/resin/lib/jsdk23.jar; /path/to/project/lib/*.jar;
文g复制Qprepare-src
创徏临时SRC存放目录和输出目录?br> <!-- =================================================================== -->
<!-- Prepares the source code -->
<!-- =================================================================== -->
<target name="prepare-src" depends="init">
<!-- create directories -->
<mkdir dir="${build.src}"/>
<mkdir dir="${build.dest}"/>
<!-- copy src files -->
<copy todir="${build.src}">
<fileset dir="${src.dir}"/>
</copy>
</target>
~译dQbuild
~译时的CLASSPATH环境通过一下方式找到引用一个path对象
<classpath refid="classpath"/>
打包dQjar
对应用打包生成项目所写名?jar文g
<!-- =================================================================== -->
<!-- Creates the class package -->
<!-- =================================================================== -->
<target name="jar" depends="build">
<jar jarfile="${lib.dir}/${name}.jar"
basedir="${build.dest}"
includes="**"/>
</target>
生成JAVADOC文d: javadoc
<!-- =================================================================== -->
<!-- Creates the API documentation -->
<!-- =================================================================== -->
<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>
清空临时~译文gQclean
<!-- =================================================================== -->
<!-- Clean targets -->
<!-- =================================================================== -->
<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>
TODOQ?br>更多d/扩展Q(样例Q?br>
参考资料:(x)
http://ant.apache.org
]]>