我的java歷程

          Ant的使用

          Ant的使用

           

                 Ant,以我自身的理解,它的作用類似與Linux下的makefile,可以對軟件項(xiàng)目進(jìn)行編譯、生成文檔、單元測試、打包、部署等;但又不同與makefile,因?yàn)閙akefile是基于shell的構(gòu)建工具,但Ant是基于Java的構(gòu)建工具,且使用Java語言可以很容易的對它進(jìn)行擴(kuò)展,Ant是基于XML的書寫格式。

           

          一、安裝Ant

          1、  先將Ant解壓到一個(gè)目錄,假如解壓到D:\ant

          2、  設(shè)置環(huán)境變量

          set ANT_HOME=d:\ant

          set PATH=%PATH%;%ANT_HOME%\bin

           

          二、使用Ant

          Ant的構(gòu)建文件是用XML格式書寫的,每個(gè)build文件包含一個(gè)project和至少一個(gè)默認(rèn)的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有三個(gè)屬性name,default,basedir

          Name:Project的名字

          Default:build文件運(yùn)行時(shí)默認(rèn)的target

          Basedir:進(jìn)行項(xiàng)目構(gòu)建的根目錄,如果沒有設(shè)置此項(xiàng),則默認(rèn)與build文件同目錄

           

          2、  Target

          一個(gè)Target可以依賴于其它多個(gè)Target,

                      <target name="A"/>

                      <target name="B" depends="A"/>

          想要執(zhí)行B必需先執(zhí)行A

          Target的屬性:name,depends,if,unless,description

          Name:Target的名字

          Depends:執(zhí)行當(dāng)前Target時(shí)需要依賴的Target

          If:這個(gè)屬性的名字必需設(shè)置,當(dāng)前的Target才能執(zhí)行

          <target name="A" if="file"/>

          Unless:這個(gè)屬性的名字必需不能設(shè)置,當(dāng)前的Target才能執(zhí)行

          <target name="A" unless="file"/>

          Description:對當(dāng)前Target的一段描述

           

          3、  Tasks

          具體需求執(zhí)行的任務(wù),這個(gè)就有很多了,如: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:編譯時(shí)需要引用的classpath

                   Debug:編譯時(shí)是否包含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:運(yùn)行class文件

            Classname:需要執(zhí)行的class文件名

            Jar:需要執(zhí)行的jar文件,必須包含程序入口類,有Main方法的類

            Args:執(zhí)行class需要的參數(shù)

            Classpath:需要使用的classpath

                               <java jar="dist/test.jar" fork="true" failonerror="true" maxmemory="128m">

                                  <arg value="-h"/>

                                  <classpath>

                                 <pathelement location="dist/test.jar"/>

                                 <pathelement path="${java.class.path}"/>

                                  </classpath>

                         </java>

           

          (3)  Jar:將多個(gè)class文件打成一個(gè)jar包

          Destfile:需要?jiǎng)?chuàng)建的jar文件名

          Basedir:文件的來源

                   Includes:需要包含哪些文件

                   Excludes:不包含哪些文件

                              <jar destfile="${dist}/lib/app.jar"

                                basedir="${build}/classes"

                                includes="mypackage/test/**"

                                excludes="**/Test.class"

                              />

           

          (4)  War:將文件打包成War文件

          Destfile:需要?jiǎng)?chuàng)建的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:需要?jiǎng)?chuàng)建的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:創(chuàng)建一個(gè)目錄

          <mkdir dir="${dist}"/>

           

          (7)  Delete:刪除一個(gè)文件,或文件夾及其包含的文件

          File:需要?jiǎng)h除的文件名

          Dir:需要?jiǎng)h除的目錄

          <delete file="/lib/ant.jar"/>

          <delete dir="lib"/>

                            <delete>

                                <fileset dir="." includes="**/*.bak"/>

                            </delete>

           

          4、  Properties

          一個(gè)Project可以設(shè)置多個(gè)Property,可以在build文件內(nèi)使用,也可以通過Ant命令使用

          (1) 在build文件內(nèi)

                    <property name="build" location="build"/>

                    <delete dir="${build}"/>

          (2) 通過Ant命令,使用選項(xiàng)為:-Dproperty=value

                    <property name="build" location="build"/>

                    執(zhí)行Ant命令:ant –Dbuild=aa 則location的值就變?yōu)閍a了

                        設(shè)置Property的六種方式

          (1) 通過name,value的屬性設(shè)置

                   <property name="foo.dist" value="dist"/>

          (2) 通過name,refid的屬性設(shè)置

          (3) 通過file,url,resource屬性設(shè)置,foo.properties是鍵值對的屬性文件

                   <property file="foo.properties"/>

                   <property resource="foo.properties"/>

                   <property url=">

          (4) 通過environment屬性設(shè)置,獲得環(huán)境變量

                   <property environment="env"/>

                  <echo message="ANT_HOME is set to = ${env.ANT_HOME}"/>

           

          三、運(yùn)行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

           

          posted on 2007-04-10 12:32 landril 閱讀(3246) 評論(0)  編輯  收藏 所屬分類: Ant


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 罗山县| 依安县| 安丘市| 离岛区| 修武县| 新竹市| 乐都县| 昭觉县| 革吉县| 浦县| 邓州市| 霍州市| 孝昌县| 怀宁县| 韩城市| 襄城县| 富川| 海盐县| 冕宁县| 涿州市| 鹤山市| 迁安市| 青岛市| 平塘县| 皋兰县| 南漳县| 资中县| 凉山| 邯郸市| 青冈县| 阜阳市| 杭锦后旗| 台湾省| 青海省| 吴旗县| 林周县| 利川市| 济南市| 滦南县| 拉孜县| 宁阳县|