我的java歷程

          Ant的使用

          Ant的使用

           

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

           

          一、安裝Ant

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

          2、  設置環境變量

          set ANT_HOME=d:\ant

          set 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" failonerror="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=">

          (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

           

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


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


          網站導航:
           
          主站蜘蛛池模板: 象州县| 嘉义市| 阿鲁科尔沁旗| 昆明市| 嘉兴市| 三亚市| 铜陵市| 九寨沟县| 榆中县| 宣汉县| 松滋市| 滁州市| 万全县| 青铜峡市| 江安县| 隆回县| 怀远县| 大宁县| 从江县| 常山县| 清水县| 长宁区| 灵丘县| 通城县| 洪雅县| 芷江| 怀仁县| 宁远县| 平顺县| 大田县| 仪征市| 延津县| 临沭县| 唐河县| 株洲市| 沙坪坝区| 丹棱县| 蓬莱市| 无为县| 南汇区| 朔州市|