当一个代码项目大了以后,每次重新编译,打包,测试等都会变得非常复杂而且重复,因此c语言中有make脚本来帮助这些工作的批量完成。在Java中应用是平台无关性的,当然不会用平台相关的make脚本来完成这些批处理任务了,ANT本身就是这样一个流程脚本引擎,用于自动化调用程序完成项目的编译,打包,测试等。除了基于JAVA是平台无关的外,脚本的格式是基于XML的,比make脚本来说还要好维护一些。
每个ant脚本(缺省叫build.xml)中设置了一系列任务(target):比如对于一个一般的项目可能需要有以下任务。
<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:
更多任务/扩展:(样例)