---
title: "ant的使用（下）"
author: "hdy007"
date: "2006-12-05T15:09:19Z"
tags: []
slug: "ant"
draft: false
lastmod: "2006-12-05T15:09:19Z"
id: 37506
url: "https://java-tech.iteye.com/blog/37506"
---

四:编写build.xml  
build.xml相当Linux下的makefile,具体的实现都在build.xml中实现。  
我给给例子说明一下。  
build.xml  
\================================================================  
&lt;project name="bingo" default="build" basedir="../.." &gt;  
&lt;!--basedir设定工作目录--&gt;  
&lt;property name="version" value="1.0"/&gt;

&lt;!-- The base directory relative to which most targets are built --&gt;  
&lt;property name="base" value="."/&gt;

&lt;!-- The directory where source files are stored. --&gt;  
&lt;property name="java.source.dir" value="bingo/src"/&gt;  
&lt;!--代码保存路径--&gt;  
&lt;!-- Destination for compiled files --&gt;  
&lt;property name="javac.dest" value="bingo/classes"/&gt;  
&lt;!--class保存路径--&gt;  
&lt;!-- Destination for generated jar files --&gt;  
&lt;property name="jar.dest" value="bingo/jar"/&gt;  
&lt;!--jar文件保存路径--&gt;  
&lt;!-- Destination for documentation files generated or not --&gt;  
&lt;property name="docs" value="bingo/docs"/&gt;  
&lt;!--javadoc文件保存路径--&gt;  
&lt;!-- Destination for javadoc generated files --&gt;  
&lt;property name="javadoc.dest" value="bingo/docs"/&gt;

&lt;!-- The stem where most log4j source code is located. --&gt;  
&lt;property name="stem" value="com/bingo"/&gt;

&lt;property name="base-files" value="include"/&gt;

&lt;!-- Original manifest.mf file before filtering. --&gt;  
&lt;property name="manifest.src" value="bingo/build/manifest.mf"/&gt;

&lt;!-- Some targets needs a more precise stem. --&gt;  
&lt;property name="BSTEM" value="${java.source.dir}/${stem}"/&gt;

&lt;property name="tomcat.dir" value="c:/Apache/Tomcat"/&gt;

&lt;property name="webapp.dir" value="${tomcat.dir}/webapps/ROOT/WEB-INF/classes"/&gt;

&lt;!--List all Package used in this project --&gt;  
&lt;property name="PackageList" value="  
com.bingo,  
com.bingo.database,  
com.bingo.dbocw,  
com.bingo.util,  
com.bingo.taglibs.jndi,  
com.bingo.finance.database,  
com.bingo.finance.entity,  
com.bingo.finance.manager"  
/&gt;  
&lt;!--你的project中所有的包--&gt;  
&lt;!-- List all jar or file used in this project --&gt;  
&lt;property name="classpath" value="${classpath};  
${base-files}/tomcat/servlet.jar;  
${base-files}/tomcat/webserver.jar;  
${base-files}/log4j/log4j.jar;  
${base-files}/log4j/log4j-core.jar"

/&gt;  
&lt;!--你需要用到的包--&gt;  
&lt;target name="init"&gt;  
&lt;tstamp /&gt;  
&lt;/target&gt;

&lt;target name="build" depends="init"&gt;  
&lt;echo&gt;  
Building...  
&lt;/echo&gt;

&lt;mkdir dir="${javac.dest}" /&gt;  
&lt;javac srcdir="${java.source.dir}"  
destdir="${javac.dest}"  
classpath="${classpath}"  
debug="on"/&gt;

&lt;/target&gt;  
&lt;!-- ================================================================= --&gt;  
&lt;!-- Copy class files to tomcat dir. --&gt;  
&lt;!-- ================================================================= --&gt;  
&lt;target name="copy" depends="build"&gt;  
&lt;copy todir="${webapp.dir}/com/bingo"&gt;  
&lt;fileset dir="${javac.dest}/com/bingo"&gt;  
&lt;include name="\*.class"/&gt;  
&lt;/fileset&gt;  
&lt;/copy&gt;  
&lt;copy todir="${webapp.dir}/com/bingo/util"&gt;  
&lt;fileset dir="${javac.dest}/com/bingo/util"&gt;  
&lt;include name="\*.class"/&gt;  
&lt;/fileset&gt;  
&lt;/copy&gt;  
&lt;copy todir="${webapp.dir}/com/bingo/database"&gt;  
&lt;fileset dir="${javac.dest}/com/bingo/database"&gt;  
&lt;include name="\*.class"/&gt;  
&lt;/fileset&gt;  
&lt;/copy&gt;  
&lt;copy todir="${webapp.dir}/com/bingo/dbocw"&gt;  
&lt;fileset dir="${javac.dest}/com/bingo/dbocw"&gt;  
&lt;include name="\*.class"/&gt;  
&lt;/fileset&gt;  
&lt;/copy&gt;  
&lt;copy todir="${webapp.dir}/com/bingo/finance/database"&gt;  
&lt;fileset dir="${javac.dest}/com/bingo/finance/database"&gt;  
&lt;include name="\*.class"/&gt;  
&lt;/fileset&gt;  
&lt;/copy&gt;  
&lt;copy todir="${webapp.dir}/com/bingo/finance/entity"&gt;  
&lt;fileset dir="${javac.dest}/com/bingo/finance/entity"&gt;  
&lt;include name="\*.class"/&gt;  
&lt;/fileset&gt;  
&lt;/copy&gt;  
&lt;copy todir="${webapp.dir}/com/bingo/finance/manager"&gt;  
&lt;fileset dir="${javac.dest}/com/bingo/finance/manager"&gt;  
&lt;include name="\*.class"/&gt;  
&lt;/fileset&gt;  
&lt;/copy&gt;  
&lt;/target&gt;

