借助Ant工具,实现快速开发
?
當一個項目大了以后,每次重新編譯,打包,運行等就會變得復(fù)雜、費時、重復(fù),在c語言中有make腳本進行這些工作的批量完成,那么在java中,我們可以使用Ant。
?
Ant是一個構(gòu)建工具,可以完成這些任務(wù):編譯java源代碼、運行java程序、拷貝文件或目錄、將編譯完成的類打包等等。
而且,Ant允許我們按照自己的方式,添加自己的任務(wù)(Task),比如通過Xdoclet完成項目中的hibernate文件生成。
?
直接使用范例,那些基礎(chǔ)知識看來枯燥無味,我會把他們放到最后,如果例子看不明白,再去看基礎(chǔ)知識也不遲。
?
?
使用Ant進行:j2ee項目打包發(fā)布,hibernate相關(guān)文件生成
?
<?xml version="1.0" encoding="GBK"?> <project name="測試ANT" basedir="."> <!--資源文件位置--> <property name="src.dir" value="${basedir}/src" /> <!--編譯的目標位置--> <property name="classes" value="${basedir}/build/classes" /> <!--打包的目標位置--> <property name="build" value="${basedir}/build" /> <!--Lib的位置--> <property name="lib" value="${basedir}/WebContent/WEB-INF/lib" /> <!--tomcat.webapps位置--> <property name="webapps" value="D:/tomcate/apache-tomcat-7.0.39/webapps" /> <!--Xdoclet.Home--> <property name="xdoclet.home" value="D:/oa/xdoclet-plugins-1.0.3" /> <!-- Build classpath 指定定lib所在位置--> <path id="classpath"> <fileset dir="${lib}"> <include name="*.jar" /> </fileset> </path> <!-- 刪除build 路徑--> <target name="刪除build 路徑"> <delete dir="${build}" /> </target> <!-- 建立build/classes 路徑,并編譯class 文件到build/classes 路徑下--> <target name="建立build/classes 路徑,并編譯class 文件到build/classes 路徑下" depends="刪除build 路徑"> <mkdir dir="${classes}" /> <javac srcdir="${basedir}/src" destdir="${classes}"> <classpath refid="classpath" /> </javac> <copy todir="${classes}"> <fileset dir="${src.dir}"> <include name="**/*.xml" /> <include name="**/*.properties" /> </fileset> </copy> </target> <!-- 打war 包--> <target name="打war 包" depends="建立build/classes 路徑,并編譯class 文件到build/classes 路徑下"> <war destfile="${build}/WebTest1.war" webxml="${basedir}/WebContent/WEB-INF/web.xml"> <!-- 拷貝WebContent下所有文件--> <fileset dir="${basedir}/WebContent" /> <!-- 拷貝lib 目錄下的jar 包--> <lib dir="${lib}" /> <!-- 拷貝build/classes 下的class 文件--> <classes dir="${classes}" /> </war> <delete dir="${classes}" /> </target> <!--發(fā)布到本地tomcat--> <target name="發(fā)布到本地tomcat" depends="打war 包"> <copy file="${build}/WebTest1.war" tofile="${webapps}/WebTest1.war" /> </target> <!-- 下面是借助xdoclet,進行自定義任務(wù),完成hibernate相關(guān)文件生成 --><!-- xdoclet通過對源文件進行掃描,并讀取源文件中的注釋,然后生成相應(yīng)的配置文件,因此xdoclet有自己的一套注釋規(guī)則,詳細看文檔。--><!-- Build classpath --> <path id="xdoclet.task.classpath"> <fileset dir="${xdoclet.home}/lib"> <include name="**/*.jar" /> </fileset> <fileset dir="${xdoclet.home}/plugins"> <include name="**/*.jar" /> </fileset> </path> <taskdef name="xdoclet" classname="org.xdoclet.ant.XDocletTask" classpathref="xdoclet.task.classpath" /> <!--生成Hibernate配置文件--> <target name="生成Hibernate配置文件" depends="生成hibernate映射文件"> <xdoclet> <fileset dir="${src.dir}/com/tch/model"> <include name="**/*.java" /> </fileset> <component classname="org.xdoclet.plugin.hibernate.HibernateConfigPlugin" destdir="${src.dir}" version="3.0" hbm2ddlauto="update" jdbcurl="jdbc:mysql://127.0.0.1/oa" jdbcdriver="com.mysql.jdbc.Driver" jdbcusername="root" jdbcpassword="123456" dialect="org.hibernate.dialect.MySQLDialect" showsql="true" /> </xdoclet> </target> <!--生成hibernate映射文件--> <target name="生成hibernate映射文件"> <xdoclet> <fileset dir="${src.dir}/com/tch/model"> <include name="**/*.java" /> </fileset> <component classname="org.xdoclet.plugin.hibernate.HibernateMappingPlugin" version="3.0" destdir="${src.dir}" /> </xdoclet> </target> </project>?
?
?基礎(chǔ)
1、搭建環(huán)境
?
2、基本元素
bulid.xml
?
<?xml version="1.0" encoding="GBK"?> <project name="ant測試" default="getBaseDir" basedir="D:\antTest"> <!--基本元素--> <target name="getBaseDir"> <description> the first example! </description> <echo message="hello world!" /> </target> <target name="targetA" if ="ant.version"> <echo message ="Java Version: ${ant.version}"/> </target> <target name="targetC" depends ="targetA" unless ="anotherTarget" > <description> show depends,unless,basedir,if </description> <echo message ="The base dir is: ${basedir}"/> </target> <!--property--> <property name="name" value="jim" /> <property name="age" value="18" /> <target name="targetD" > <echo message="name:${name},age:${age}" /> </target> <!--echo--> <target name="targetEcho"> <echo message="Hello,ANT" file="herfile/hello.log" append="true" /> </target> </project>?
?
?project 元素是 Ant 構(gòu)件文件的根元素, Ant 構(gòu)件文件至少應(yīng)該包含一個project 元素,否則會發(fā)生錯誤。在每個 project 元素下,可包含多個 target 元素。
1) name 屬性
??? 用于指定 project 元素的名稱。
2) default 屬性
??? 用于指定 project 默認執(zhí)行時所執(zhí)行的target 的名稱。
3) basedir 屬性
??? 用于指定基路徑的位置。該屬性沒有指定時,使用 Ant 的構(gòu)件文件的附目錄作為基準目錄。
?
target?元素
?? target為Ant的基本執(zhí)行單元,它可以包含一個或多個具體的任務(wù)。多個target 可以存在相互依賴關(guān)系。它有如下屬性:
1) name 屬性
??? 指定 target 元素的名稱,這個屬性在一個project 元素中是唯一的。我們可以通過指定 target 元素的名稱來指定某個 target 。
2) depends 屬性
??? 用于描述 target 之間的依賴關(guān)系,若與多個target 存在依賴關(guān)系時,需要以“,”間隔。 Ant 會依照 depends 屬性中 target 出現(xiàn)的順序依次執(zhí)行每個 target 。被依賴的 target 會先執(zhí)行。
3) if 屬性
??? 用于驗證指定的屬性是否存在,若不存在,所在 target 將不會被執(zhí)行。
4) unless 屬性
??? 該屬性的功能與 if 屬性的功能正好相反,它也用于驗證指定的屬性是否存在,若不存在,所在 target 將會被執(zhí)行。
5) description 屬性
??? 該屬性是關(guān)于 target 功能的簡短描述和說明。
?
property?元素
property元素可看作參量或者參數(shù)的定義,project 的屬性可以通過 property元素來設(shè)定,也可在 Ant 之外設(shè)定。若要在外部引入某文件,例如 build.properties 文件,可以通過如下內(nèi)容將其引入:
<property?file=”build.properties”/>
property 元素可用作 task 的屬性值。在 task 中是通過將屬性名放在“ ${”和“ } ”之間,并放在 task 屬性值的位置來實現(xiàn)的。
?
3、常用命令
build.xml
?
<?xml version="1.0" encoding="GBK"?> <project name="ant測試" default="getBaseDir" basedir="D:\antTest"> <!--常用命令--> <!--copy--> <target name="targetCopy"> <copy file="myfile/hello.txt" tofile="herfile/copied.txt"/> <copy todir="hisfile"> <fileset dir="myfile"/></copy> </target> <!--delete--> <target name="targetDelete"> <delete file="myfile/hello.txt" /> <delete dir="myfile" /> <delete includeEmptyDirs="true"> <fileset dir="." includes="**/*.bak" /> </delete> </target> <!--mkdir--> <target name="targetMkdir"> <mkdir dir="/myfile/src/build/classes"/> </target> <!--move--> <target name="targetMove"> <move file="destfile.txt" tofile="hello.txt"/> <move file="hello.txt" todir="myfile"/> <move todir="herfile"> <fileset dir="myfile"/></move> </target> </project>?
?
copy命令用來對文件和目錄的復(fù)制功能。
delete 命令對文件或目錄進行刪除
mkdir 命令創(chuàng)建目錄
move 命令移動文件或目錄
?
?
與Ant相當?shù)墓ぞ哂?/strong>Maven,Maven預(yù)設(shè)了一些方便的功能,而ant只能自己寫,各有自己的優(yōu)勢。就像hibernate和IBatis,hibernate做了一定的封裝,方便了,卻失去了一定的靈活性。
?
java的世界太大,有許許多多的工具,為了快速進行開發(fā),我們一定要善假于物也,對眾多工具先有個了解,知道有這么回事,需要時才知道去找誰,再即用即學也不遲。
?
?
總結(jié)
以上是生活随笔為你收集整理的借助Ant工具,实现快速开发的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 难治性痛风和一般痛风有什么区别吗?
- 下一篇: POJ2391(最大流Isap+Floy