作者:佚名 來(lái)自:J2ME開發(fā)網(wǎng)
Ant 是Java平臺(tái)下非常棒的批處理命令執(zhí)行程序,能自動(dòng)完成編譯,測(cè)試,打包,部署等等一系列任務(wù)。
Ant是Java平臺(tái)下非常棒的批處理命令執(zhí)行程序,能非常方便地自動(dòng)完成編譯,測(cè)試,打包,部署等等一系列任務(wù),大大提高開發(fā)效率。如果你現(xiàn)在還沒有開始使用Ant,那就要趕快開始學(xué)習(xí)使用,使自己的開發(fā)水平上一個(gè)新臺(tái)階。
Eclipse 中已經(jīng)集成了Ant,我們可以直接在Eclipse中運(yùn)行Ant。
以前面建立的Hello工程為例,創(chuàng)建以下目錄結(jié)構(gòu):
![](http://www./uploadfiles/20041220112701100.jpg)
新建一個(gè)build.xml,放在工程根目錄下。build.xml定義了Ant要執(zhí)行的批處理命令。雖然Ant也可以使用其它文件名,但是遵循標(biāo)準(zhǔn)能更使開發(fā)更規(guī)范,同時(shí)易于與別人交流。
通常,src存放Java源文件,classes存放編譯后的class文件,lib存放編譯和運(yùn)行用到的所有jar文件,web存放JSP等web文件,dist存放打包后的jar文件,doc存放API文檔。
然后在根目錄下創(chuàng)建build.xml文件,輸入以下內(nèi)容:
<-- properies -->
<-- 定義classpath -->
<-- 初始化任務(wù) -->
<-- 編譯 -->
<-- 測(cè)試 -->
haltonfailure="false" failureproperty="tests.failed" showoutput="true">
*********************************************************** **** One or more tests failed! Check the output ... **** ***********************************************************
<-- 打包成jar -->
<-- 輸出api文檔 -->
author="true" version="true" use="true" windowtitle="Test API">
[CDATA[<1>ello, test]>doctitle> [CDATA[<>ll Rights Reserved.]>bottom>
以上xml依次定義了init(初始化),compile(編譯),test(測(cè)試),doc(生成文檔),pack(打包)任務(wù),可以作為模板。
選中Hello工程,然后選擇“Project”,“Properties”,“Builders”,“New…”,選擇“Ant Build”:
![](http://www./uploadfiles/20041220112701200.jpg)
填入Name:Ant_Builder;Buildfile:build.xml;Base Directory:${workspace_loc:/Hello}(按“Browse Workspace”選擇工程根目錄),由于用到了junit.jar包,搜索Eclipse目錄,找到j(luò)unit.jar,把它復(fù)制到Hello/lib目錄下,并添加到Ant的Classpath中:
![](http://www./uploadfiles/20041220112701300.jpg)
然后在Builder面板中鉤上Ant_Build,去掉Java Builder:
![](http://www./uploadfiles/20041220112701400.jpg)
再次編譯,即可在控制臺(tái)看到Ant的輸出:
Buildfile: F:\eclipse-projects\Hello\build.xml
init:
compile: [mkdir] Created dir: F:\eclipse-projects\Hello\classes [javac] Compiling 2 source files to F:\eclipse-projects\Hello\classes
test: [mkdir] Created dir: F:\eclipse-projects\Hello\report [junit] Running example.HelloTest [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.02 sec
pack: [mkdir] Created dir: F:\eclipse-projects\Hello\dist [jar] Building jar: F:\eclipse-projects\Hello\dist\hello.jar
doc: [mkdir] Created dir: F:\eclipse-projects\Hello\doc [javadoc] Generating Javadoc [javadoc] Javadoc execution [javadoc] Loading source files for package example... [javadoc] Constructing Javadoc information... [javadoc] Standard Doclet version 1.4.2_04 [javadoc] Building tree for all the packages and classes... [javadoc] Building index for all the packages and classes... [javadoc] Building index for all classes... [javadoc] Generating F:\eclipse-projects\Hello\doc\stylesheet.css... [javadoc] Note: Custom tags that could override future standard tags: @todo. To avoid potential overrides, use at least one period character (.) in custom tag names. [javadoc] Note: Custom tags that were not seen: @todo BUILD SUCCESSFUL Total time: 11 seconds
Ant依次執(zhí)行初始化,編譯,測(cè)試,打包,生成API文檔一系列任務(wù),極大地提高了開發(fā)效率。將來(lái)開發(fā)J2EE項(xiàng)目時(shí),還可加入部署等任務(wù)。并且,即使脫離了Eclipse環(huán)境,只要正確安裝了Ant,配置好環(huán)境變量ANT_HOME=Path=…;%ANT_HOME%\bin,在命令行提示符下切換到Hello目錄,簡(jiǎn)單地鍵入ant即可。 |