經(jīng)過(guò)摸索,監控工具的開(kāi)發(fā)過(guò)程實(shí)現了利用Ant來(lái)自動(dòng)從CVS服務(wù)器checkout源碼,然后編譯,進(jìn)行單元測試并生成測試報告,部署或者發(fā)布,最后郵件通知的流程現將Ant的實(shí)踐總結跟貼于此
Ant的核心就在于它的Task,分為Core Task和Optional Task,通過(guò)這些Task,可以完成很多任務(wù)而不僅僅是建構另外,還可以自己編寫(xiě)擴展Task類(lèi)實(shí)現特定的功能
使用Ant的Task時(shí),需要注意的是很多Task需要相關(guān)的Jar包在環(huán)境變量classpath中,或者放在A(yíng)NT_HOMEib下,這樣可以省去設定classpath的麻煩比如發(fā)送MIME形式的Email的mail Task需要有mail.jar(JavaMail)和activation.jar(JavaMail需要的JAF)這些在ant的文檔中都有說(shuō)明
執行具體任務(wù)的例子:
Ant從CVS服務(wù)器checkout源碼通過(guò)cvs任務(wù)實(shí)現:
<target name="checkout">
<mkdir dir="${localdir}"/>
<cvs cvsRoot="${cvsroot}" package="${module}" dest="${localdir}">
<commandline>
<argumentline="checkout"/>
</commandline>
</cvs>
</target>
編譯是通過(guò)javac任務(wù)實(shí)現:
<target name="compile" depends="checkout">
<javac srcdir="${localdir}\${module}\src" destdir="${localdir}\${module}\bin" includes="**\*.java">
<classpath refid="classpath"/>
</javac>
</target>
編譯好了之后還可以另行打包或者創(chuàng )建副本,可以通過(guò)copy和zip等任務(wù)
單元測試是利用junit,生成測試報告用junitreport:
<target name="junit" depends="dist">
<junit printsummary="on">
<classpath>
<pathelement location="${localdir}\${module}\bin"/>
</classpath>
<formatter type="xml"/>
<batchtest>
<fileset dir="${localdir}\${module}\bin">
<include name="**\*Test.class"/>
</fileset>
</batchtest>
</junit>
<junitreport>
<fileset dir=".">
<include name="TEST-*.xml"/>
</fileset>
<report format="noframes"/>
</junitreport>
</target>
單元測試都無(wú)誤后,就可以通過(guò)上傳至ftp:
<target name="publish" depends="junit">
<property name="ftpserver" value="xxx"/>
<property name="ftpdir" value="/Monitor"/>
<ftp server="${ftpserver}" userid="anonymous" password="@" remotedir="${ftpdir}">
<fileset dir="${distdir}">
<include name="${dist.zip}"/>
</fileset>
</ftp>
</target>
這里要說(shuō)明的是,<ftp>任務(wù)中有個(gè)屬性action,用于指明操作命令(包括"put", "get", "del", "list", "chmod", "mkdir","rmdir")當然,還需要有相應的權限
之后就可以郵件通知了可以將單元測試報告一并發(fā)送:
<target name="mail" depends="publish">
<tstamp>
<format property="TODAY" pattern="yyyy-MM-dd"/>
<format property="TSTAMP" pattern="hh:mm:ss"/>
</tstamp>
<property name="mailto" value="xxx@gmail.com, xxx@gmail.com "/>
<property name="mailfrom" value="xxx@gmail.com"/>
<mail from="${mailfrom}" tolist="${mailto}" mailhost="mail.gmail.com"
subject="xxxx"
messagefile="junit-noframes.html" messagemimetype="text/html">
<fileset dir=".">
<include name="TEST-*.xml"/>
</fileset>
</mail>
</target>
上面,<tstamp/>用來(lái)得到時(shí)間戳,并利用<format/>來(lái)對日期和時(shí)間格式化,ant的這個(gè)功能很實(shí)用
Ant的Task還有很多,有待進(jìn)一步挖掘不過(guò)對于自動(dòng)的編譯測試等流程,上面這些任務(wù)應該就夠了
需要兩個(gè)額外的支持文件加入到Ant的Lib中:mail.jar(http://java.sun.com/products/javamail/)
和activation.jar(http://java.sun.com/products/javabeans/glasgow/jaf.html)
-----------------------------參考代碼ant1.6以上(發(fā)郵件)--------------------------------------
<project name= "Simple Buildfile " default= "init " basedir= ". ">
<target name= "init ">
<mail mailhost= "smtp.mailcenter.com.cn " mailport= "25 " subject= "Test sendmail " messagemimetype= "text/html " user= "xxxx " password= "xxxx " >
<from address= "xx@xxx.com "/>
<to address= "abc@abc.com "/>
<message> I am trying to test send mail by ant </message>
</mail>
</target>
</project>
聯(lián)系客服