使用ANT協助開發java項目

來源:互聯網
上載者:User

      ANT是Apache的開源項目,目前在java的項目開發中被廣泛的採用,功能非常的強大!因此我們有必要熟悉一下這個基於java的工具。

      關於ANT的相關介紹,請參考www.apache.org。

      首先我們應該安裝ANT,從apache.org下載後直接安裝到機器上就可以使用了。最好設定一下相關的環境變數,雖然很多系統能夠自動識別出來。你還是應該設定ANT_HOME,JAVA_HOME,PATH這幾個環境變數,例如你的ANT安裝在c:/ant1.6 你的java安裝在c:/j2sdk1.4.2.那麼我們可以執行下面的操作設定環境變數(winxp):
set ANT_HOME=c:/ant1.6
set JAVA_HOME= c:/j2sdk1.4.2
set PATH=%PATH%;%PATH%/bin
設定好ant以後 在command裡面執行ant -version看看是不是可以輸出ant的版本資訊確認安裝成功

      由於很多開發工具都內建了ant,所以我直接以eclipse為例說明如何用ant開發java項目,這裡的project非常簡單,建立一個項目為TestAnt,然後再src裡面寫兩個類內容如下:
package com.north;

/**
 * @author P2800
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class TestAnt
{

    public static void main(String[] args)
    {
         new MyWork().print();  
    }
}

package com.north;
/*
 * Created on 2004-7-23
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author P2800
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class MyWork
{
    public void print()
    {
        System.out.println(System.getenv("CLASSPATH"));
    }
}
        我不準備介紹過多的ant的使用,通過察看使用者手冊你能很快上手的。而是直接給出build.xml的內容並對相關的內容作一些必要的說明。你在project TestAnt裡面建立一個檔案build.xml,這個是ant預設去尋找的檔案,如果你用其他檔案名稱的話你應該使用ant -buildfile mybuildfile.xml
<project default="jar" basedir="." name="myproject">
 <description>this is my test for ant tool</description>
 <property name="src" location="src"></property>
 <property name="dist" location="dist"></property>
 <property name="classes" location="classes"/>
 
 <target name="init">
  <tstamp></tstamp>
  <mkdir dir="${dist}"/>
  <mkdir dir="${classes}"/>
 </target>
 
 <target name="compile" depends="init">
  <javac srcdir="${src}" destdir="${classes}"/>
 </target>
 
 <target name="jar" depends="compile">
  <jar destfile="${dist}/myjar-${DSTAMP}.jar" basedir="${classes}"></jar>
 </target>
</project>
這個build.xml並不複雜 他的目的是對project裡面的原始碼編譯  然後打包到dist目錄。在build.xml裡面主要有如下幾個元素:project  target  task property。其中property是讓你去定義一些屬性值,在以後的target或者task裡面去使用。每個project必須指定一個default的target來執行。每個target是task的集合,用來完成一個特定的任務,一般比單個task複雜。target的一個參數depends說明了編譯的順序,例如在這裡你要讓jar執行的話,必須compile先執行,而compile執行之前init先執行。所以順序是init->compile->jar。task是最重要的,在ant中有很多內建的task,你看一下列表就會覺得ant真是非常強大。具體你要使用哪個task就要參考他的文檔。

      選中build.xml右鍵選擇run->ant build,你將在console看到
Buildfile: C:/eclipse/workspace/TestAnt/build.xml
init:
    [mkdir] Created dir: C:/eclipse/workspace/TestAnt/dist
    [mkdir] Created dir: C:/eclipse/workspace/TestAnt/classes
compile:
    [javac] Compiling 2 source files to C:/eclipse/workspace/TestAnt/classes
    [javac] Note: C:/eclipse/workspace/TestAnt/src/com/north/MyWork.java uses or overrides a deprecated API.
    [javac] Note: Recompile with -deprecation for details.
jar:
      [jar] Building jar: C:/eclipse/workspace/TestAnt/dist/myjar-20040723.jar
BUILD SUCCESSFUL
Total time: 2 seconds
這表示編譯成功了,但是提示有個deprecated API在程式中使用了,這個留給讀者去查看一下手冊看如何顯示出哪個API?

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.