做互聯(lián)網(wǎng)應(yīng)用的各位tx都已經(jīng)被部署過程的繁瑣煩透了吧?

先要壓縮,然后一臺機(jī)器一臺機(jī)器的上傳,接著還要停止服務(wù)器,解壓縮,重新啟動。。。。。。

如果只有兩天臺機(jī)器,那么還可以忍受;但是如果你有七八臺機(jī)器要部署時,你的感覺一定是要瘋掉了。更何況如工程比較大的話,壓縮、上傳都要花費(fèi)很多的時間,我們寶貴的時間就這樣溜走了。更可惡的是我們很多時候需要加班也是因?yàn)椴渴鸬男侍?/p>

廢話太多了,還是直接進(jìn)入主題吧,讓你和我們一起分享部署的樂趣吧!

 

首先說明幾個主要的ant命令:

  1. scp:一個optional task,用來上傳文件到遠(yuǎn)程服務(wù)器上;
  2. sshexec:ssh大家很熟悉吧?這個任務(wù)就是用來執(zhí)行一個ssh腳本命令,可以執(zhí)行遠(yuǎn)程服務(wù)器的命令;
  3. fileset:這個大家應(yīng)該也比較熟悉,用來設(shè)置一個文件的集合,關(guān)鍵的是它的一個子元素date可以用來設(shè)定文件的日期范圍,比如<date datetime="2007.12.18 00:00:00" pattern="yyyy.MM.dd HH:mm:ss" when="after" />指定2007年12月18日以后的文件,具體的說明請參見http://ant.apache.org/manual/index.html

主要就是這幾個命令,很簡單吧。

接著我們分析一下我們部署的一般流程吧。

  1. 我們要先把本地的文件壓縮到一個壓縮文件中,實(shí)現(xiàn)增量部署的關(guān)鍵也是在這里(感謝付成睿的幫助)。最好的方式是每次只壓縮最近修改的內(nèi)容,而那些沒有修改的內(nèi)容沒有必要再次上傳。壓縮一個時間點(diǎn)以后的文件可以通過date來實(shí)現(xiàn),不過我們需要自動的記錄上一次部署的時間。現(xiàn)在有兩種方式可以做這件事,一種是付成睿的,比較簡單,但會修改配置文件;另外一種就是我的方式,也就是將日期保存到一個文件中,如果沒有文件就認(rèn)為是全部部署,并創(chuàng)建文件。
    具體的代碼如下:
     <target name="appZipModified">
      <!-- 判斷文件是否存在 -->
      <condition property="local.app.timestamp.present">
       <available file="${app.zip.timestamp.file}" type="file" />
      </condition>
      <!-- 如果文件不存在則創(chuàng)建文件 -->
      <antcall target="mkStampFile">
       <param name="zipFilePresent" value="${local.app.timestamp.present}" />
       <param name="newZipFile" value="${app.zip.timestamp.file}" />
      </antcall>
      <!-- 從文件中獲取上次部署的時間 -->
      <echo>Load the old timestamp from the disk file</echo>
      <loadfile property="old.zip.timestamp" srcFile="${app.zip.timestamp.file}" />
      <!-- 獲取當(dāng)前時間 -->
      <tstamp>
       <format property="this.zip.timestamp" pattern="${ts.pattern}" />
      </tstamp>
      <echo>zip the new modified files &amp; folders that after ${old.zip.timestamp} to ${appzip} </echo>
      <!-- 先刪除上次的zip文件,這樣保證上次的壓縮的文件不會再次上傳 -->
      <delete file="${appzip}" />
      <!-- 執(zhí)行壓縮操作 -->
      <zip destfile="${appzip}">
       <fileset dir="../WebRoot">
        <include name="**/*" />
        <!-- 這個語句是關(guān)鍵,只壓縮old.zip.timestamp以后修改的文件 -->
        <date datetime="${old.zip.timestamp}" pattern="${ts.pattern}" when="after" />
       </fileset>
      </zip>
      <echo>Replace the old timestamp with the new timestamp</echo>
      <!-- 最后將當(dāng)前的時間更新到文件中 -->
      <replace file="${app.zip.timestamp.file}" token="${old.zip.timestamp}" value="${this.zip.timestamp}" />
     </target>
     <!-- 創(chuàng)建文件的操作,unless的含義是zipFilePresent為false時才執(zhí)行,與之對應(yīng)的是if -->
     <target name="mkStampFile" unless="zipFilePresent">
      <echo>Create txt file to store timestamp</echo>
      <!-- 創(chuàng)建一個文件 -->
      <touch file="${newZipFile}" datetime="12/19/2007 21:20 pm" />
      <!-- 應(yīng)用正則表達(dá)式的replace命令,寫入一個很早的時間(正則真是太神奇了!) -->
      <replaceregexp file="${newZipFile}" match=".*" replace="2000.01.01 00:00:00" byline="true" />
     </target>

    這種方式還是比較復(fù)雜的,使用付成睿的方法則更簡單。只是需要在properties文件中增加last.zip.timestamp的設(shè)置。
     <target name="zipModified">
      <echo>zip modified files</echo>
      <echo>get current time stamp</echo>
      <tstamp>
       <format property="this.zip.timestamp" pattern="${ts.pattern}" />
      </tstamp>
      <echo>current time stamp: ${this.zip.timestamp}</echo>
      <echo>zip modified files aflter ${last.zip.timestamp}</echo>
      <zip destfile="${local.testZip}">
       <fileset dir="./test">
        <date datetime="${last.zip.timestamp}" pattern="${ts.pattern}" when="after" />
       </fileset>
      </zip>
      <echo>save this zip time stamp</echo>
      <replace file="build.properties" token="${last.zip.timestamp}" value="${this.zip.timestamp}" />
     </target>
  2. 有了zip文件后下面就需要把文件上傳到服務(wù)器上,方法如下:
     <!-- 上傳zip壓縮文件 -->
     <target name="uploadZipApp" description="upload ziped app file to remote server">
      <echo>upload ziped app file to remote server</echo>
      <scp file="${appzip}" todir="user
    @${host}:/path/test.zip" password="${pass}" trust="yes" />
     </target>
    很簡單吧?
  3. 上傳完后就是在服務(wù)器的操作了(例子用的是unix),包括解壓縮、停止服務(wù)器、重新啟動等,主要還是如何遠(yuǎn)程調(diào)用服務(wù)器命令。
    下面的例子是將文件解壓縮,最關(guān)鍵的是sshexec命令的用法。
      <!-- 解壓縮備份文件 -->
     <target name="unZipBackupResource" description="decompress the backup tar file back to the file system">
      <echo>decompress the backup tar file back to the file system</echo>
      <sshexec host="${host}" username="user" command="tar xvf test.tar" password="${pass}" trust="yes" />
     </target>
    做其他的操作只要把上面例子中的黑體字部分去掉就可以了。
  4. 最后最好將一個完整的部署流程封裝到一個target中,這樣部署一臺服務(wù)器只要輸入相應(yīng)的密碼就可以了。
  5. 而現(xiàn)在部署時你要做的操作只是在outline模式下在一個部署的target上點(diǎn)擊右鍵,然后run就可以了。

這就是完整的流程。

如果部署的是jsp文件或者靜態(tài)文件,那就更簡單了,直接上傳解壓縮就可以了。

其他tz有什么更好的方法也一起分享啊,我在這算是拋磚引玉了。。。。。。