---
title: "ant编译打包学习笔记"
author: "gary_bu"
date: "2014-08-14T17:25:39Z"
tags: []
categories: ["企业架构"]
slug: "ant"
draft: false
lastmod: "2014-08-14T17:28:36Z"
id: 2104024
url: "https://gary-bu.iteye.com/blog/2104024"
---

学习了一下使用ant给web项目打包,使用ant命令预先安装ant软件,并且配置

环境变量:

ANT\_HOME=ant安装目录   

PATH=%PATH%;%ANT\_HOME%/bin

根据[http://blog.csdn.net/lip009/article/details/1226931](http://blog.csdn.net/lip009/article/details/1226931) 自己写了一个 build.xml文件

```
<?xml version="1.0" encoding="UTF-8"?>
<project name="AntTest" basedir="." default="war">
	<description>
			This is a  description for AntTest
	</description>
	<!-- 设置一些变量 -->
	<property name="webroot" location="WebRoot"></property>
	<property name="classes.dir" location="${webroot}/WEB-INF/classes"></property>
	<property name="lib.dir" location="${webroot}/WEB-INF/lib"></property>
	<property name="dist.dir" location="dist"></property>
	<property name="config.dir" location="config"></property>
	
	<path id="classpath">
			<fileset dir="${classes.dir}"></fileset>
			<fileset dir="${lib.dir}"></fileset>
		</path>
	
	<target name="init-dir" >
		<mkdir dir="${classes.dir}"/>	
		<mkdir dir="${lib.dir}"/>
		<mkdir dir="${dist.dir}"/>
		<echo>创建目录,完成.</echo>
	</target>	
	
	<target  name="src-compile">
		<javac target="1.6" srcdir="src" destdir="${classes.dir}"  classpathref="classpath" includeantruntime="false"> 
			<!-- 设置编码方式,否则控制台乱码 -->
			<compilerarg line="-encoding UTF-8"/>
		</javac>
		<echo>编译文件,完成.</echo>
	</target>
	
	<target name="copy-config">
		<copy todir="${classes.dir}">
			<fileset dir="${config.dir}" includes="**/*.*"></fileset>
		</copy>
		<echo>拷贝配置文件到classes下,完成.</echo>
	</target>
	
	<target name="war"  depends="init-dir,src-compile,copy-config">
		<war destfile="${dist.dir}/${ant.project.name}.war" basedir="${webroot}"></war>
		<echo>生成War包,完成.</echo>
	</target>

</project>
```

这只是最简单的一个buil.xml 了,如果要打jar包,还可以在里面加.