Ant的使用

Ant的使用 
一、安装Ant 
1、  先将Ant解压到一个目录,假如解压到D:\ant 
2、  设置环境变量 
ANT_HOME=d:\ant 
PATH=%PATH%;%ANT_HOME%\bin 

二、使用Ant 
Ant的构建文件是用XML格式书写的,每个build文件包含一个project和至少一个默认的target。 
<?xml version="1.0" encoding="UTF-8"?> 
<project name="jartest" default="jar" basedir="."> 
       <target name="jar" depends="war"> 
              <jar jarfile="${basedir}/Operation.jar"> 
                     <fileset dir="bin"> 
                            <include name="**/*.class" /> 
                     </fileset> 
                     <!-- 
                     <fileset dir="src"> 
                            <include name="jndi.properties"/> 
                     </fileset> 
                     --> 
              </jar> 
       </target> 
       <target name="war"> 
              <war warfile="OperationTest.war" webxml="web/WEB-INF/web.xml"> 
                     <fileset dir="web"> 
                            <include name="**/*.jsp"/> 
                     </fileset> 
              </war> 
       </target> 
</project> 

1、  Project 
Project有三个属性name,default,basedir 
Name:Project的名字 
Default:build文件运行时默认的target 
Basedir:进行项目构建的根目录,如果没有设置此项,则默认与build文件同目录 

2、  Target 
一个Target可以依赖于其它多个Target, 
            <target name="A"/> 
            <target name="B" depends="A"/> 
想要执行B必需先执行A 
Target的属性:name,depends,if,unless,description 
Name:Target的名字 
Depends:执行当前Target时需要依赖的Target 
If:这个属性的名字必需设置,当前的Target才能执行 
<target name="A" if="file"/> 
Unless:这个属性的名字必需不能设置,当前的Target才能执行 
<target name="A" unless="file"/> 
Description:对当前Target的一段描述 

3、  Tasks 
具体需求执行的任务,这个就有很多了,如:WAR, EAR, JAVAC, JAVA, JAR, COPY, COPYDIR, COPYFILE, MKDIR, MOVE, DELETE, ECHO, EXEC, UNZIP, ZIP, TAR, UNJAR, UNTAR, UNWAR, SCP, FTP, TELNET, 等等,以下是各Task的属性介绍: 

(1)  Javac:编译Java源文件 
         Srcdir:Java文件的目录 
         Destdir:Class文件的保存目录 
         Includes:需要包含哪些文件 
         Excludes:不包含哪些文件 
         Classpath:编译时需要引用的classpath 
         Debug:编译时是否包含debug信息 
  <javac destdir="${build}" classpath="xyz.jar" debug="on"> 
    <src path="${src}"/> 
                            <src path="${src2}"/> 
                            <include name="mypackage/p1/**"/> 
                            <include name="mypackage/p2/**"/> 
                            <exclude name="mypackage/p1/testpackage/**"/> 
                             </javac> 

(2)  Java:运行class文件 
  Classname:需要执行的class文件名 
  Jar:需要执行的jar文件,必须包含程序入口类,有Main方法的类 
  Args:执行class需要的参数 
  Classpath:需要使用的classpath 
                     <java jar="dist/test.jar" fork="true" failοnerrοr="true" maxmemory="128m"> 
                        <arg value="-h"/> 
                        <classpath> 
                       <pathelement location="dist/test.jar"/> 
                       <pathelement path="${java.class.path}"/> 
                        </classpath> 
               </java> 

(3)  Jar:将多个class文件打成一个jar包 
Destfile:需要创建的jar文件名 
Basedir:文件的来源 
         Includes:需要包含哪些文件 
         Excludes:不包含哪些文件 
                    <jar destfile="${dist}/lib/app.jar" 
                      basedir="${build}/classes" 
                      includes="mypackage/test/**" 
                      excludes="**/Test.class" 
                    /> 

