Ant的使用
Ant的使用
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Ant,以我自身的理解,它的作用类似与Linux下的makefile,可以对软件项目进行编译、生成文档、单元测试、打包、部署等;但又不同与makefile,因为makefile是基于shell的构建工具,但Ant是基于Java的构建工具,且使用Java语言可以很容易的对它进行扩展,Ant是基于XML的书写格式。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
一、安装Ant
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
1、 先将Ant解压到一个目录,假如解压到D:\ant
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2、 设置环境变量
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
set ANT_HOME=d:\ant
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
set PATH=%PATH%;%ANT_HOME%\bin
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
二、使用Ant
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Ant的构建文件是用XML格式书写的,每个build文件包含一个project和至少一个默认的target。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="UTF-8"?>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<project name="jartest" default="jar" basedir=".">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<target name="jar" depends="war">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<jar jarfile="${basedir}/Operation.jar">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<fileset dir="bin">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<include name="**/*.class" />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</fileset>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<!--
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<fileset dir="src">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<include name="jndi.properties"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</fileset>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-->
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</jar>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</target>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<target name="war">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<war warfile="OperationTest.war" webxml="web/WEB-INF/web.xml">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<fileset dir="web">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<include name="**/*.jsp"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</fileset>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</war>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</target>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</project>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
1、 Project
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Project有三个属性name,default,basedir
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Name:Project的名字
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Default:build文件运行时默认的target
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Basedir:进行项目构建的根目录,如果没有设置此项,则默认与build文件同目录
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2、 Target
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
一个Target可以依赖于其它多个Target,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<target name="A"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<target name="B" depends="A"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
想要执行B必需先执行A
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Target的属性:name,depends,if,unless,description
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Name:Target的名字
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Depends:执行当前Target时需要依赖的Target
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
If:这个属性的名字必需设置,当前的Target才能执行
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<target name="A" if="file"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Unless:这个属性的名字必需不能设置,当前的Target才能执行
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<target name="A" unless="file"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Description:对当前Target的一段描述
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3、 Tasks
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
具体需求执行的任务,这个就有很多了,如:WAR, EAR, JAVAC, JAVA, JAR, COPY, COPYDIR, COPYFILE, MKDIR, MOVE, DELETE, ECHO, EXEC, UNZIP, ZIP, TAR, UNJAR, UNTAR, UNWAR, SCP, FTP, TELNET, 等等,以下是各Task的属性介绍:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
(1) Javac:编译Java源文件
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Srcdir:Java文件的目录
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Destdir:Class文件的保存目录
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Includes:需要包含哪些文件
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Excludes:不包含哪些文件
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Classpath:编译时需要引用的classpath
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Debug:编译时是否包含debug信息
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<javac destdir="${build}" classpath="xyz.jar" debug="on">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<src path="${src}"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<src path="${src2}"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<include name="mypackage/p1/**"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<include name="mypackage/p2/**"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<exclude name="mypackage/p1/testpackage/**"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</javac>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
(2) Java:运行class文件
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Classname:需要执行的class文件名
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Jar:需要执行的jar文件,必须包含程序入口类,有Main方法的类
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Args:执行class需要的参数
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Classpath:需要使用的classpath
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<java jar="dist/test.jar" fork="true" failonerror="true" maxmemory="128m">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<arg value="-h"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<classpath>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<pathelement location="dist/test.jar"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<pathelement path="${java.class.path}"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</classpath>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</java>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
(3) Jar:将多个class文件打成一个jar包
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Destfile:需要创建的jar文件名
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Basedir:文件的来源
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Includes:需要包含哪些文件
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Excludes:不包含哪些文件
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<jar destfile="${dist}/lib/app.jar"
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
basedir="${build}/classes"
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
includes="mypackage/test/**"
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
excludes="**/Test.class"
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
(4) War:将文件打包成War文件
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Destfile:需要创建的war文件名
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Webxml:web.xml文件的路径及文件名
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Basedir:文件的来源
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Includes:需要包含哪些文件
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Excludes:不包含哪些文件
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<war destfile="myapp.war" webxml="src/metadata/myapp.xml">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<fileset dir="src/html/myapp"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<fileset dir="src/jsp/myapp"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<lib dir="thirdparty/libs">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<exclude name="jdbc1.jar"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</lib>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<classes dir="build/main"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<zipfileset dir="src/graphics/images/gifs"
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
prefix="images"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</war>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
(5) Ear:将文件打包成Ear文件
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Destfile:需要创建的ear文件名
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
appxml:META-INF/application.xml文件的路径及文件名
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Basedir:文件的来源
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Includes:需要包含哪些文件
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Excludes:不包含哪些文件
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<ear destfile="${build.dir}/myapp.ear" appxml="${src.dir}/metadata/application.xml">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<fileset dir="${build.dir}" includes="*.jar,*.war"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</ear>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
(6) Mkdir:创建一个目录
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<mkdir dir="${dist}"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
(7) Delete:删除一个文件,或文件夹及其包含的文件
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
File:需要删除的文件名
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Dir:需要删除的目录
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<delete file="/lib/ant.jar"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<delete dir="lib"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<delete>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<fileset dir="." includes="**/*.bak"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</delete>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4、 Properties
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
一个Project可以设置多个Property,可以在build文件内使用,也可以通过Ant命令使用
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
(1) 在build文件内
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<property name="build" location="build"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<delete dir="${build}"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
(2) 通过Ant命令,使用选项为:-Dproperty=value
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<property name="build" location="build"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
执行Ant命令:ant –Dbuild=aa 则location的值就变为aa了
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
设置Property的六种方式
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
(1) 通过name,value的属性设置
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<property name="foo.dist" value="dist"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
(2) 通过name,refid的属性设置
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
(3) 通过file,url,resource属性设置,foo.properties是键值对的属性文件
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<property file="foo.properties"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<property resource="foo.properties"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<property url="http://www.mysite.com/bla/props/foo.properties"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
(4) 通过environment属性设置,获得环境变量
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<property environment="env"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<echo message="ANT_HOME is set to = ${env.ANT_HOME}"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
三、运行Ant
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ant [options] [target [target2 [target3]
]]
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Options:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-help, -h print this message
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-projecthelp, -p print project help information
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-version print the version information and exit
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-diagnostics print information that might be helpful to
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
diagnose or report problems.
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-quiet, -q be extra quiet
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-verbose, -v be extra verbose
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-debug, -d print debugging information
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-emacs, -e produce logging information without adornments
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-lib <path> specifies a path to search for jars and classes
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-logfile <file> use given file for log
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-l <file> ''
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-logger <classname> the class which is to perform logging
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-listener <classname> add an instance of class as a project listener
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-noinput do not allow interactive input
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-buildfile <file> use given buildfile
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-file <file> ''
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-f <file> ''
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-D<property>=<value> use value for given property
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-keep-going, -k execute all targets that do not depend
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
on failed target(s)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-propertyfile <name> load all properties from file with -D
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
properties taking precedence
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-inputhandler <class> the class which will handle input requests
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-find <file> (s)earch for buildfile towards the root of
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-s <file> the filesystem and use it
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-nice number A niceness value for the main thread:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
1 (lowest) to 10 (highest); 5 is the default
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-nouserlib Run ant without using the jar files from ${user.home}/.ant/lib
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-noclasspath Run ant without using CLASSPATH
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
如:ant -buildfile test.xml -Dbuild=build/classes dist
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
posted on
2008-07-28 10:28
SunWentao
阅读(
382)
评论()
编辑
收藏
举报