原文:http://www.blogjava.net/baoyaer/articles/169126.html
做網際網路應用的各位tx都已經被部署過程的繁瑣煩透了吧?
先要壓縮,然後一台機器一台機器的上傳,接着還要停止伺服器,解壓縮,重新啟動。。。。。。
如果隻有兩天台機器,那麼還可以忍受;但是如果你有七八台機器要部署時,你的感覺一定是要瘋掉了。更何況如工程比較大的話,壓縮、上傳都要花費很多的時間,我們寶貴的時間就這樣溜走了。更可惡的是我們很多時候需要加班也是因為部署的效率太慢。
廢話太多了,還是直接進入主題吧,讓你和我們一起分享部署的樂趣吧!
首先說明幾個主要的ant指令:
- scp:一個optional task,用來上傳檔案到遠端伺服器上;
- sshexec:ssh大家很熟悉吧?這個任務就是用來執行一個ssh腳本指令,可以執行遠端伺服器的指令;
- fileset:這個大家應該也比較熟悉,用來設定一個檔案的集合,關鍵的是它的一個子元素date可以用來設定檔案的日期範圍,比如<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;
主要就是這幾個指令,很簡單吧。
接着我們分析一下我們部署的一般流程吧。
-
我們要先把本地的檔案壓縮到一個壓縮檔案中,實作增量部署的關鍵也是在這裡(感謝付成睿的幫助)。最好的方式是每次隻壓縮最近修改的内容,而那些沒有修改的内容沒有必要再次上傳。壓縮一個時間點以後的檔案可以通過date來實作,不過我們需要自動的記錄上一次部署的時間。現在有兩種方式可以做這件事,一種是付成睿的,比較簡單,但會修改配置檔案;另外一種就是我的方式,也就是将日期儲存到一個檔案中,如果沒有檔案就認為是全部部署,并建立檔案。
具體的代碼如下:
<target name="appZipModified">
<!-- 判斷檔案是否存在 -->
<condition property="local.app.timestamp.present">
<available file="${app.zip.timestamp.file}" type="file" />
</condition>
<!-- 如果檔案不存在則建立檔案 -->
<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}" />
<!-- 擷取目前時間 -->
<tstamp>
<format property="this.zip.timestamp" pattern="${ts.pattern}" />
</tstamp>
<echo>zip the new modified files & folders that after ${old.zip.timestamp} to ${appzip} </echo>
<!-- 先删除上次的zip檔案,這樣保證上次的壓縮的檔案不會再次上傳 -->
<delete file="${appzip}" />
<!-- 執行壓縮操作 -->
<zip destfile="${appzip}">
<fileset dir="../WebRoot">
<include name="**/*" />
<!-- 這個語句是關鍵,隻壓縮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>
<!-- 最後将目前的時間更新到檔案中 -->
<replace file="${app.zip.timestamp.file}" token="${old.zip.timestamp}" value="${this.zip.timestamp}" />
</target>
<!-- 建立檔案的操作,unless的含義是zipFilePresent為false時才執行,與之對應的是if -->
<target name="mkStampFile" unless="zipFilePresent">
<echo>Create txt file to store timestamp</echo>
<!-- 建立一個檔案 -->
<touch file="${newZipFile}" datetime="12/19/2007 21:20 pm" />
<!-- 應用正規表達式的replace指令,寫入一個很早的時間(正則真是太神奇了!) -->
<replaceregexp file="${newZipFile}" match=".*" replace="2000.01.01 00:00:00" byline="true" />
</target>
這種方式還是比較複雜的,使用付成睿的方法則更簡單。隻是需要在properties檔案中增加last.zip.timestamp的設定。
<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>
-
有了zip檔案後下面就需要把檔案上傳到伺服器上,方法如下:
<!-- 上傳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>
很簡單吧?
-
上傳完後就是在伺服器的操作了(例子用的是unix),包括解壓縮、停止伺服器、重新啟動等,主要還是如何遠端調用伺服器指令。
下面的例子是将檔案解壓縮,最關鍵的是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>
做其他的操作隻要把上面例子中的黑體字部分去掉就可以了。
- 最後最好将一個完整的部署流程封裝到一個target中,這樣部署一台伺服器隻要輸入相應的密碼就可以了。
- 而現在部署時你要做的操作隻是在outline模式下在一個部署的target上點選右鍵,然後run就可以了。
這就是完整的流程。
如果部署的是jsp檔案或者靜态檔案,那就更簡單了,直接上傳解壓縮就可以了。
其他tz有什麼更好的方法也一起分享啊,我在這算是抛磚引玉了。。。。。。