让ant帮你提高部署的效率吧!

做互联网应用的各位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文件或者静态文件,那就更简单了,直接上传解压缩就可以了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值