在Eclipse中使用Ant
Ant是Java平台下非常棒的批處理命令執行程序,能非常方便地自動完成編譯,測試,打包,部署等等一系列任務,大大提高開發效率。如果你現在還沒有開始使用Ant,那就要趕快開始學習使用,使自己的開發水平上一個新台階。
Eclipse中已經集成了Ant,我們可以直接在Eclipse中運行Ant。
以前面建立的Hello工程為例,創建以下目錄結構:

新建一個build.xml,放在工程根目錄下。build.xml定義了Ant要執行的批處理命令。雖然Ant也可以使用其它文件名,但是遵循標准能更使開發更規范,同時易於與別人交流。
通常,src存放Java源文件,classes存放編譯后的class文件,lib存放編譯和運行用到的所有jar文件,web存放JSP等web文件,dist存放打包后的jar文件,doc存放API文檔。
然后在根目錄下創建build.xml文件,輸入以下內容:
<?
xml version="1.0"
?>
<
project
name
="Hello world"
default
="doc"
>
<!--
properies
-->
<
property
name
="src.dir"
value
="src"
/>
<
property
name
="report.dir"
value
="report"
/>
<
property
name
="classes.dir"
value
="classes"
/>
<
property
name
="lib.dir"
value
="lib"
/>
<
property
name
="dist.dir"
value
="dist"
/>
<
property
name
="doc.dir"
value
="doc"
/>
<!--
定義classpath
-->
<
path
id
="master-classpath"
>
<
fileset
file
="${lib.dir}/*.jar"
/>
<
pathelement
path
="${classes.dir}"
/>
</
path
>
<!--
初始化任務
-->
<
target
name
="init"
>
</
target
>
<!--
編譯
-->
<
target
name
="compile"
depends
="init"
description
="compile the source files"
>
<
mkdir
dir
="${classes.dir}"
/>
<
javac
srcdir
="${src.dir}"
destdir
="${classes.dir}"
target
="1.4"
>
<
classpath
refid
="master-classpath"
/>
</
javac
>
</
target
>
<!--
測試
-->
<
target
name
="test"
depends
="compile"
description
="run junit test"
>
<
mkdir
dir
="${report.dir}"
/>
<
junit
printsummary
="on"
haltonfailure
="false"
failureproperty
="tests.failed"
showoutput
="true"
>
<
classpath
refid
="master-classpath"
/>
<
formatter
type
="plain"
/>
<
batchtest
todir
="${report.dir}"
>
<
fileset
dir
="${classes.dir}"
>
<
include
name
="**/*Test.*"
/>
</
fileset
>
</
batchtest
>
</
junit
>
<
fail
if
="tests.failed"
>
*********************************************************** **** One or more tests failed! Check the output ... **** ***********************************************************
</
fail
>
</
target
>
<!--
打包成jar
-->
<
target
name
="pack"
depends
="test"
description
="make .jar file"
>
<
mkdir
dir
="${dist.dir}"
/>
<
jar
destfile
="${dist.dir}/hello.jar"
basedir
="${classes.dir}"
>
<
exclude
name
="**/*Test.*"
/>
<
exclude
name
="**/Test*.*"
/>
</
jar
>
</
target
>
<!--
輸出api文檔
-->
<
target
name
="doc"
depends
="pack"
description
="create api doc"
>
<
mkdir
dir
="${doc.dir}"
/>
<
javadoc
destdir
="${doc.dir}"
author
="true"
version
="true"
use
="true"
windowtitle
="Test API"
>
<
packageset
dir
="${src.dir}"
defaultexcludes
="yes"
>
<
include
name
="example/**"
/>
</
packageset
>
<
doctitle
>
<![CDATA[
<h1>Hello, test</h1>
]]>
</
doctitle
>
<
bottom
>
<![CDATA[
<i>All Rights Reserved.</i>
]]>
</
bottom
>
<
tag
name
="todo"
scope
="all"
description
="To do:"
/>
</
javadoc
>
</
target
>
</
project
>
選中Hello工程,然后選擇“Project”,“Properties”,“Builders”,“New…”,選擇“Ant Build”:

填入Name:Ant_Builder;Buildfile:build.xml;Base Directory:${workspace_loc:/Hello}(按“Browse Workspace”選擇工程根目錄),由於用到了junit.jar包,搜索Eclipse目錄,找到junit.jar,把它復制到Hello/lib目錄下,並添加到Ant的Classpath中:

然后在Builder面板中鈎上Ant_Build,去掉Java Builder:

再次編譯,即可在控制台看到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依次執行初始化,編譯,測試,打包,生成API文檔一系列任務,極大地提高了開發效率。將來開發J2EE項目時,還可加入部署等任務。並且,即使脫離了Eclipse環境,只要正確安裝了Ant,配置好環境變量ANT_HOME=<Ant解壓目錄>,Path=…;%ANT_HOME%\bin,在命令行提示符下切換到Hello目錄,簡單地鍵入ant即可。