ant 范例操作
ant 实例操作?主要就是这几个命令,很简单吧。接着我们分析一下我们部署的一般流程吧。我们要先把本地的文件
ant 实例操作
?
主要就是这几个命令,很简单吧。
接着我们分析一下我们部署的一般流程吧。
我们要先把本地的文件压缩到一个压缩文件中,实现增量部署的关键也是在这里(感谢付成睿的帮助)。最好的方式是每次只压缩最近修改的内容,而那些没有修改的内容没有必要再次上传。压缩一个时间点以后的文件可以通过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有什么更好的方法也一起分享啊,我在这算是抛砖引玉了。。。。。。