---
title: "用ant进行工程管理"
author: "danlley"
date: "2007-07-17T13:50:17Z"
tags: []
slug: "ant"
draft: false
lastmod: "2007-07-17T13:50:17Z"
id: 101987
url: "https://danlley.iteye.com/blog/101987"
---

1.典型地，一个ant工程脚本如下：

01. &lt;?xml version="1.0"?&gt;
02. &lt;project name="springapp" basedir="." default="usage"&gt;
03. &lt;property file="build.properties" /&gt;
04. &lt;path id="master-classpath"&gt;
05. &lt;target name="usage"&gt;
06. &lt;target name="build" description="Compile main source tree java files"&gt;
07. &lt;target name="deploy" depends="build" description="Deploy application"&gt;
08. &lt;target name="deploywar" depends="build" description="Deploy application as a WAR file"&gt;
09. &lt;target name="clean" description="Clean output directories"&gt;
10. &lt;target name="undeploy" description="Un-Deploy application"&gt;
11. &lt;/project&gt;

其中的property属性可以理解为ant在运行期间所要用到的常量，它是ant的基本数据类型，代表了一个String常量，该常量可以采用直接定义的方式，也可以采用将属性配置到一个properties文件中，在上面的这个例子中，我们同时使用了两种方式定义ant运行时的property属性。在ant脚本中如果要使用外部的配置属性就必须指明文件的确切位置及文件名称。如下所示：

xml 代码

1. &lt;property file="build.properties" /&gt;

其中build.properties 的文件内容如下：

properties 代码

1. # Ant properties for building the springapp
2. appserver.home=D:\\\\tomcat6
3. deploy.path=${appserver.home}/deploy
4. tomcat.manager.url=http://localhost:8080/manager
5. tomcat.manager.username=admin
6. 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：

xml 代码

1. &lt;path id="master-classpath"&gt;
2. &lt;fileset dir="${web.dir}/WEB-INF/lib"&gt;
3. &lt;include name="\*.jar" /&gt;
4. &lt;/fileset&gt;
5. &lt;pathelement path="${build.dir}" /&gt;
6. &lt;/path&gt;

   
   
   
   
3.接下来就是N多个 target 属性，ant支持定义一个默认target和多个可选target 属性，下面的代码说明了默认使用name为 usage 的 target 属性。

xml 代码

1. &lt;project name="springapp" basedir="." default="usage"&gt;

   
查看该节点信息，可以看出，在该 target 节点下仅仅定义了一系列的 echo message 信息。我们在这里定义的这些信息仅仅用于提示一些ant当前可用操作（build、deploy、deploywar、clean、undeploy）。

xml 代码

01. &lt;target name="usage"&gt;
02. &lt;echo message="" /&gt;
03. &lt;echo message="${appserver.home}      ---&gt;the server root (Danlley Wei)" /&gt;
04. &lt;echo message="${name} build file" /&gt;
05. &lt;echo message="-----------------------------------" /&gt;
06. &lt;echo message="" /&gt;
07. &lt;echo message="Available targets are:" /&gt;
08. &lt;echo message="" /&gt;
09. &lt;echo message="build     --&gt; Build the application" /&gt;
10. &lt;echo message="deploy    --&gt; Deploy application as directory" /&gt;
11. &lt;echo message="deploywar --&gt; Deploy application as a WAR file" /&gt;
12. &lt;echo message="clean     --&gt; clean the generated file which is like \*.class " /&gt;
13. &lt;echo message="undeploy  --&gt; clean all files in web server tomcat" /&gt;
14. &lt;echo message="" /&gt;
15. &lt;/target&gt;

   
4.name为 build 的 target 节点：

xml 代码

1. &lt;target name="build" description="Compile main source tree java files"&gt;
2. &lt;mkdir dir="${build.dir}" /&gt;
3. &lt;javac destdir="${build.dir}" target="1.5" debug="true" deprecation="false" optimize="false" failonerror="true"&gt;
4. &lt;src path="${src.dir}" /&gt;
5. &lt;classpath refid="master-classpath" /&gt;
6. &lt;/javac&gt;
7. &lt;/target&gt;

   
   
从该节点定义的信息可以看出，ant把整个工程的编译过程分为两步，首先创建编译路经，这里你根本不需要担心该路经是否已经存在，因为如果不存在，ant就会自动创建该工程编译路经

xml 代码

1. &lt;mkdir dir="${build.dir}" /&gt;

   
   
接下来对工程进行编译，其编译后的文件将存放在destdir="${build.dir}" 下 ，注意这里的target="1.5"  这里指明了使用JDK1.5，当然你还可以使用其它的版本。

xml 代码

1. &lt;javac destdir="${build.dir}" target="1.5" debug="true" deprecation="false" optimize="false" failonerror="true"&gt;
2. &lt;src path="${src.dir}" /&gt;
3. &lt;classpath refid="master-classpath" /&gt;
4. &lt;/javac&gt;

   
5.name为 deploy 的 target 节点，其实就是一个工程的拷贝过程，ant在发现 ${name} 路经不存在后会自动创建该目录，因此你没必要去手动创建一个${name} 目录 ：

xml 代码

1. &lt;target name="deploy" depends="build" description="Deploy application"&gt;
2. &lt;copy todir="${deploy.path}/${name}" preservelastmodified="true"&gt;
3. &lt;fileset dir="${web.dir}"&gt;
4. &lt;include name="\*\*/\*.\*" /&gt;
5. &lt;/fileset&gt;
6. &lt;/copy&gt;
7. &lt;/target&gt;

   
6.name为 deploywar 的 target 节点：

xml 代码

01. &lt;target name="deploywar" depends="build" description="Deploy application as a WAR file"&gt;
02. &lt;war destfile="${name}.war" webxml="${web.dir}/WEB-INF/web.xml"&gt;
03. &lt;fileset dir="${web.dir}"&gt;
04. &lt;include name="\*\*/\*.\*" /&gt;
05. &lt;/fileset&gt;
06. &lt;/war&gt;
07. &lt;copy todir="${deploy.path}" preservelastmodified="true"&gt;
08. &lt;fileset dir="."&gt;
09. &lt;include name="\*.war" /&gt;
10. &lt;/fileset&gt;
11. &lt;/copy&gt;
12. &lt;/target&gt;

   
7.name为 clean 的 target 节点：

xml 代码

1. &lt;target name="clean" description="Clean output directories"&gt;
2. &lt;delete&gt;
3. &lt;fileset dir="${build.dir}"&gt;
4. &lt;include name="\*\*/\*.class"/&gt;
5. &lt;/fileset&gt;
6. &lt;/delete&gt;
7. &lt;/target&gt;

   
8.name为 undeploy 的 target 节点：

xml 代码

1. &lt;target name="undeploy" description="Un-Deploy application"&gt;
2. &lt;delete&gt;
3. &lt;fileset dir="${deploy.path}/${name}"&gt;
4. &lt;include name="\*\*/\*.\*"/&gt;
5. &lt;/fileset&gt;
6. &lt;/delete&gt;
7. &lt;/target&gt;

 

xml 代码