1.典型地,一个ant工程脚本如下:
- <?xml version="1.0"?>
- <project name="springapp" basedir="." default="usage">
- <property file="build.properties" />
- <path id="master-classpath">
- <target name="usage">
- <target name="build" description="Compile main source tree java files">
- <target name="deploy" depends="build" description="Deploy application">
- <target name="deploywar" depends="build" description="Deploy application as a WAR file">
- <target name="clean" description="Clean output directories">
- <target name="undeploy" description="Un-Deploy application">
- </project>
其中的property属性可以理解为ant在运行期间所要用到的常量,它是ant的基本数据类型,代表了一个String常量,该常量可以采用直接定义的方式,也可以采用将属性配置到一个properties文件中,在上面的这个例子中,我们同时使用了两种方式定义ant运行时的property属性。在ant脚本中如果要使用外部的配置属性就必须指明文件的确切位置及文件名称。如下所示:
- <property file="build.properties" />
其中build.properties 的文件内容如下:
- # Ant properties for building the springapp
- appserver.home=D:\\tomcat6
- deploy.path=${appserver.home}/deploy
- tomcat.manager.url=http://localhost:8080/manager
- tomcat.manager.username=admin
- tomcat.manager.password=tomcat
2.path属性为ant在编译整个工程的过程中提供所需的第三方资源包,ant除了拥有基本数据类型外,还提供了一些高级结构以包括一系列的String,其主要有:path/pathelement、fileset以及dirset 由于我们在前面已经定义过了web.dir常量,因此,在这里我们可以直接把他的值拿来使用(同理:build.dir),根据我们的常量设置,${web.dir}/WEB-INF/lib路经的值在ant运行期间会被转换为war/WEB-INF/lib:
- <path id="master-classpath">
- <fileset dir="${web.dir}/WEB-INF/lib">
- <include name="*.jar" />
- </fileset>
- <pathelement path="${build.dir}" />
- </path>
3.接下来就是N多个 target 属性,ant支持定义一个默认target和多个可选target 属性,下面的代码说明了默认使用name为 usage 的 target 属性。
- <project name="springapp" basedir="." default="usage">
查看该节点信息,可以看出,在该 target 节点下仅仅定义了一系列的 echo message 信息。我们在这里定义的这些信息仅仅用于提示一些ant当前可用操作(build、deploy、deploywar、clean、undeploy)。
- <target name="usage">
- <echo message="" />
- <echo message="${appserver.home} --->the server root (Danlley Wei)" />
- <echo message="${name} build file" />
- <echo message="-----------------------------------" />
- <echo message="" />
- <echo message="Available targets are:" />
- <echo message="" />
- <echo message="build --> Build the application" />
- <echo message="deploy --> Deploy application as directory" />
- <echo message="deploywar --> Deploy application as a WAR file" />
- <echo message="clean --> clean the generated file which is like *.class " />
- <echo message="undeploy --> clean all files in web server tomcat" />
- <echo message="" />
- </target>
4.name为 build 的 target 节点:
- <target name="build" description="Compile main source tree java files">
- <mkdir dir="${build.dir}" />
- <javac destdir="${build.dir}" target="1.5" debug="true" deprecation="false" optimize="false" failonerror="true">
- <src path="${src.dir}" />
- <classpath refid="master-classpath" />
- </javac>
- </target>
从该节点定义的信息可以看出,ant把整个工程的编译过程分为两步,首先创建编译路经,这里你根本不需要担心该路经是否已经存在,因为如果不存在,ant就会自动创建该工程编译路经
- <mkdir dir="${build.dir}" />
接下来对工程进行编译,其编译后的文件将存放在destdir="${build.dir}" 下 ,注意这里的target="1.5" 这里指明了使用JDK1.5,当然你还可以使用其它的版本。
- <javac destdir="${build.dir}" target="1.5" debug="true" deprecation="false" optimize="false" failonerror="true">
- <src path="${src.dir}" />
- <classpath refid="master-classpath" />
- </javac>
5.name为 deploy 的 target 节点,其实就是一个工程的拷贝过程,ant在发现 ${name} 路经不存在后会自动创建该目录,因此你没必要去手动创建一个${name} 目录 :
- <target name="deploy" depends="build" description="Deploy application">
- <copy todir="${deploy.path}/${name}" preservelastmodified="true">
- <fileset dir="${web.dir}">
- <include name="**/*.*" />
- </fileset>
- </copy>
- </target>
6.name为 deploywar 的 target 节点:
- <target name="deploywar" depends="build" description="Deploy application as a WAR file">
- <war destfile="${name}.war" webxml="${web.dir}/WEB-INF/web.xml">
- <fileset dir="${web.dir}">
- <include name="**/*.*" />
- </fileset>
- </war>
- <copy todir="${deploy.path}" preservelastmodified="true">
- <fileset dir=".">
- <include name="*.war" />
- </fileset>
- </copy>
- </target>
7.name为 clean 的 target 节点:
- <target name="clean" description="Clean output directories">
- <delete>
- <fileset dir="${build.dir}">
- <include name="**/*.class"/>
- </fileset>
- </delete>
- </target>
8.name为 undeploy 的 target 节点:
- <target name="undeploy" description="Un-Deploy application">
- <delete>
- <fileset dir="${deploy.path}/${name}">
- <include name="**/*.*"/>
- </fileset>
- </delete>
- </target>