&lt;!-- ================================================================= --&gt;  
&lt;!-- Remove all generated (compiled) class files. --&gt;  
&lt;!-- ================================================================= --&gt;  
&lt;target name="clean" depends="init"&gt;  
&lt;delete dir="${javac.dest}/" /&gt;  
&lt;/target&gt;

&lt;!-- ================================================================= --&gt;  
&lt;!-- Remove all backup files. --&gt;  
&lt;!-- ================================================================= --&gt;  
&lt;target name="delete" depends="init"&gt;  
&lt;delete &gt;  
&lt;fileset dir="${java.source.dir}/com/bingo"&gt;  
&lt;include name="\*.bak"/&gt;  
&lt;/fileset&gt;  
&lt;/delete&gt;  
&lt;delete &gt;  
&lt;fileset dir="${java.source.dir}/com/bingo/util"&gt;  
&lt;include name="\*.bak"/&gt;  
&lt;/fileset&gt;  
&lt;/delete&gt;  
&lt;delete &gt;  
&lt;fileset dir="${java.source.dir}/com/bingo/database"&gt;  
&lt;include name="\*.bak"/&gt;  
&lt;/fileset&gt;  
&lt;/delete&gt;  
&lt;delete &gt;  
&lt;fileset dir="${java.source.dir}/com/bingo/finance/database"&gt;  
&lt;include name="\*.bak"/&gt;  
&lt;/fileset&gt;  
&lt;/delete&gt;  
&lt;delete &gt;  
&lt;fileset dir="${java.source.dir}/com/bingo/finance/entity"&gt;  
&lt;include name="\*.bak"/&gt;  
&lt;/fileset&gt;  
&lt;/delete&gt;  
&lt;delete &gt;  
&lt;fileset dir="${java.source.dir}/com/bingo/finance/manager"&gt;  
&lt;include name="\*.bak"/&gt;  
&lt;/fileset&gt;  
&lt;/delete&gt;  
&lt;/target&gt;

&lt;!-- ================================================================= --&gt;  
&lt;!-- Remove the temporary manifest file, actual work is done in the --&gt;  
&lt;!-- dependencies. --&gt;  
&lt;!-- ================================================================= --&gt;

&lt;target name="prejar" depends="build"&gt;  
&lt;mkdir dir="${jar.dest}"/&gt;  
&lt;filter token="version" value="${version}" /&gt;  
&lt;copy file="${manifest.src}" tofile="${jar.dest}/manifest.mf"  
filtering="true"/&gt;  
&lt;/target&gt;

&lt;!-- ================================================================= --&gt;  
&lt;!-- This target Create bingo.jar --&gt;  
&lt;!-- ================================================================= --&gt;  
&lt;target name="jar" depends="prejar"&gt;  
&lt;delete file="${jar.dest}/bingo.jar"/&gt;  
&lt;jar jarfile="${jar.dest}/bingo.jar" basedir="${javac.dest}"  
manifest="${jar.dest}/manifest.mf"  
/&gt;  
&lt;/target&gt;

&lt;!-- ================================================================= --&gt;  
&lt;!-- This target builds the javadoc files. --&gt;  
&lt;!-- ================================================================= --&gt;  
&lt;target name="javadoc" depends="build,init"&gt;  
&lt;mkdir dir="${javadoc.dest}" /&gt;  
&lt;javadoc sourcepath="${java.source.dir}"  
destdir="${javadoc.dest}"  
classpath="${classpath}"  
packagenames="${PackageList}"  
version="true"  
protected="true"  
author="true"  
use="true"  
windowtitle="Bingo Free Java Code Version ${version}"  
header="Bingo Free Java Code${version}"  
/&gt;  
&lt;/target&gt;  
&lt;/project&gt;