http://jerry17768java.blogspot.com/search/label/Ant%E4%BD%BF%E7%94%A8
目前Ant工具已經內崁為Eclipse IDE的一部份,因此我們不需要額外再安裝Ant plugin套件,
其使用方法極為簡單,完整的操作流程可參考下列說明:
1. 開啟您的Eclipse IDE,建立一個TestProject的專案之後,於src/jcode/test套件目錄底下
新增HelloTest類別且於程式進入點的main()函式,加入以下的一段程式代碼:
public static void main(String[] args) { System.out.println("Hello, everybody!"); } |
2. 把滑鼠指標移至TestProject的圖示,然後點擊滑鼠右鍵,出現上下文選單(context menu)點取Export項目,
畫面帶出如下圖的"Export"對話方塊,選擇"Ant Buildfiles"圖示,來產出TestProject的build.xml檔案檔:
3. 打開build.xml文檔,並且對該檔案進行局部性的修改,其修改過的程式代碼以
紅色粗體字表示。
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <project basedir= "." default= "build" name= "TestProject" > <property environment= "env" /> <property name= "ECLIPSE_HOME" value= "../../../eclipse-sdk-helios" /> <property name= "debuglevel" value= "source,lines,vars" /> <property name= "target" value= "1.6" /> <property name= "source" value= "1.6" />
<property name="dist" location="dist"/> <path id= "TestProject.classpath" > <pathelement location= "bin" /> <pathelement location= "../../../jar-lib/ojdbc14.jar" /> </path> <target name= "init" > <mkdir dir= "bin" /> <copy includeemptydirs= "false" todir= "bin" > <fileset dir= "src" > <exclude name= "**/*.launch" /> <exclude name= "**/*.java" /> </fileset> </copy> </target> <target name= "clean" > <delete dir= "bin" /> </target> <target depends= "clean" name= "cleanall" /> <!--target depends="build-subprojects,build-project" name="build"/-->
<target depends="dist" name="build"/> <target name= "build-subprojects" /> <target depends= "init" name= "build-project" > <echo message= "${ant.project.name}: ${ant.file}" /> <javac debug= "true" debuglevel= "${debuglevel}" destdir= "bin" source= "${source}" target= "${target}" > <src path= "src" /> <classpath refid= "TestProject.classpath" /> </javac> </target>
<target name="dist" depends="build-subprojects,build-project"description="generate the distribution" >
<!-- 創建放置jar檔目錄 -->
<mkdir dir="${dist}/lib"/>
<!-- 把build目錄內的所有檔案結構打包成一個TestProject.jar檔 -->
<jar jarfile="${dist}/lib/${ant.project.name}.jar"basedir="bin"/>
</target> <target description= "Build all projects which reference this project. Useful to propagate changes." name= "build-refprojects" /> <target description= "copy Eclipse compiler jars to ant lib directory" name= "init-eclipse-compiler" > <copy todir= "${ant.library.dir}" > <fileset dir= "${ECLIPSE_HOME}/plugins" includes= "org.eclipse.jdt.core_*.jar" /> </copy> <unzip dest= "${ant.library.dir}" > <patternset includes= "jdtCompilerAdapter.jar" /> <fileset dir= "${ECLIPSE_HOME}/plugins" includes= "org.eclipse.jdt.core_*.jar" /> </unzip> </target> <target description= "compile project with Eclipse compiler" name= "build-eclipse-compiler" > <property name= "build.compiler" value= "org.eclipse.jdt.core.JDTCompilerAdapter" /> <antcall target= "build" /> </target> </project>
4. 將滑鼠指標移動到build.xml圖示,單點滑鼠右鍵後選取"Run As -> 1 Ant Build"項目執行,會於"Console"輸出Ant的腳本代碼:
Buildfile: D:\works\TestProject\build.xml build-subprojects : init : build-project : [ echo ] TestProject: D:\works\TestProject\build.xml dist : [ mkdir ] Created dir: D:\works\TestProject\dist\lib [ jar ] Building jar: D:\works\TestProject\dist\lib\TestProject.jar build : BUILD SUCCESSFUL Total time: 526 milliseconds 張貼者: Jerry Chan 於 下午4:07 0 意見 以電子郵件傳送這篇文章 BlogThis。 分享至 Twitter 分享至 Facebook 標籤: Ant使用
Ant的手動安裝設定
Ant本身並非是一個Windows的程式,因此安裝過程需要一些手動安裝步驟。
1. 首先,到apache ant的官方網站 http://ant.apache.org/bindownload.cgi下載ant的程式套件壓縮檔
2. 再來,將Ant程式套件的zip檔解壓縮到Windows的指定目錄 e.g., C:\myant\
zip檔內會包含依據版本資訊命名"apache-ant"的子目錄結構。
3. 然後,你需要設定一些環境變數以讓你的command console知道如何去找到Ant的執行檔
以及讓Ant知道在那裡可以找到java執行檔
在Windows XP的對話框中,你可以存取環境變數藉由"開始選單"與右擊"我的電腦"圖示。
稍後會帶出"系統屬性"對話方塊。請選取"進階"按鈕:
並且點擊"環境變數"按鈕。
4. 最後,你需要使用"新增"按鈕加入兩個新的變數分別如下:
ANT_HOME是ant套件解壓縮後所放置的根目錄所在位置 JAVA_HOME表示Java SDK安裝所在根目錄
而且你還需要編輯變數:
於PATH變數增加%ANT_HOME%/bin的路徑
5. 完成以上步驟之後,打開你的command console輸入ant按Enter鍵,若輸出以下的訊息字串時,代表Ant的安裝設定成功。