Terry.Li-彬

          虛其心,可解天下之問;專其心,可治天下之學;靜其心,可悟天下之理;恒其心,可成天下之業。

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            143 隨筆 :: 344 文章 :: 130 評論 :: 0 Trackbacks
          ant 是apache的java子項目"jakarta"的子項目.你可以選擇當前的版本,,window版
          解壓后ant_home用來方便訪問。并確保你也設置了java_home 。
          set ant_home=D:"java"kit"ant"jakarta-ant-1.5.1 這是我的目錄
          hello ant

          我們要開發一個java類:其內容只有一句,輸出"hello ant"字符串。并使用ant完成編譯和運行工作,這個例子只是為了跑通ant,不附加多余的東西。

          下面是:“hello.ant.HelloAnt.java”文件。

          package hello.ant;

          public class HelloAnt{
          public static void main(String[] args){
          System.out.println("hello ant,ant 的第一次接觸,好棒!");
          }
          }

          在項目根目錄(hello-ant")寫1個文件:ant執行配置文件build.xml

          “build.xml”文件

          <?xml version="1.0" encoding="GB2312" ?>

          <!-- 一個項目,可包含很多任務組(target) -->
          <project default="main" basedir=".">

          <!-- 項目中的一個任務組,可包含很多任務(task:javac,java...) -->
          <target name="main">

          <!--編譯-->
          <javac srcdir="src"main"hello"ant" destdir="build"classes"/>

          <!--運行-->
          <java classname="hello.ant.HelloAnt">
          <classpath>
          <pathelement path="build"classes"/>
          </classpath>
          </java>

          </target>
          </project>


          ok,一切大功告成,哦,不,還沒有運行它。

          dos下進入hello-ant的目錄,即build.xml所在的目錄,我們要用ant工具執行它 ,

          執行: %ant_home%/bin/ant -file build.xml 用ant工具執行當前目錄下的配置文件build.xml

          或 :ant -file build.xml 你如果設置%ant_home%/bin到path中

          這次ok了,這是答案:

          命令提示符窗口
          D:"temp"hello-ant>ant -file build.xml
          Build build.xml

          main:
          [javac] Compiling 1 source file to D:"temp"hello-ant"build"classes
          [java] hello ant,ant 的第一次接觸,好棒!

          BUILD SUCCESSFUL
          Total time: 2 seconds
          D:"temp"hello-ant>

          檢查一下build/classes目錄,哦,看到編譯過的文件就在這里:
          build/classes/hello/ant/HelloAnt.class.

          hello ant 進級

          我們要改進build.xml,讓它做更多的事情:

          定義全局變量
          初始化,主要是建立目錄
          編譯 (已有)
          打包為jar
          建立API documentation
          生成distribution產品
          凡事都講究平衡,你要ant給你做更多事,當然要累一點點,不過只用累一次,以后的代碼修改后的構建都是"一鍵式"完成,我們制作一個hello的簡單例子,你可以自己做j2ee的練習。

          我們要擴充目錄結構,使它更像回事:

          :"src,"docs,"lib是自己組織的文件結構,"build,"dist是ant動態生成的成品。

          "src 源文件:java源,源,jsp源,xml配置.....
          "src"main java源
          "src" window,unix,liunx的執行,我們的簡單只有一個:
          run.bat: java hello.ant.HelloAnt

          "docs 手寫說明文檔
          "lib 程序所需類庫的jar,比如j2ee.jar,mail,jar...

          "build 用ant動態生成的構建目錄
          "build"classes 編譯的類文件
          "build"docs copy ""docs"的手寫說明文檔,和ant生成的api文檔
          "build"lib 放置我們自己的HelloAnt.class打包成品hello-ant.jar

          "dist"bin copy ""src"" 得執行文件
          "dist"docs copy ""build"docs" 的文檔
          "dist"lib 除了copy ""build"lib"下的hello-ant.jar外,
          還應copy ""lib"的程序所需jar,這里我們沒有。

          以上是我學老外的文件組織,大家可以按照自己的愛好組織

          我們編寫必要的文件:

          hello.ant. HelloAnt.java

          src".bat

          @echo off
          echo ========================================================
          echo 請先設置 Environment
          echo .
          echo JAVA_HOME: %JAVA_HOME%
          echo ======================================================

          %java_home%"bin"java -classpath .."lib"hello-ant.jar hello.ant.HelloAnt

          pause

          "docs"index.html 隨便寫一個手寫的文檔
          hello ant 軟件項目手冊docs
          --------------------------------------------------------------------------------
          訪問api文檔
          "build.xml 配置文件

          <?xml version="1.0" encoding="GB2312" ?>
          <!--
          =======================================================================
          hello-ant 項目 ,學習ant工具的第2個build file.

          參照ant的jakarta-ant-1.6alpha的build.xml

          Copyright (c) 2002 The Neusoft Software Foundation. All rights
          reserved.

          =======================================================================
          -->

          <!--
          文檔結構為:
          <project>
          <property/> 全局變量的定義
          <property/>...

          <target name="1"> 任務組(tasks)
          <javac></javac> 一項javac任務
          ...
          <oneTask></ontTask> 一項其它任務
          </target>

          <target name="2">
          <javac></javac>
          ...
          <oneTask></ontTask>
          </target>
          </project>

          project代表一個項目,
          default:運行到名稱為"dist"的target(任務組)
          basedir:基準路徑。
          -->
          <project default="dist" basedir=".">

          <!--
          ===================================================================
          定義屬性(property tasks)
          最好把用到的路徑呀,名稱呀都在這里定義成全局變量
          例:定義
          <property name="a" ="hello"/>
          以后就可以這樣用它:
          <property name="b" ="${a}/b"/>
          現在:b=="hello/b"
          ===================================================================
          -->

          <!--主要的系統環境屬性-->
          <property environment="env"/><!--取window,unix...的環境變量-->
          <property name="java.home" ="${env.JAVA_HOME}"/>
          <property name="ant.home" ="${env.ANT_HOME}"/>

          <!--主要的app環境屬性-->
          <property name="app.name" ="hello-ant"/>
          <property name="app.jar" ="${app.name}.jar"/>
          <property name="app.copyright" =" Copyright (c) 2002 The Neusoft Software Foundation. All rights reserved."/>


          <!--app中src的屬性-->
          <property name="src.dir" ="src" />
          <property name="src.main" ="${src.dir}/main"/>
          <property name="src." ="${src.dir}/"/>

          <!--app用到的lib-->
          <property name="lib.dir" ="lib"/>

          <!--app的build目錄中-->
          <property name="build.dir" ="build" />
          <property name="build.classes" ="${build.dir}/classes"/>
          <property name="build.docs" ="${build.dir}/docs"/>
          <property name="build.docs.api" ="${build.docs}/api"/>
          <property name="build.lib" ="${build.dir}/lib"/>

          <!--app的dist (distribution) 目錄中-->
          <property name="dist.dir" ="dist"/>
          <property name="dist.bin" ="${dist.dir}/bin"/>
          <property name="dist.docs" ="${dist.dir}/docs"/>
          <property name="dist.lib" ="${dist.dir}/lib"/>

          <!--app的docs目錄中-->
          <property name="docs.dir" ="docs"/>

          <!--
          定義一組路徑以后可以通過id重用這組路徑 ,例:
          <javac srcdir="src/main" destdir="build/classes">
          <classpath refid="classpath"/>
          </javac>
          -->
          <path id="classpath">
          <!--本項目只有一個java,用不上classpath,這里只是做個例子-->
          <pathelement location="${build.classes}"/>
          <pathelement path="${java.home}/lib/tools.jar"/>
          </path>

          <!--
          ===================================================================
          init 準備目錄(File Tasks)
          主要的目錄結構通常是不會變的,一起生成他們
          ===================================================================
          -->
          <target name="init">
          <!--清除以前目錄-->
          <delete dir="${build.dir}" fail="false" />
          <delete dir="${dist.dir}" fail="false"/>

          <!--準備目錄-->
          <mkdir dir="${build.dir}"/>
          <mkdir dir="${build.classes}"/>
          <mkdir dir="${build.docs}"/>
          <mkdir dir="${build.docs.api}"/>
          <mkdir dir="${build.lib}"/>

          <mkdir dir="${dist.dir}"/>
          <mkdir dir="${dist.bin}"/>
          <mkdir dir="${dist.lib}"/>

          </target>

          <!--
          ===================================================================
          Build the code (Compile Tasks,File Tasks)
          ===================================================================
          -->
          <target name="build" depends="init">
          <!--編譯-->
          <javac srcdir="${src.main}" destdir="${build.classes}">
          <classpath refid="classpath"/>
          </javac>
          </target>

          <!--
          ===================================================================
          打包文檔(Archive Tasks)
          Create the project jars: xxx1.jar and xxx2.jar
          ===================================================================
          -->
          <target name="jars" depends="build">
          <jar basedir="${build.classes}" jarfile="${build.lib}/${app.jar}"/>
          </target>

          <!--
          ===================================================================
          Creates the API documentation
          ===================================================================
          -->
          <target name="javadocs"
          depends="jars"
          deion="--> creates the API documentation">
          <!--copy docs 手冊... -->
          <copy todir="${build.docs}">
          <fileset dir="${docs.dir}"/>
          </copy>

          <javadoc packagenames="hello.ant.*"
          sourcepath="${src.main}"
          defaultexcludes="yes"
          destdir="${build.docs.api}"
          author="true"
          version="true"
          use="true"
          windowtitle="Docs API">
          <doctitle><![CDATA[<h1>hello ant Docs API</h1>]]></doctitle>
          <bottom><![CDATA[<i>${app.copyright}</i>]]></bottom>
          <tag name="todo" scope="all" deion="To do:" />
          </javadoc>
          </target>

          <!--
          ===================================================================
          Create the distribution that can run (Archive Tasks)
          主要是從各目錄中把該copy的copy上
          ===================================================================
          -->
          <target name="dist" depends="javadocs">
          <!--copy bin 執行文件 -->
          <copy todir="${dist.bin}">
          <fileset dir="${src.}/"/>
          </copy>
          <copy todir="${dist.docs}">
          <fileset dir="${build.docs}/"/>
          </copy>
          <!-- copy lib 文件 -->
          <copy todir="${dist.lib}">
          <fileset dir="${build.lib}/"/>
          </copy>

          </target>
          <!--
          ===================================================================
          Cleans everything(File Tasks)
          例如可以刪除build中的文件,留給你發揮吧
          ===================================================================
          -->

          </project>

          build.xml多了些,但其實很簡單:(注釋比較詳細可以參照,這里再簡單說一下)

          一個build.xml包含一個工程的自動化處理的完整xml說明,并且基本由3種東東組成:

          <project >

          1.全局變量的定義
          <property/>

          2.任務組
          <target>
          3.許多單項任務... 像copy,delete,javac,jar...
          <task1/>
          <task2/>
          <task3/>
          </target>

          </project>

          posted on 2008-02-13 22:57 禮物 閱讀(504) 評論(0)  編輯  收藏

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

          網站導航:
           
          主站蜘蛛池模板: 囊谦县| 易门县| 九龙城区| 黔江区| 东安县| 利辛县| 太谷县| 奉新县| 九龙坡区| 永新县| 佛冈县| 静海县| 太和县| 大城县| 桦甸市| 潼南县| 奉节县| 那曲县| 镇雄县| 武功县| 西峡县| 渑池县| 新沂市| 杂多县| 辰溪县| 东兰县| 汾西县| 大埔县| 岢岚县| 黄石市| 区。| 灯塔市| 长武县| 岳阳县| 聂荣县| 卓尼县| 襄垣县| 扎赉特旗| 西盟| 新绛县| 洪洞县|