天天看點

使用Cruisecontrol實作Flex項目的持續內建

使用Cruisecontrol實作Flex項目的持續內建

持續內建是靈活開發中的最佳實踐之一,它強調要頻繁的內建,每次源代碼更改之後要通過所有的測試,如果一次建構不成功,開發者可以很快得到回報,及時修改,降低項目的風險。關于詳細的介紹,可以參考Martin Fowler的Continuous Integration.

我們的項目使用的持續內建工具是目前流行的Cruisecontrol ,使用的版本控制系統是Perforce,現在網上關于svn, cvs等工具的資料較多,關于Perforce的比較少,我把自己在搭建Flex項目的持續內建環境中遇到的問題以及解決方法整理一下,希望對缺少這方面資料的人有些幫助。

第1

:下載下傳Cruisecontrol。http://cruisecontrol.sourceforge.net,目前最新的版本是2.7.3,建議下載下傳src版本,因為在生成報告建構結果的應用程式時需要手動來build。

2

:寫Cruisecontrol的配置檔案config.xml,之前需要建立合适的目錄結構,可以參考網上的資料。

<!--根節點-->

<cruisecontrol>

<!--定義項目的資訊,一個<cruisecontrol>下可以有多個<project>節點-->

 <project name="AIRClient" buildafterfailed="true">

<!--定義一些可以在下面使用的屬性鍵值對,由于cruisecontrol 間隔一定時間就檢查Perforce上的代碼是否有更改,是以需要指定Perforce伺服器以及用戶端的資訊:

P4_PORT的值為Perforce伺服器的名字:端口;

P4_CLIENT的值為本機p4 client的名字,如果查詢不到,可以在Perforce的官網上下載下傳p4 command-line client的一個工具,運作p4 client指令,會打開一個txt檔案,裡面會顯示用戶端的所有資訊,其中有一項是”ROOT”,它指向的值是從Perforce上取到的源代碼自動存放的地方,可以根據自己的需要修改它,将下載下傳的源代碼存放在自己指定的合适的地方。一個使用者可以有多個client,可以用該工具建立多個client.

P4_USER是本機p4 client的使用者名

P4_PASSWD是本機p4 client的密碼-->

       <property name="P4_PORT" value="peuck:1710" />

       <property name="P4_CLIENT" value="Jissie-xp" />

       <property name="P4_USER" value="guang" />

       <property name="P4_PASSWD" value="pass1234" />

<listeners>

<!--cruisecontrol會把建構的狀态資訊記錄在下面的檔案中-->

      <currentbuildstatuslistener

          file="logs/AIRClient/status.txt"/>

    </listeners>

    <!-- Bootstrappers are run every time the build runs,

        *before* the modification checks -->

    <bootstrappers>

    </bootstrappers>

    <!-- Defines where cruise looks for changes, to decide

         whether to run the build -->

<!-- quietperiod指定在最後一次代碼check in之後的10秒後建構。防止代碼沒有完全check in就可以建構。View指定的是項目在Perforce伺服器上的路徑,使用…能夠把目錄下的所有檔案取出-->

    <modificationset quietperiod="10">

      <p4 port="${P4_PORT}"

                                      client="${P4_CLIENT}"

                                      user="${P4_USER}"

                                      passwd="${P4_PASSWD}"

                                      view="//HL/AIRClient/..."/>

    </modificationset>

    <!-- Configures the actual build loop, how often and which

         build file/target -->

<!— interval指定cruisecontrol每隔多少時間檢查Perforce代碼是否有更新,本例中為60秒;ant指令調用ant的build檔案,如果代碼有更新,則開始運作ant的build檔案,anthome指定ant.bat所在的路徑,buildfile指定ant.bat要運作的build檔案的名稱,target指定運作的是哪個ant target,multiple=1指定每次都要運作這個target.-->

    <schedule interval="60">

        <ant anthome="C:/cc-sandbox/cruisecontrol-bin-2.7.3/apache-ant-1.7.0"

           buildfile="p4-build-wrapper.xml"

           target="experimentBuild"

           uselogger="true"

           usedebug="false"

           multiple="1"/>

<!—multiple=5指定每建構5次運作一次target="cleanbuild"-->                     

        <ant anthome="C:/cc-sandbox/cruisecontrol-bin-2.7.3/apache-ant-1.7.0"

           buildfile="p4-build-wrapper.xml"

           target="cleanbuild"

           uselogger="true"

           usedebug="false"

           multiple="5"/>

    </schedule>

    <!-- directory to write build logs to -->

    <log logdir="logs/AIRClient"/>

<!-- Publishers are run *after* a build completes -->

<!—publishers指定如何把建構的結果表示出來,本例選擇将顯示建構結果的網頁寫在一個郵件中發送到指定的郵箱:mailhost是要發送郵件的SMTP伺服器的名字,username是該伺服器能識别的使用者,一般用自己的收郵件用戶端的帳号,returnaddress 是該郵件顯示的發送人的郵箱,buildresultsurl 是顯示建構結果的網頁的路徑。-->

    <publishers>

         <email mailhost="apacmail. com"

                                                username="guang"

                                                password="gl_0304"

                                                returnaddress="[email protected]"

                                                defaultsuffix=""

                                                buildresultsurl="http://10.132.119.163:8080">

<!—always指定的郵箱位址是每次建構結果都會發送,不論建構成功或者失敗,failure指定的是建構失敗發送的郵箱位址-->

            <always address="[email protected]" />

                                                <always address="[email protected]"/>

                                                <always address="[email protected]"/>

            <failure address="[email protected]" />

         </email>

    </publishers>

  </project>

</cruisecontrol>

第3

:寫build檔案:

<?xml version="1.0"?>

<project name="airclient" default="experimentBuild" basedir=".">

            <property environment="env" />

            <property name="P4_PORT" value="peuck:1710" />

            <property name="P4_CLIENT" value="Jissie-xp" />

            <property name="P4_USER" value="guang" />

            <property name="P4_PASSWD" value="pass1234" />

            <property name="p4.view" value="//HL/AIRClient/..."/>

            <property name="experiment.dir" value="${basedir}/checkout/Experiment"/>

            <!--if AIRClient has changed, get latest source code from perforce-->

    <target name="checkout">       

        <p4sync port="${P4_PORT}"

                          user="${P4_USER}"

                          view="${p4.view}"

                          client="${P4_CLIENT}"

                          force="true" />

    </target>     

            <!--call AIRClient build file to build souce code and generate headlights.swc-->

            <target name="masterbuild" depends="checkout,copyAirBuildFile">

                        <ant inheritall="false"

                                    antfile="build.xml"

                                    dir="${basedir}/checkout/AIRClient/Source"

                                    target="compile"/>

            </target>

            <!--call AIRClient build file to clean project-->

            <target name="cleanbuild" depends="checkout">

                        <ant inheritall="false"

                                    antfile="build.xml"

                                    dir="${basedir}/checkout/AIRClient/Source/build"

                                    target="clean"/>

            </target>

            <target name="experimentBuild" depends="masterbuild,copySWCFile">

                        <ant inheritall="false"

                                    antfile="build.xml"

                                    dir="${experiment.dir}"

                                    target="main"/>

            </target>

            <!-- because ant dir in config.xml is the basedir of AIRClient build file, so I need to copy the build.xml file to its base dir-->

            <target name="copyAirBuildFile">

       <copy todir="${basedir}/checkout/AIRClient/Source">

                                    <fileset dir="${basedir}/checkout/AIRClient/Source/build">

                                                <include name="build.properties"/>

                                                <include name="build.xml"/>

                                    </fileset>

                        </copy>

   </target>

   <!--when AIRClient source code changes, first build and generate headlights.swc file, then copy this file to project Experiment as a lib file-->

   <target name="copySWCFile">

       <copy file="${basedir}/checkout/AIRClient/Source/bin/headlights.swc"

                                    tofile="${experiment.dir}/libs/headlights.swc"/>

   </target>

</project>

第4

:配置顯示建構結構的網頁,下載下傳tomcat伺服器。。。