ANT的安装/配置
--------------------------------------------------------------------------------
内容摘要:
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 完成以上所有任务:jar javadoc
而多个任务之间往往又包含了一定了依赖关系:比如把整个应用打包任务(jar)的这个依赖于编译任务(build),而编译任务又依赖于整个环境初始化任务(init)等。
注:我看到很多项目的ant脚本中的命名基本上都是一致的,比如:编译一般叫build或者compile;打包一般叫jar或war;生成文档一般命名为javadoc或javadocs;执行全部任务all。在每个任务的中,ANT会根据配置调用一些外部应用并配以相应参数执行。虽然ANT可调用的外部应用种类非常丰富,但其实最常用的就2,3个:比如javac
javadoc jar等。
ANT的安装
解包后在系统可执行路径中加入指向ant的bin的路径就可以了,比如可以在GNU/Linux上把以下配置加入/etc/profile中:
export
ANT_HOME=/home/ant
export
JAVA_HOME=/usr/java/j2sdk1.4.1
export
PATH=$PATH:$JAVA_HOME/bin:$ANT_HOME/bin
这样执行ant
后,如果不指定配置文件ant会缺省找build.xml这个配置文件,并根据配置文件执行任务,缺省的任务设置可以指向最常用的任务,比如: build,或指向打印帮助信息:usage,告诉用户有那些脚本选项可以使用。
ANT的使用
最好的学习过程就是看懂那些open
source项目中的build.xml脚本,然后根据自己的需要简化成一个更简单的,ANT和APACHE上很多非常工程派的项目:简单易用,而且适应性非常强,因为这些项目的建立往往来源于开发人员日常最直接的需求。
以下是的一个WebLucene应用的例子:修改自JDOM的build.xml:
<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 -->
缺省任务:usage
打印帮助文档,告诉有那些任务选项:可用的有build, jar, javadoc和clean.
初始化环境变量:init
所有任务都基于一些基本环境变量的设置初始化完成,是后续其他任务的基础,在环境初始化过程中,有2点比较可以方便设置:
1
除了使用却缺省的property设置了JAVA源路径和输出路径外,引用了一个外部的build.properties文件中的设置,
<property
file=
"${basedir}/build.properties"
/>
这样大部分简单配置用户只要会看懂build.properties就可以了,毕竟XML比起key
value的属性文件还是要可读性差一些。用build.properties也可以方便其他用户从编译的细节中解放出来。
2
CLASSPATH设置:使用了其中的:
<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存放目录和输出目录。
<!-- =================================================================== -->
<!-- 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>
编译任务:build
编译时的CLASSPATH环境通过一下方式找到引用一个path对象
<classpath
refid=
"classpath"
/>
打包任务:jar
对应用打包生成项目所写名的.jar文件
<!-- =================================================================== -->
<!-- Creates the
class
package
-->
<!-- =================================================================== -->
<target name=
"jar"
depends=
"build"
>
<jar jarfile=
"${lib.dir}/${name}.jar"
basedir=
"${build.dest}"
includes=
"**"
/>
</target>
生成JAVADOC文档任务:
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>
清空临时编译文件:clean
<!-- =================================================================== -->
<!-- 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>
TODO:
更多任务/扩展:(样例)
测试任务:JUnit测试
代码风格检查任务:CheckStyle,Jalopy等
邮件警报任务:可以把以上这些任务的输出警告发送到制定的用户列表中,这个任务可以设置每天自动运行。
================================================
原文:http://blog.csdn.net/cyjch/article/details/51498333
作者:超越
超越的主页:http://blog.csdn.net/cyjch
================================================