Flex项目ant脚本编译示例

本文分享了一套用于Flex项目的Ant自动化编译脚本,详细介绍了模块编译、库项目编译、文档生成等步骤,并提供了完整的XML配置示例。

用lunt build自动化部署项目时时常要用到ant编译客户端代码,在网上找到的资料基本都不完善,很难成功编译项目,特别是经常出现一些细节问题,令到排错十分困难。因此,将个人已成功应用于编译Flex项目的ant script共享出来,以备不时之需。
下面脚本的完整代码。

此脚本包含以下几部分内容
1.Module的编译(生成RSL访问路径)
2.library project的编译(包括生成统一的命名空间)
3.生成html template
4.生成as doc

在SDK 4.1的环境下测试成功。


<?xml version="1.0" encoding="UTF-8"?>
<project name="platform" default="buildFlex" basedir=".">
	<property file="build.properties" />

	<!-- is platform project -->
	<property name="compileSWCProject" value="true"/>
	
	<!--set the flexTasks path-->
	<path id="ant.classpath">
	    <fileset dir="${FLEX_HOME}/ant/lib">
			<include name="*.jar" />
		</fileset>
	</path>
	<taskdef resource="flexTasks.tasks" classpathref="ant.classpath"/>
	<taskdef resource="net/sf/antcontrib/antlib.xml" classpathref="ant.classpath" />
	
	<!-- task execute sequence -->
	<target name="buildFlex">
		<antcall target="cleanFlexDir"/>
		<antcall target="compressMainSource"/>
		<antcall target="wrapper"/>
		<antcall target="copyResource"/>
		<if>
			<istrue value="${compileSWCProject}"/>
			<then>
				<antcall target="compileLib"/>
				<antcall target="genarateDoc"/>
			</then>
		</if>
		<antcall target="compileFlexModule"/>
		<antcall target="copySWF"/>
	</target>
	
	<!-- recreate the deploy folder -->
	<target name="cleanFlexDir">
		<echo message="clean..." />	
		<delete dir="${DIST_DIR}/${LIBS_DIR}" />
		<mkdir dir="${DIST_DIR}/${LIBS_DIR}" />
		
		<delete dir="${DEPLOY_DIR}/flex" />
		<mkdir dir="${DEPLOY_DIR}/flex" />
	</target>

	<!-- create template-->
	<target name="wrapper">
		
		<!--main app-->
		<echo message="create the index html template..." />
		<html-wrapper title="GRG Platform" file="Index.html"
			        height="100%" width="100%" application="app" swf="Index"
			 		version-major="10" version-minor="0" version-revision="0"
			        express-install="true" output="${DEPLOY_DIR}/flex" />
	</target>
	
	<!-- recreate the deploy's directory -->
	<target name="copyResource">
		<echo message="Flex SDK Home: ${FLEX_HOME}" />		
		
		<!-- copy Index.html -->
		<copy todir="${DEPLOY_DIR}/flex" file="${SRC_DIR}/Index.html" overwrite="true" />
		
		<!--copy flex libs-->
		<echo message="prepare the flex libs..." />
		<copy todir="${DIST_DIR}/${LIBS_DIR}">
			<fileset dir="${basedir}/${LIBS_DIR}"/>
		</copy>
		
		<!--copy rsls files-->
		<echo message="prepare the rsls files..." />
		<copy todir="${DEPLOY_DIR}/flex">
			<fileset dir="${FLEX_HOME}/frameworks/rsls">
				<include name="*.swf"/>
				<include name="*.swz"/>
			</fileset>
		</copy>
		
		<!--copy resource files-->
		<echo message="prepare the resource files..." />
		<copy todir="${DEPLOY_DIR}/flex/resources">
			<fileset dir="${SRC_DIR}/resources"/>
		</copy>
	</target>

	<!-- compress main source -->
	<target name="compressMainSource">	 		 
		<zip destfile="${DIST_DIR}/mainSource.zip">
			<fileset dir="${SRC_DIR}">
				<include name="Index.html"/>
				<include name="*.mxml"/>
			</fileset>
		</zip>
	</target>

	<!-- gen doc -->
	<target name="genarateDoc">
		<asdoc output="${DIST_DIR}/${DOC_DIR}" lenient="true" failonerror="false"> 
			<!-- doc ref class -->
			<doc-sources path-element="${SWC_PROJECT}/src"/>
			
			<!-- source path -->
			<source-path path-element="${SWC_PROJECT}/src" />
			
			<!-- external lib reference -->
			<external-library-path dir="${SWC_PROJECT}/libs">
				<include name="**/*.swc" />
			</external-library-path>
		</asdoc>
		<zip basedir="${DIST_DIR}/${DOC_DIR}" destfile="${DIST_DIR}/${DOC_DIR}.zip" />
		<delete dir="${DIST_DIR}/${DOC_DIR}" />
	</target>
	
	<!-- compile library -->
	<target name="compileLib">
		<echo message="start complie Flex Library Project..." />

		<compc output="${DIST_DIR}/${LIBS_DIR}/${SWC_LIB_NAME}.swc" debug="false" incremental="true">
			
			<!-- sdk namespaces -->
			<namespace uri="http://ns.adobe.com/mxml/2009" manifest="${FLEX_HOME}/frameworks/mxml-2009-manifest.xml"/>
			<namespace uri="library://ns.adobe.com/flex/spark" manifest="${FLEX_HOME}/frameworks/spark-manifest.xml"/>
			<namespace uri="library://ns.adobe.com/flex/mx" manifest="${FLEX_HOME}/frameworks/mx-manifest.xml"/>
			<namespace uri="http://www.adobe.com/2006/mxml" manifest="${FLEX_HOME}/frameworks/mxml-manifest.xml"/>
			
			<!-- grgbanking namespace -->
			<namespace uri="library://ns.grgbanking.com/platform/flex" manifest="${SWC_PROJECT}/manifest.xml"/>
			<include-namespaces uri="library://ns.grgbanking.com/platform/flex"/>
			
			<!-- swc project source dir -->
			<source-path path-element="${SWC_PROJECT}/src" />
			
			<!-- need to compile file types -->
			<include-sources dir="${SWC_PROJECT}/src">
				<include name="**/*.as" />
				<include name="**/*.mxml" />
			</include-sources>
			
			<!-- reference the sdk libs -->
			<external-library-path dir="${FLEX_HOME}/frameworks">
				<include name="**/*.swc" />
			</external-library-path>
			
			<!-- external lib reference -->
			<external-library-path dir="${SWC_PROJECT}/libs">
				<include name="**/*.swc" />
			</external-library-path>
		</compc>
		
		<!-- delete cache files -->
		<delete>
			<fileset dir="${DIST_DIR}/${LIBS_DIR}" includes="*.cache" />
		</delete>
		
		<!-- copy flex lib -->
		<copy todir="${DIST_DIR}" file="${DIST_DIR}/${LIBS_DIR}/${SWC_LIB_NAME}.swc" overwrite="true" />
		
		<echo message="compile Flex Library Project finished! ${LIBS_DIR}/${SWC_LIB_NAME}.swc" />
	</target>

	<!-- copy modules -->
	<target name="copySWF">
		<echo message="copy another modules to deploy folder..." />
		
		<for param="file">
			<path>
				<fileset dir="${SRC_DIR}">					
					<!-- anothoer modules -->
					<include name="**/*.swf" />
				</fileset>
			</path>
			
			<sequential>
				<!-- find out the module files with regexp & compile it -->
				<propertyregex override="yes" property="release.dir" input="@{file}" 
							regexp="(.*)flex_src\\(.*)\\([^\\]+)" casesensitive="false"
							replace="\2"/>
				
				<copy todir="${DEPLOY_DIR}/flex/${release.dir}" file="@{file}" overwrite="true" />
				<echo message="copy @{file} =====>> ${DEPLOY_DIR}/flex/${release.dir}" />
				<echo message=" " />
			</sequential>
		</for>
	</target>
	
	<!-- compile modules -->
	<target name="compileFlexModule">
		<echo message="start compile Flex Project..." />
		<for param="file">
			<path>
				<fileset dir="${SRC_DIR}">
					
					<!-- Index -->
					<include name="Index.mxml"/>
					
					<!-- anothoer modules -->
					<include name="**/*Module.mxml" />
					<!--<include name="**/ParamModule.mxml" />-->
				</fileset>
			</path>
			
			<sequential>
				<!-- find out the module files with regexp & compile it -->
				<propertyregex override="yes" property="release.name" input="@{file}" 
							regexp="(.*)flex_src(.*)(mxml)" casesensitive="false"
							replace="flex\2swf"/>
				
				<!-- replace the rsls path start-->
				<propertyregex override="yes" property="tmp.rsl" input="${release.name}"
							regexp="flex(.*\\)[^\\]*" select="\1" />
				<propertyregex override="yes" property="tmp.rsl1" input="${tmp.rsl}"
							regexp="[^\\]+" replace=".." defaultValue=""/> 
				<propertyregex override="yes" property="tmp.rsl2" input="${tmp.rsl1}"
							regexp="\\(.*)" select="\1" defaultValue=""/>
				<propertyregex override="yes" property="rsl.path" input="${tmp.rsl2}"
							regexp="\\" replace="\/" defaultValue=""/>
				<!-- replace the rsls path end-->
				<mxmlc file="@{file}" output="${DEPLOY_DIR}/${release.name}" optimize="true"
						debug="false" incremental="true" isolate-styles="false">
					<!--jvm args-->
					<jvmarg value="-Xmx2048m"/>
					<jvmarg value="-Xms512m"/>
					<jvmarg value="-XX:MaxPermSize=512m"/>
					
					<!--reference source code-->
					<source-path path-element="${SRC_DIR}"/>
					
					<!--rsl setting-->
					<static-link-runtime-shared-libraries>false</static-link-runtime-shared-libraries>
					
					<!-- rsls file setting start-->
					<!--framework rsl-->
					<runtime-shared-library-path path-element="${FLEX_HOME}/frameworks/libs/textLayout.swc">
						<url rsl-url="${rsl.path}textLayout_1.1.0.604.swz" policy-file-url=""/>
						<url rsl-url="${rsl.path}textLayout_1.1.0.604.swf" policy-file-url=""/>
				    </runtime-shared-library-path>
					<runtime-shared-library-path path-element="${FLEX_HOME}/frameworks/libs/osmf.swc">
						<url rsl-url="${rsl.path}osmf_flex.4.0.0.13495.swz" policy-file-url=""/>
						<url rsl-url="${rsl.path}osmf_flex.4.0.0.13495.swf" policy-file-url=""/>
				    </runtime-shared-library-path>
					<runtime-shared-library-path path-element="${FLEX_HOME}/frameworks/libs/framework.swc">
						<url rsl-url="${rsl.path}framework_4.1.0.16076.swz" policy-file-url=""/>
						<url rsl-url="${rsl.path}framework_4.1.0.16076.swf" policy-file-url=""/>
				    </runtime-shared-library-path>
					<runtime-shared-library-path path-element="${FLEX_HOME}/frameworks/libs/spark.swc">
						<url rsl-url="${rsl.path}spark_4.1.0.16076.swz" policy-file-url=""/>
						<url rsl-url="${rsl.path}spark_4.1.0.16076.swf" policy-file-url=""/>
				    </runtime-shared-library-path>
					<runtime-shared-library-path path-element="${FLEX_HOME}/frameworks/libs/sparkskins.swc">
						<url rsl-url="${rsl.path}sparkskins_4.1.0.16076.swz" policy-file-url=""/>
						<url rsl-url="${rsl.path}sparkskins_4.1.0.16076.swf" policy-file-url=""/>
				    </runtime-shared-library-path>
					<runtime-shared-library-path path-element="${FLEX_HOME}/frameworks/libs/rpc.swc">
						<url rsl-url="${rsl.path}rpc_4.1.0.16076.swz" policy-file-url=""/>
						<url rsl-url="${rsl.path}rpc_4.1.0.16076.swf" policy-file-url=""/>
				    </runtime-shared-library-path>
					<!-- rsls file setting end-->
					
					<library-path dir="${DIST_DIR}/${LIBS_DIR}">
						<include name="**/*.swc"/>
					</library-path>
						
					<!--framework libs-->
					<library-path dir="${FLEX_HOME}/frameworks">
						<include name="**/*.swc"/>
					</library-path>
				</mxmlc>
				
				<!-- remove cache file -->
				<delete file="${DEPLOY_DIR}/${release.name}.cache" />
				
				<!--echo info-->
				<echo message="Compiled @{file} =====>>   ${DEPLOY_DIR}/${release.name}" />
				<echo message=" " />
			</sequential>
		</for>
		
		<!-- delete lib dir -->
		<delete dir="${DIST_DIR}/${LIBS_DIR}" />
	</target>
</project>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hhq2607

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值