在《Ant與批處理(win環境)學習筆記》中學習了Ant的一些基礎知識,這期繼續深入學習
——————————Ant常用task————————————————————————
1、使用classPath
<target> <javac> <classpath refid="project.class.path"/> </javac> </target>
2、設定classpath
<classpath id="project.class.path"> <pathelement path="${classpath}"/> <fileset dir="lib"> <include name="**/*.jar"/> </fileset> <pathelement location="classes"/> <dirset dir="build"> <include name="apps/**/classes"/> <exclude name="apps/**/*Test*"/> </dirset> <filelist refid="xxx"/> </classpath>
3、 輸出資訊
輸出文本資訊使用echo; 輸出xml使用echoxml; 引入檔案使用import
4、 拷貝檔案
<copy file="myfile.txt" tofile="yourfile.txt"/>
copy的屬性有:
file、 dir、fileset等;
copydir:拷貝目錄
5、 刪除檔案
<delete file="xxx"/>
delete屬性有file、dir
deletetree:刪除檔案分類樹
6、 剪下檔案
<move todir="xxx">
//被剪下的東西
</move>
7、 重新命名
<rename src="xxx" dest="yyyy"/>
8、建立臨時檔案
<tempfile property="xxx" destDir="yyy" suff=".xml"/>
9、 touch的使用
<touch file="myfile" datetime="xxx"/>
10、 Condition的使用[color=blue][/color]
有<and>、<or>、<not>等tag,樣本如下:
<condition property="isLinuxButNotRedHat"> <and> <os family="Linux"/> <not> <os family="RedHat"/> </not> </and> </condition>
11、 替換replace
replace、replacefilter
12、 調用chmod
樣本:
<chmod perm="go-rwx" type="file"> <fileset dir="/web"> <include name="xxx"/> <exclude name="yyy"/> </fileset> </chmod>
13、 設定Property
有如下一些情況:
設定屬性name-value;讀取屬性檔案中的配置:<property file="xxx.properties"/>
讀取網路中的property-set:
<property url="xxx..."/>;讀取環境變數:<property environmen="xxx"/>
13、 建立目錄
<mkdir dir="xxx"/>
14、 打jar包
<jar destfile="${dist}" basedir="xxx"/>
15、 打ear包
<ear destfile="build/myapp.ear" appxml="src/metadata/application.xm">
<fileset> dir="build" includes="*.jar, *.war"/>
</ear>
16、 執行程式
<target name="help">
<exec exectuable="cmd">
<arg value="/c"/>
<arg value="ant.bat"/>
<arg value="-p"/>
</exec>
</target>
17、 運行jar包
<java classname="test.Main"> <arg value="-h"/> <classpath> <pathelement location="dist/test.jar"/> <pathelement path="/xxxx/yyy.jar"/> </classpath> </java>
疑問:
location與path的區別?
path可以用於指向存在多個檔案的位置,而location只能指向單個的檔案或目錄。另外path可以被設定id,供其它的path或classpath引用。如:<path id="main-classpath">,而location則沒有。
18、 製作Javadoc
... ...
19、 定義一個新的task類庫
<taskdef name="myjavadoc" classname="xxxxx"/>
20、 運行sql
<sql> driver="xxxx" url="yyy" userid="sa" password="123456"> insert into table test_table values(1,2,3); truncate table some_other_table; </sql>
21、 Filter的使用
<filter token="xxx" value="yyy"/>
22、還有一些常用的task如發送郵件、解壓縮
例子略去... ...
23、 antcall
調用target,DoSomethingelse:
<target name=default"> <antcall target="doSomethingelse"> <param name="param1" value="value"/> </antcall> <target name="doSomethingelse"> <echo message="Hello World"/> </target> </target>
在target中還可以使用if...else類的控制流程程
————————Ant中使用CVS的樣本————————————
<?xml version="1.0" encoding="UTF-8"?> <project> <project name="cvsroot" value=":pserver:yaoxxxx"/> <project name="basedir" value="xxx"/> <project name="cvs.password" value=":yyyy"/> <project name="cvs.passfile" value="zzzz"/> <target name="initpass"> <cvspass cvsroot="${cvsroot}" password="${cvs.password}" passfile="${cvs.passfile}"/> </target> <target name="checkout" depends="initpass"> <cvs cvsroot="${cvsroot}" command="checkout" cvsrsh="ssh" package="myproject" dese="${distdir}" passfile="${cvs.passfile}"/> </target> </project></xml>
Ant的學習就此告一段落,平時多看看開源項目的build.xml,翻翻ant docs。這是一個積累的過程... ... 我積累呀我積累呀!!!
——————————家庭作業—
1、 使用Eclipse整合Ant
2、 使用Ant構建tomcat的源碼
3、 使用Ant結合Junit進行自動化測試