最初注意到Ant是在ArcStyler這個著名的MDA工具之中,既然是MDA工具,當然要具備很多“自動”的功能。自動的從流程圖到代碼的生成,自動的代碼編譯和配置功能,自動的臨時目錄清理功能。而這些自動的功能中,很多來自于一個小小的“爬蟲”一樣的圖標。這就是我后來意識到鼎鼎大名的Ant了。
現在當然知道了,從最著名商務軟件開發環境JBuilder到最著名的開源IDE集成平臺Eclipse上,都爬上了這只小螞蟻的身影。而Apache組織里面幾乎所有的開源項目,都是用Ant進行編譯配置的。這很容易理解,Ant本身就是Apache的頂級開源項目之一。也可以說是使用得最為廣泛的項目之一。
Ant的意思是“Another Neat Tool”,就是“另一個整潔的工具”。那么,第一個整潔的工具是什么呢?我想應該是make。在IDE大行其道下成長起來的程序員,也許對Ant和make都不太熟悉,但是再前一點的人們,相信都有過在機房摸著腦袋寫makefile的經歷了。也許你們看到這句“摸著腦袋”的話,嘴角已經露出了會心的微笑了。用make編譯項目最繁瑣的莫過于在不同的平臺下面要寫不同的makefile,即使可以寫在一個makefile中,也是把真正的內容淹沒在一大堆的編譯開關里面。那時候,沒有想到一個真正可以跨平臺的編譯配置工具這么快就可以出現了,那就是2000年出生的Ant。
本科的時候,最佩服一個師兄。他幾乎從來不用什么開發環境和可視化的工具,搞什么事情都是make install,make clean,make –f makefile.mak,當然還有telnet,ftp等等,命令行記得純熟無比,不管是在windows還是linux下面。寫程序的時候當然更是如此,一堆一堆的目錄,源程序,通通一個都是一個make來管理。用VC的時候是nmake,用gcc的時候就是make?,F在想起來,他就是把一個make用得滾瓜爛熟了,不想再用其他東西而已。
很早就明白了,離開IDE就不會編譯程序的程序員,永遠不可能成為真正的程序員。但是一直以來,不想好好學習make或者ant,每次事到臨頭都是匆匆修改別人的makefile或者build.xml,抱抱佛腳,里面的內容也是似是而非,似懂非懂。但是,我覺得Ant還是最終要學的一門手藝,特別是對于一個java程序員。
今天終于下定決心,要認真看看Ant,事情的起因是因為hibernate的學習過程中,看到一個例子程序使用了Ant,不甚了了,所以下了決心。運氣特別好的是,Ant有一個非常好的入門教程http://www.javafan.net/softview.jsp?ID=206。用了一個上午加上一個下午的時間,基本上已經掌握了Ant最基本的內容,對于大多數人來說,已經足夠了。當然,要得益于自己對xml、make、Eclipse等等相關內容都比較熟悉。
詳細寫學習Ant的安裝、配置和學習過程沒什么意思。把自己的一個小練習列在下面,就是一個上午和一個下午的學習結果:
Java項目HelloAnt,根目錄名稱就是HelloAnt,根目錄包含build.xml和sub.xml。
項目包含一個src子目錄,src中包含一個源文件HelloAnt.java。
文件內容如下:
HelloAnt:
public class HelloAnt {
public static void main(String[] args) {
System.out.println("Hello Ant!");
}
}
build.xml
<?xml version="1.0"?>
<project default="jar" name="Project HelloAnt">
<description>
</description>
<property name="base.dir" value="."/>
<property name="src.dir" value="src"/>
<property name="build.dir" value="classes"/>
<property name="dist.dir" value="dist"/>
<property name="user.name" value="Wxb_nudt"/>
<path id="myclasspath">
<pathelement location="${build.dir}"/>
</path>
<target name="init" description="初始化編譯工作">
<mkdir dir="${build.dir}"/>
<mkdir dir="${dist.dir}"/>
<tstamp/>
</target>
<target name="build" depends="init" description="編譯源文件">
<javac srcdir="${src.dir}" destdir="${build.dir}"/>
<copy todir="${build.dir}">
<fileset dir="${src.dir}">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="jar" depends="build" description="打包class文件和源代碼">
<jar destfile="${dist.dir}/HelloAnt-${DSTAMP}.jar" basedir="${build.dir}">
<manifest>
<attribute name="Built-By" value="${user.name}"/>
</manifest>
</jar>
<jar destfile="${dist.dir}/HelloAnt-src-${DSTAMP}.jar" basedir="${src.dir}"/>
</target>
<target name="run" depends="build" description="運行HelloAnt的main函數">
<java classpathref="myclasspath" classname="HelloAnt" fork="true"/>
</target>
<target name="clean" description="清除編譯過程中創建的目錄">
<delete dir="${build.dir}"/>
<delete dir="${dist.dir}"/>
</target>
<target name="copy" description="將源文件夾中的java文件拷貝到build文件夾">
<copy todir="${build.dir}">
<fileset dir="${src.dir}">
<include name="*.java"/>
</fileset>
</copy>
<echo message="copy file finished!"/>
</target>
<target name="CallSub" description="調用另一個sub.xml文件中的目標">
<ant antfile="sub.xml" target="showMessage" inheritAll="false">
<property name="message" value="Hello from parent build"/>
</ant>
</target>
</project>
sub.xml:
<?xml version="1.0"?>
<project default="showMessage">
<target name="showMessage">
<echo message="Message=${message}"/>
</target>
</project>
運行結果如下:
輸入ant
Buildfile: D:\eclipse301\workspace\HelloAnt\build.xml
init:
[mkdir] Created dir: D:\eclipse301\workspace\HelloAnt\classes
[mkdir] Created dir: D:\eclipse301\workspace\HelloAnt\dist
build:
[javac] Compiling 1 source file to D:\eclipse301\workspace\HelloAnt\classes
jar:
[jar] Building jar: D:\eclipse301\workspace\HelloAnt\dist\HelloAnt-20050518.jar
[jar] Building jar: D:\eclipse301\workspace\HelloAnt\dist\HelloAnt-src-20050518.jar
BUILD SUCCESSFUL
Total time: 3 seconds
輸入ant run
Buildfile: D:\eclipse301\workspace\HelloAnt\build.xml
init:
build:
run:
[java] Hello Ant!
BUILD SUCCESSFUL
Total time: 2 seconds
輸入ant copy
Buildfile: D:\eclipse301\workspace\HelloAnt\build.xml
copy:
[copy] Copying 1 file to D:\eclipse301\workspace\HelloAnt\classes
[echo] copy file finished!
BUILD SUCCESSFUL
Total time: 1 second
輸入ant CallSub
Buildfile: D:\eclipse301\workspace\HelloAnt\build.xml
CallSub:
showMessage:
[echo] Message=Hello from parent build
BUILD SUCCESSFUL
Total time: 922 milliseconds
輸入ant clean
Buildfile: D:\eclipse301\workspace\HelloAnt\build.xml
clean:
[delete] Deleting directory D:\eclipse301\workspace\HelloAnt\classes
[delete] Deleting directory D:\eclipse301\workspace\HelloAnt\dist
BUILD SUCCESSFUL
Total time: 985 milliseconds
其中Ant版本1.5,JDK版本J2sdk1.4.2,Eclipse3.0.1(這個無關緊要,我在Eclipse中和在控制臺運行沒有絲毫區別)。
今日心情甚好,結繩以記之!