(4)  War:将文件打包成War文件 
Destfile:需要创建的war文件名 
Webxml:web.xml文件的路径及文件名 
Basedir:文件的来源 
         Includes:需要包含哪些文件 
         Excludes:不包含哪些文件 
                     <war destfile="myapp.war" webxml="src/metadata/myapp.xml"> 
                          <fileset dir="src/html/myapp"/> 
                          <fileset dir="src/jsp/myapp"/> 
                          <lib dir="thirdparty/libs"> 
                          <exclude name="jdbc1.jar"/> 
                          </lib> 
                          <classes dir="build/main"/> 
                          <zipfileset dir="src/graphics/images/gifs" 
                          prefix="images"/> 
                  </war> 

(5)  Ear:将文件打包成Ear文件 
Destfile:需要创建的ear文件名 
appxml:META-INF/application.xml文件的路径及文件名 
Basedir:文件的来源 
         Includes:需要包含哪些文件 
         Excludes:不包含哪些文件 
                     <ear destfile="${build.dir}/myapp.ear" appxml="${src.dir}/metadata/application.xml"> 
                                 <fileset dir="${build.dir}" includes="*.jar,*.war"/> 
               </ear> 

(6)  Mkdir:创建一个目录 
<mkdir dir="${dist}"/> 

(7)  Delete:删除一个文件,或文件夹及其包含的文件 
File:需要删除的文件名 
Dir:需要删除的目录 
<delete file="/lib/ant.jar"/> 
<delete dir="lib"/> 
                  <delete> 
                      <fileset dir="." includes="**/*.bak"/> 
                  </delete> 

4、  Properties 
一个Project可以设置多个Property,可以在build文件内使用,也可以通过Ant命令使用 
(1) 在build文件内 
          <property name="build" location="build"/> 
          <delete dir="${build}"/> 
(2) 通过Ant命令,使用选项为:-Dproperty=value 
          <property name="build" location="build"/> 
          执行Ant命令:ant –Dbuild=aa 则location的值就变为aa了 
              设置Property的六种方式 
(1) 通过name,value的属性设置 
         <property name="foo.dist" value="dist"/> 
(2) 通过name,refid的属性设置 
(3) 通过file,url,resource属性设置,foo.properties是键值对的属性文件 
         <property file="foo.properties"/> 
         <property resource="foo.properties"/> 
         <property url="http://www.mysite.com/bla/props/foo.properties"/> 
(4) 通过environment属性设置,获得环境变量 
         <property environment="env"/> 
        <echo message="ANT_HOME is set to = ${env.ANT_HOME}"/> 

三、运行Ant 
ant [options] [target [target2 [target3] ...]] 
Options: 
  -help, -h              print this message 
  -projecthelp, -p       print project help information 
  -version               print the version information and exit 
  -diagnostics           print information that might be helpful to 
                         diagnose or report problems. 
  -quiet, -q             be extra quiet 
  -verbose, -v           be extra verbose 
  -debug, -d             print debugging information 
  -emacs, -e             produce logging information without adornments 
  -lib <path>            specifies a path to search for jars and classes 
  -logfile <file>        use given file for log 
    -l     <file>                '' 
  -logger <classname>    the class which is to perform logging 
  -listener <classname>  add an instance of class as a project listener 
  -noinput               do not allow interactive input 
  -buildfile <file>      use given buildfile 
    -file    <file>              '' 
    -f       <file>              '' 
  -D<property>=<value>   use value for given property 
  -keep-going, -k        execute all targets that do not depend 
                         on failed target(s) 
  -propertyfile <name>   load all properties from file with -D 
                         properties taking precedence 
  -inputhandler <class>  the class which will handle input requests 
  -find <file>           (s)earch for buildfile towards the root of 
    -s  <file>           the filesystem and use it 
  -nice  number          A niceness value for the main thread: 
                         1 (lowest) to 10 (highest); 5 is the default 
  -nouserlib             Run ant without using the jar files from ${user.home}/.ant/lib 
  -noclasspath           Run ant without using CLASSPATH 

如:ant -buildfile test.xml -Dbuild=build/classes dist
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值