我所在的项目有无数个测试环境,每一个环境都有不同版本的代码,所以有了bug以后,提交了代码也不一定能在短时间内部署到所有的测试环境中,这样就有可能出现bug改好了,代码页提交了,但是还有tester在某个环境上报同样的bug,这就需要一个方法能取得所在环境的代码版本以确定改动是不是已经部署到那个环境中去了。
因为我们使用svn和ant,所以就在代码的build脚本中加入了一个target把svn code version生成到一个html文件里面, 并让这个target在每次build的时候都自动执行,这样每次build就把当前的svn版本号生成为html文件并且部署到服务器上,并直接通过url来查看当前环境的代码版本。
具体的ant脚本如下
<target name="generateCodeVersion">
<exec executable="svn" output="${webroot}/version.html" failοnerrοr="false" failifexecutionfails="false">
<arg line="info"/>
</exec>
</target>
这个target会在${webroot}下生成一个version.html文件,里面的内容格式如下:
Path: .
URL: http://88.8.888.88/lods/dev_2.0.0
Repository Root: http://10.3.100.78/code
Repository UUID: 465e05aa-64dd-4f22-9bd7-d30a52464735
Revision: 13308
Node Kind: directory
Schedule: normal
Last Changed Author: rainman
Last Changed Rev: 132887
Last Changed Date: 2009-01-26 12:49:03 -0700 (Mon, 26 Jan 2009)
如果你不想暴露你的repository和svn代码库的url, 可以修改ant脚本如下:
<target name="generateCodeVersion">
<exec executable="svn" output="${webroot}/version.html" failοnerrοr="false" failifexecutionfails="false">
<arg line="info"/>
<redirector>
<outputfilterchain>
<linecontainsregexp>
<regexp pattern="^Revision:"/>
</linecontainsregexp>
<tokenfilter>
<replaceregex pattern="Revision/:/s*" flags="s" replace=""/>
</tokenfilter>
</outputfilterchain>
</redirector>
</exec>
</target>
这样就会只保留Revision号,而不包括其他信息,html里面的内容也就只有13308这个数字了。
同样的方法也可以得到svn的tag号,比如1.0,2.0等等。