---
title: "一个比较完整的ANT实例"
author: "5aijava"
date: "2008-12-09T15:00:35Z"
tags: []
slug: "ant"
draft: false
lastmod: "2008-12-09T15:01:35Z"
id: 289449
url: "https://5aijava.iteye.com/blog/289449"
---

**[](http://lighter.iteye.com/blog/39594)**

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;!-- name:对应工程的名字；default:需要的缺省任务(运行"ant"不指明任务时执行的任务) --&gt;  
&lt;project name="ZZ" default="dist" basedir="."&gt;  
    &lt;!-- 建立目录结构  
        src       JAVA源码编辑目录  
        lib       jar包(类库)存放目录  
        classes   编译生成的class文件存放目录  
        WebRoot   jsp文件存放地方  
    --&gt;  
    &lt;property name="src.dir" value="src"/&gt;  
    &lt;property name="webapps.dir" value="D:/Tomcat6.0/webapps/ZZ"/&gt;  
    &lt;property name="lib.dir" value="${webapps.dir}/WEB-INF/lib"/&gt;  
    &lt;property name="classes.dir" value="${webapps.dir}/WEB-INF/classes"/&gt;  
    &lt;property name="webroot.dir" value="WebRoot"/&gt;

    &lt;!-- Compilation Classpath 路径设置 --&gt;  
    &lt;path id="compile.classpath"&gt;  
        &lt;pathelement location="${classes.dir}"/&gt;  
        &lt;fileset dir="${webroot.dir}/WEB-INF/lib" includes="\*\*/\*.jar"/&gt;        
    &lt;/path&gt;  
      
    &lt;!-- Clean Target 删除tomcat的目录结构 --&gt;  
    &lt;target name="clean" description="Delete old build and dist directories"&gt;  
        &lt;delete dir="${webapps.dir}"/&gt;  
    &lt;/target&gt;  
      
    &lt;!-- Prepare Target 建立打包的目的目录结构 --&gt;  
    &lt;target name="prepare"&gt;  
        &lt;mkdir dir="${classes.dir}"/&gt;  
        &lt;mkdir dir="${lib.dir}"/&gt;  
    &lt;/target&gt;  
      
    &lt;!-- Compile Target 编译代码，在这之前先由prepare建立目录结构 --&gt;    
    &lt;target name="compile" depends="prepare" description="Compile java sources"&gt;  
          
        &lt;!--不知道为什么不能删除copy部分，删除就不能成功编译类文件,难道我设置的path没有一点作用吗？--&gt;  
       &lt;copy todir="${webapps.dir}"&gt;  
            &lt;fileset dir="${webroot.dir}"&gt;  
                &lt;include name="\*\*/\*\*"/&gt;  
            &lt;/fileset&gt;  
        &lt;/copy&gt;  
        &lt;copy todir="${lib.dir}"&gt;  
            &lt;fileset dir="${webroot.dir}/WEB-INF/lib"&gt;  
                &lt;include name="\*\*/\*.jar"/&gt;  
            &lt;/fileset&gt;  
        &lt;/copy&gt;  
        &lt;copy todir="${classes.dir}"&gt;  
            &lt;fileset dir="${src.dir}"&gt;  
                &lt;include name="\*\*/\*.xml"/&gt;  
                &lt;include name="\*\*/\*.properties" /&gt;  
            &lt;/fileset&gt;  
        &lt;/copy&gt;  
                
        &lt;!-- Compile java class as necessary --&gt;  
        &lt;javac srcdir="${src.dir}" destdir="${classes.dir}" debug="true"&gt;  
            &lt;classpath refid="compile.classpath"/&gt;  
        &lt;/javac&gt;  
    &lt;/target&gt;  
      
    &lt;!-- Deploy Target 在tomcat下建立应用目录并部署应用 --&gt;     
    &lt;target name="deploy" depends="compile" description="Deploy application to servlet container"&gt;  
        &lt;!-- Copy the contents of the build directory --&gt;      
        &lt;copy todir="${webapps.dir}"&gt;  
            &lt;fileset dir="${webroot.dir}"&gt;  
                &lt;include name="\*\*/\*\*"/&gt;  
            &lt;/fileset&gt;  
        &lt;/copy&gt;  
        &lt;copy todir="${lib.dir}"&gt;  
            &lt;fileset dir="${webroot.dir}/WEB-INF/lib"&gt;  
                &lt;include name="\*\*/\*.jar"/&gt;  
            &lt;/fileset&gt;  
        &lt;/copy&gt;  
        &lt;copy todir="${classes.dir}"&gt;  
            &lt;fileset dir="${src.dir}"&gt;  
                &lt;include name="\*\*/\*.xml"/&gt;  
                &lt;include name="\*\*/\*.properties" /&gt;