前言:這段時間在學習Ant,發現這是一個很強大的構建工具。你可能使用了很長一段時間,才發現Ant能做數不完的事。總之,個人覺得,Ant學習門檻低,入門簡單,能大概看懂程式,寫一些簡單的腳本即可,剩下在遇到的時候邊做邊學就可以了。另外,博主資歷尚淺,有說的不對的地方,還請大家不吝指教。 1、Ant是一 ...
前言:這段時間在學習Ant,發現這是一個很強大的構建工具。你可能使用了很長一段時間,才發現Ant能做數不完的事。總之,個人覺得,Ant學習門檻低,入門簡單,能大概看懂程式,寫一些簡單的腳本即可,剩下在遇到的時候邊做邊學就可以了。另外,博主資歷尚淺,有說的不對的地方,還請大家不吝指教。
1、Ant是一個Apache基金會下的跨平臺的構建工具,它可以實現項目的自動構建和部署等功能。它具有跨平臺、使用簡單、語法清晰、功能強大等特點 2、下載和安裝:http://ant.apache.org/,添加環境變數ANT_HOME,指向根目錄D:\hybirs630\hybris\bin\platform\apache-ant-1.9.1、安裝完成後,進入控制台界面,輸入 ant -version檢查是否安裝成功。 3、Ant的構建文件是通過XML編寫的,預設名稱是build.xml.
<?xml version="1.0"?> <project name="helloWorld"> <target name="sayHelloWorld"> <echo message="Hello,Amigo"/> </target> </project>備註:(在文件的存在目錄執行ant sayHelloWorld, ant 預設尋找文件build.xml,若文件不為build.xml,則執行ant –f hello.xml sayHelloWorld、ant –buildfile hello.xml sayHelloWorld或ant –file hello.xml sayHelloWorld) 4、Ant的關鍵元素project、target、property、task 1、project元素(至少包含一個,可以包含多個target) 、 name屬性、 default屬性(用於指定project預設執行時所執行的target的名稱)、basedir屬性(用於指定路徑的位置。沒有指定,使用Ant的構件文件的附目錄作為基準目錄)
<?xml version="1.0"?> <project name="projectStudy" default="sayBaseDir" basedir="D:\hybirs630\hybris\bin\platform\apache-ant-1.9.1"> <target name="sayBaseDir"> <echo message="The base dir is: ${basedir}"/> </target> </project>

2、ant -projecthelp(列出當前ant可執行的所有命令) 3、target元素(是Ant的基本執行單元,它可以包含一個或多個具體的任務,多個target可以存在相互依賴關係)、name屬性(指定名稱)、depends屬性(描述target之間的依賴關係,以,間隔,順序執行,被依賴的target優先執行)、if/unless屬性(驗證屬性是否存在,若存在,則執行/不執行)、description屬性(target功能的簡短描述與說明)
<?xml version="1.0"?> <project name="targetStudy"> <target name="targetA" if="ant.java.version"> <echo message="Java Version: ${ant.java.version}"/> </target> <target name="targetB" depends="targetA" unless="amigo"> <description> a depend example! </description> <echo message="The base dir is: ${basedir}"/> </target> </project>
4、property元素(參量或者參數的定義),若要在外部引入某文件,例如build.properties文件,可以通過如下內容將其引入:<property file=” build.properties”/> 同時,Ant還提供了一些它自己的內置屬性,如下: basedir:project基目錄的絕對路徑. ant.file:buildfile的絕對路徑,如上面的各例子中,ant.file的值為E:"build.xml; ant.version:Ant的版本,在本文中,值為1.7.0; ant.project.name:當前指定的project的名字,即前文說到的project的name屬性的值; ant.java.version:Ant檢測到的JDK的版本。 備註:ant中若引用property或者它的內置屬性,用${}括弧起來引用。
<?xml version="1.0"?> <project name="propertyStudy" default="example"> <property name="name" value="amigo"/> <!--讀取屬性文件的屬性配置--> <property file="foo.properties"/> <property name="age" value="25"/> <target name="example"> <echo message="name: ${name}, age: ${age}"/> </target> </project>
5、Ant的任務介紹 1、輸出信息
<echo message="xxx"/> <echo>yyyy</echo>2、copy任務 該任務主要用來對文件和目錄的複製功能。舉例如下: Eg1.複製單個文件:<copy file="file.txt" tofile="copy.txt"/> Eg2.對文件目錄進行複製: <copy todir="../newdir/dest_dir"> <fileset dir="src_dir"/> </copy> Eg3. 將文件複製到另外的目錄: <copy file="file.txt" todir="../other/dir"/> <copyfile src="test.java" dest="subdir/test.java"> dest:代表目標目錄 Eg4.拷貝一堆文件到一個目錄 <copy todir="../dest/dir"> <fileset dir="src_dir"> <exclude name="**/*.java"> </fileset> </copy> Eg5.拷貝一個目錄下的東西到另一個目錄下(includes加入,excludes排除,即排除這些之外的東西都要加入) <copy src="${src}/resources" dest="${dist}" includes="**/*.java" excludes="**/Test.java"/> 2、delete任務 對文件或目錄進行刪除,舉例如下: Eg1. 刪除某個文件:<delete file="photo/amigo.jpg"/> Eg2. 刪除某個目錄:<delete dir="photo"/> Eg3. 刪除所有的備份目錄或空目錄:(也可以使用include和exclude) <delete includeEmptyDirs="true"> <fileset dir="." includes="**/*.bak"/> </delete> 3. mkdir任務 創建目錄。eg:<mkdir dir="build"/> 4、move任務 移動文件或目錄,舉例如下: Eg1. 移動單個文件:<move file="fromfile" tofile=”tofile”/> Eg2. 移動單個文件到另一個目錄:<move file="fromfile" todir=”movedir”/> Eg3. 移動某個目錄到另一個目錄: <move todir="newdir"> <fileset dir="olddir"/> </move> 5、重命名任務 <rename src="foo.jar" dest="ant-${version}.jar"/> 6、Touch的使用 如果文件不存在,創建文件,如果存在,更改最後訪問時間為系統時間 <touch file="myfile"/> 7、打Jar包 <target name="build" depends="compile"> <jar destfile="${hello_jar}" basedir="${dest}"/> </target> 8、壓縮zip文件 <zip destfmlile="${dist}/manual.zip" basedir="htdocs/manual" includes="api/**/*.html" excludes="**/todo.html"/> 6、利用Ant構建和部署Java工程 參考我的另一篇博客:http://www.cnblogs.com/jmcui/p/6946777.html
7、一份Ant的API文檔:http://files.cnblogs.com/files/jmcui/ant%E4%BD%BF%E7%94%A8%E5%B8%AE%E5%8A%A9%E6%89%8B%E5%86%8C_%E4%B8%AD%E6%96%87.pdf