`
zawa
  • 浏览: 58144 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

使用Apache Ant及SVN进行自动化构建

阅读更多

详见我的博客:http://www.zawaliang.com/2013/04/319.html

 

在团队里,一个项目通常对应多个开发人员,基于对性能优化的考虑,上线前往往需要对js、css等资源进行压缩、合并、修改时间戳,然后再按团队规范提交SVN,最后由SVN导出增量zip发布包进行发布。
传统的做法可能需要手工对一个个修改过的文件进行YUI压缩,然后更新相应的时间戳,最后根据SVN log提取文件打包。
既然工作流程都是基本固化的,作为懒人,就需要省时省力点的做法。
Apache Ant,一个将软件编译、测试、部署等步骤联系在一起加以自动化的一个工具。没有了解过的同学自己Google学习,下面我就以一个Demo为例,给大家介绍下Ant的大部分常用功能,并达到自动压缩、合并、SVN提交、生成增量zip包的目的。
 

Demo

假设我们的目录架构如下,html5为根目录,app里放了一个lottery的应用,app.json.tpl为统一的版本号文件,build.xml为Ant的入口文件;res为资源目录,里面也放了lottery的资源文件。

我们现在需要把每个xx.js压缩为xx.min.js,xx.css压缩为xx_min.css,并将更改版本号应用到app.json文件里。

html5/---
   |---app/
        |---lottery/
               |---a/
                   |---a.html
               |---b/
                   |---b.html
               |---index.html
               |---app.json
               |---app.json.tpl
               |---build.xml
   |---res/
        |---app/
             |---lottery/
                    |---css/
                         |---global/
                               |---global.css
                         |---index/
                               |---index.css
                    |---js/
                        |---common/
                               |---zepto.js
                               |---touch.js
                               |---app.js
                        |---index/
                               |---index.js

 

第三方库

因需要用到SVN、xpath、for循环等功能,需要引入第三方库,点击这里下载并解压到Ant的lib目录即可。

Ant-Contrib Tasks
 
svnant
 
svnant不支持svn1.7.x版本,这里使用svnkit1.7的版本
 
其他支持svn1.7.x的方案,未验证

 

build.xml

详细请参考:https://github.com/zawaliang/AntSvnDemo

下面说明一些需要注意的点:

  • project的basedir需要定位到根目录,这也是取SVN基准地址的前提
    <project name="lottery" default="build" basedir="../../" xmlns:ac="antlib:net.sf.antcontrib">
  • 基于SVN Commit Message进行提取需要根据指定的规则进行,这里以“[xxx_v1.0] abcde”的格式提取,最后生成的zip包命名为“xxx_v1.0.zip”
    <property name="svn.version-regexp" value=".*(\[(.+)\]).*" />
  • app.json.tpl为版本号文件,这里需要替换的token格式为:{/html5/res/app/lottery/js/common/zepto.min.js},其中的地址为文件相对根目录的地址,这个token最终会被替换为文件校验和(MD5值)
  • SVN日志数限制是一个经验值,由于不能指定某个revision区间,这里取的是所有日志,但限定提取的条数,这个请根据实际项目的经验值调整。
    <property name="svn.log-limit" value="100" />
  • 文件压缩
    <apply executable="java" verbose="true" dest="${path.build.js}" failonerror="true" parallel="false">
    			<fileset dir="${path.build.js}" includes="**/*.js" excludes="**/*${suffix.js}" />
    			<arg line="-jar" />
    			<arg path="${path.yuicompressor}" />
    			<arg line="--charset ${encode.js}" />
    			<arg value="--type" />
    			<arg value="js" />
    			<arg value="-o" />
    			<targetfile />
    			<mapper type="regexp" from="^(.*)\.js$" to="\1${suffix.js}"/>
    		</apply>
  • 文件合并
    <concat encoding="${encode.js}" outputencoding="${encode.js}" destfile="${path.build.js}/common/lottery${suffix.js}">
    			<header trimleading="yes">${copyright.zepto}</header>
    			<path path="${path.build.js}/common/zepto${suffix.js}" />
    			<path path="${path.build.js}/common/touch${suffix.js}" />
    			<path path="${path.build.js}/common/app${suffix.js}" />
    		</concat>
  • SVN初始化,由于写作时svnant官方还不支持svn1.7.x的版本,所以这里需要更新svnkit到1.7.9的版本方可
    <target name="svn-init">
    		<echo level="info" message="正在初始化SVN..." />
    		<path id="path.svnant">
    			<pathelement location="${ant.library.dir}/svnant/svnant.jar" />
    			<pathelement location="${ant.library.dir}/svnant/svnClientAdapter.jar" />
    			<pathelement location="${ant.library.dir}/svnant/ganymed.jar" />
    			<!-- svnkit-1.7.9 支持svn1.7.x -->
    			<pathelement location="${ant.library.dir}/svnkit-1.7.9/svnkit-1.7.9.jar" />
    			<pathelement location="${ant.library.dir}/svnkit-1.7.9/svnkit-javahl16-1.7.9.jar" />
    			<pathelement location="${ant.library.dir}/svnkit-1.7.9/sqljet-1.1.7.jar" />
    			<pathelement location="${ant.library.dir}/svnkit-1.7.9/antlr-runtime-3.4.jar" />
    			<pathelement location="${ant.library.dir}/svnkit-1.7.9/sequence-library-1.0.2.jar" />
    		</path>
    		<typedef resource="org/tigris/subversion/svnant/svnantlib.xml" classpathref="path.svnant" />
    		<svnSetting id="svn.settings" svnkit="true" javahl="false" username="${svn.user}" password="${svn.password}" failonerror="true" />
    	</target>
  • 关于SVN基准路径的获取,基于svn内部变量来自动获取当前工作副本的svn映射路径
    <ac:propertyregex property="repourl" override="true" casesensitive="true" global="true" input="${svn.info.repourl}" regexp="\\" replace="\\\\\\\\" defaultValue="${svn.info.repourl}" />
    		<ac:propertyregex property="repourl" override="true" casesensitive="true" global="true" input="${repourl}" regexp="\:" replace="\\\\:" defaultValue="${repourl}" />
    		<ac:propertyregex property="repourl" override="true" casesensitive="true" global="true" input="${repourl}" regexp="\." replace="\\\\." defaultValue="${repourl}" />
    		<ac:propertyregex property="svn.basedir" override="true" casesensitive="true" global="true" input="${svn.info.url}" regexp="${repourl}(.*)" select="\1" />
  • 提交新增的文件到SVN时注意不要使用fileset,否则会出现告警
    <!-- 使用fileset会导致失败 http://stackoverflow.com/questions/11402891/svn-ant-add-task-with-force-option-gives-is-already-under-version-control -->
    
    <add force="true" dir="${path.js}" />
    <add force="true" dir="${path.css}" />
    <add force="true" dir="${path.app}" />
  • SVN日志解析,文件路径提取及zip包处理
    <!-- 1. 查询commit message含指定版本号的节点 -->
    <!-- 2. 排除Action=Deleted的目录或文件 -->
    <copy property="log.paths" path="/log/logentry[contains(msg, '${svn.versionFormat}')]/paths/path[@action!='D']/text()" append="true" trim="true" propertySeparator="|" />

    先提取含指定版本号的节点,同时排除action为deleted的操作。在处理zip包时,对svnlog获取到的路径再加以判断,以排除那些delete操作的文件(夹)。

    <ac:var name="zip.isfile" unset="true" />
    				<available file="${zip.file}" type="file" property="zip.isfile" />
    				<ac:if>
    					<isset property="zip.isfile" />
    					<ac:then>
    						<ac:if>
    							<!-- ant build.xml不处理 -->
    							<equals arg1="${zip.file}" arg2="${ant.file}" />
    							<ac:else>
    								<echo level="info" message="File: ${zip.file}" />
    								<copy file="${zip.file}" tofile="${zip.tofile}" overwrite="true" preservelastmodified="true" />
    							</ac:else>
    						</ac:if>
    					</ac:then>
    					<ac:else>
    						<!-- 不存在,有可能是svn deleted的目录或文件 -->
    						<ac:var name="zip.isdir" unset="true" />
    						<available file="${zip.file}" type="dir" property="zip.isdir" />
    						<ac:if>
    							<isset property="zip.isdir" />
    							<ac:then>
    								<!-- 空目录处理 -->
    								<echo level="info" message="Dir: ${zip.file}" />
    								<mkdir dir="${zip.tofile}" />
    							</ac:then>
    							<ac:else>
    								<!-- 表明当前路径为svn deleted的文件或目录,不需要处理 -->
    							</ac:else>
    						</ac:if>
    					</ac:else>
    				</ac:if>

完整版请参考:https://github.com/zawaliang/AntSvnDemo

 

 

详见我的博客:http://www.zawaliang.com/2013/04/319.html

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics