持續集成 Java手冊

持續集成 Java手冊

一、概念

Martin Fowler的文章:Continuous Integration  中文翻譯:持續集成

二、工具

傳統工具:VisualStudio.Net,VisualSourceSafe,Rational ClearCase

自動編譯工具:Ant

迴歸測試工具:JUnit

代碼檢查工具:CheckStyle

持續集成工具:CruiseControl

三、步驟

  • CruiseControl監控遠程版本控制系統的變化

  • 變化發生時CruiseControl調用編譯工具進行編譯(Ant等)

  • 編譯成功後調用JUnit進行迴歸測試

  • 編譯成功後調用CheckStyle進行代碼檢查

  • 完畢後將編譯結果、測試結果、代碼檢查結果發送至開發人員、主管經理,併發布至網站,甚至報警器

    所有這一切都是按照編制好的腳本自動進行的

四、實施示例

目前我們使用的是ClearCase,主控軟件爲CruiseControl,其腳本文件爲cccc.xml

  • 配置遠程版本控制系統

<modificationset quietperiod="30">
  <clearcase branch="main" viewpath="D:/cc_view/chelseafc/Nucleus2.0/Port" recursive="true" />
  </modificationset>
  • 配置編譯工具

<schedule interval="30">
  <ant antscript="C:/Java/JBuilder2005/thirdparty/ant/bin/ant.bat" buildfile="D:/cc_view/chelseafc/Nucleus2.0/Port/clearcase-build.xml" target="cleanbuild" multiple="1" />
  </schedule>
  • 配置測試用例(在ant的配置文件中)

<target name="test" depends="init" description="Run unit tests">
  <delete dir="${junit.results}" />
  <mkdir dir="${junit.results}" />
- <junit fork="yes" haltonfailure="yes">
- <classpath>
  <pathelement location="${build.dir}" />
  </classpath>
  <formatter type="plain" usefile="false" />
  <formatter type="xml" />
- <batchtest todir="${junit.results}">
  <fileset dir="${build.dir}" includes="**/*Test.class" />
  </batchtest>
  </junit>
  </target>
  • 配置報告形式
<publishers>
  <currentbuildstatuspublisher file="currentbuild.txt" />
- <htmlemail mailhost="mail.chelseafc.com.cn" returnaddress="[email protected]" subjectprefix="ContinuousIntegration:" buildresultsurl="http://chelsea:8044/cruisecontrol/buildresults" spamwhilebroken="true" xsldir="F:/software/Agile.Net/cruisecontrol-2.2/reporting/jsp/xsl" css="F:/software/Agile.Net/cruisecontrol-2.2/reporting/jsp/css/cruisecontrol.css" logdir="D:/Tomcat 4.1/webapps/cruisecontrol/samplelogs">
  <always address="[email protected]" />
  <always address="[email protected]" />
  <map alias="chelsea" address="[email protected]" />
  </htmlemail>
  </publishers>
  • 其中CruiseControl暫時沒有提供代碼檢查工具的支持,建議使用Ant來調用CheckStyle,示例如下(沒有真正運行過):
<target name="web.checkstyle">
  <mkdir dir="${target.temp}/checkstyle" />
  <mkdir dir="${target.web}/checkstyle" />
- <taskdef resource="checkstyletask.properties">
- <classpath>
  <fileset dir="${support.tools}/checkstyle31" includes="**/*.jar" />
  </classpath>
  </taskdef>
- <copy file="${support.tools}/checkstyle31/custom.xml" overwrite="true" tofile="${target.temp}/checkstyle/custom.xml">
- <filterset>
  <filter token="source.java" value="${basedir}/${source.java}" />
  <filter token="target.checkstyle" value="${basedir}/${target.temp}/checkstyle" />
  </filterset>
  </copy>
- <checkstyle config="${target.temp}/checkstyle/custom.xml" failOnViolation="false">
  <fileset dir="${source.java}/main" includes="**/*.java" />
  <formatter type="plain" />
  <formatter type="xml" toFile="${target.temp}/checkstyle/checkstyle_errors.xml" />
  </checkstyle>
  <style basedir="${target.temp}/checkstyle" destdir="${target.web}/checkstyle" includes="checkstyle_errors.xml" style="${support.tools}/checkstyle31/checkstyle-noframes.xsl" />
  </target>

五、幾點提示

  • CruiseControl會自動根據本地ClearCase的View監控遠程VOB
  • 其實除了監控遠程版本控制系統外其它的任務都可以由Ant來完成,CC只負責監控變化並調用Ant即可
  • CruiseControl2.2的<htmlemail>好像無法在jdk5.0下運行,如果需要集成jdk5.0的項目,則需要爲Ant的<javac>指定source和target,而CruiseControl2.2依然用jdk1.4啓動
  • 可以爲cruisecontrol.bat加入啓動參數“-port 8055”,這樣可以用JMX(http://localhost:8055)來控制cc
  • 最好避免中文路徑,否則就需要手工爲幾個Xml格式的文件,如cc的report Servlet的Web.xml等加入編碼方式“<?xml version="1.0" encoding="UTF-8" ?>,或者將中文路徑映射爲虛擬硬盤:“subst Y: "D:/cc_view/chelsea/Platform/開發/Nucleus2.0/Source"”
  • 中文log無法正常顯示時,需要設置CruiseControl配置文件中<log>元素的“encoding”屬性,如:
    <log dir="D:/Tomcat 4.1/webapps/cruisecontrol/samplelogs" encoding="utf-8">
      <merge dir="D:/cc_view/chelseafc/Nucleus2.0/Port/test-results" />
    </log>
  • 編譯失敗後,在下次checkin之前,一般不需要重新編譯,這時可設置<project >的“buildafterfailed”屬性爲false來避免重新編譯
  • <htmlemail>的幾個屬性好像沒有缺省設置,雖然文檔裏說從2.1.7開始有缺省設置,包括xsldir,css,logdir
  • 各種工具的安裝、使用,在各自的文檔裏都非常詳細,網上亦有無數資源

六、參考資料

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章