Flex ant tasks 提供了一种方便的手段使用工业标准级的构建工具来编译你的Flex工程。
(The Flex Ant tasks provide a convenient way to build your Flex projects using an industry-standard build management tool)
下面以我在做的一个示例工程为例来使用flex ant tasks来编译工程文件:
1.在现有的工程下建了个ant目录,用来放附件中的相关文件
2.将附件的包解压到这个目录下,目录结构如下
ant
|___bin----------------------编译结果输出目录
|___flexTasks-------------flex ant tasks相关的库文件和封装html所需要的文件
|___build.xml--------------任务配置文件
3.build.xml文件内容如下
- xml version="1.0" encoding="utf-8"?>
- <project name="Flex Ant Builder Sample Project" basedir=".">
- <taskdef resource="flexTasks.tasks" classpath="${basedir}/flexTasks/lib/flexTasks.jar" />
- <property name="FLEX_HOME" value="C:/Program Files/Adobe/Flex Builder 2/Flex SDK 2"/>
- <property name="APP_ROOT" value="../srcFX"/>
- <property name="DEPLOY_DIR" value="bin"/>
- <property name="fileName" value="helloworldFX" />
- <property name="fileExt" value="mxml" />
- <property name="THIRD_PARTY" value="D:/work/thirdparty/FlexLib" />
- <property name="mainApp" value="" />
- <property name="package" value="" />
- <target name="compile" depends="cleanCompile">
- <mxmlc
- file="${APP_ROOT}/${package}${fileName}.${fileExt}"
- output="${DEPLOY_DIR}/${package}${fileName}.swf"
- actionscript-file-encoding="UTF-8"
- keep-generated-actionscript="false"
- warnings="false"
- incremental="true"
- >
- <compiler.source-path path-element="../srcFX"/>
- <compiler.source-path path-element="../../srcFX"/>
- <load-config filename="${FLEX_HOME}/frameworks/flex-config.xml"/>
- <source-path path-element="${FLEX_HOME}/frameworks"/>
- <compiler.library-path dir="${FLEX_HOME}/frameworks" append="true">
- <include name="libs" />
- <include name="../bundles/{locale}" />
- compiler.library-path>
- <compiler.include-libraries dir="${THIRD_PARTY}" append="true">
- <include name="DistortionEffects.swc" />
- compiler.include-libraries>
- mxmlc>
- <delete>
- <fileset dir="${APP_ROOT}/${package}" includes="${fileName}*.cache" defaultexcludes="false"/>
- delete>
- target>
- <target name="CompileAndGenerateLinkReport">
- <mxmlc
- file="${APP_ROOT}/${package}${fileName}.${fileExt}"
- link-report="${APP_ROOT}/${package}${fileName}_LinkReport.xml"
- output="${DEPLOY_DIR}/${package}${fileName}.swf"
- actionscript-file-encoding="UTF-8"
- keep-generated-actionscript="false"
- incremental="true"
- >
- <load-config filename="${FLEX_HOME}/frameworks/flex-config.xml"/>
- <source-path path-element="${FLEX_HOME}/frameworks"/>
- <source-path path-element="D:/study/helloworld/srcFX"/>
- <compiler.library-path dir="${FLEX_HOME}/frameworks" append="true">
- <include name="libs" />
- <include name="../bundles/{locale}" />
- compiler.library-path>
- <default-size width="500" height="600" />
- mxmlc>
- <delete>
- <fileset dir="${APP_ROOT}/${package}" includes="${fileName}*.cache" defaultexcludes="false"/>
- delete>
- target>
- <target name="CompileModuleWithLinkReport">
- <mxmlc
- file="${APP_ROOT}/${package}${fileName}.${fileExt}"
- load-externs="${APP_ROOT}/${mainApp}_LinkReport.xml"
- output="${DEPLOY_DIR}/${package}${fileName}.swf"
- actionscript-file-encoding="UTF-8"
- keep-generated-actionscript="false"
- incremental="true"
- >
- <load-config filename="${FLEX_HOME}/frameworks/flex-config.xml"/>
- <source-path path-element="${FLEX_HOME}/frameworks"/>
- <compiler.library-path dir="${FLEX_HOME}/frameworks" append="true">
- <include name="libs" />
- <include name="../bundles/{locale}" />
- compiler.library-path>
- <default-size width="500" height="600" />
- mxmlc>
- <delete>
- <fileset dir="${APP_ROOT}/${package}" includes="${fileName}*.cache" defaultexcludes="false"/>
- delete>
- target>
- <target name="wrapper" depends="cleanWrapper">
- <html-wrapper
- title="Flex Ant Builder Sample"
- width="100%"
- height="100%"
- application="flexApp"
- swf="${fileName}"
- version-major="9"
- version-minor="0"
- version-revision="0"
- history="true"
- template="express-installation"
- output="${DEPLOY_DIR}/${package}"/>
- <move file="${DEPLOY_DIR}/${package}index.html" tofile="${DEPLOY_DIR}/${fileName}.html"/>
- target>
- <target name="cleanCompile">
- <delete dir="${APP_ROOT}/${package}generated"/>
- <delete>
- <fileset dir="${DEPLOY_DIR}/${package}" includes="${fileName}*.swf"/>
- delete>
- target>
- <target name="cleanWrapper">
- <delete>
- <fileset dir="${DEPLOY_DIR}/${package}" includes="history.swf" defaultexcludes="false"/>
- <fileset dir="${DEPLOY_DIR}/${package}" includes="playerProductInstall.swf" defaultexcludes="false"/>
- <fileset dir="${DEPLOY_DIR}/${package}" includes="${fileName}*.html" defaultexcludes="false"/>
- <fileset dir="${DEPLOY_DIR}/${package}" includes="$history.htm" defaultexcludes="false"/>
- <fileset dir="${DEPLOY_DIR}/${package}" includes="*.js" defaultexcludes="false"/>
- delete>
- target>
- project>
几点说明:
(1)FLEX_HOME是FlexBuilder安装目录下的SDK目录
(2)APP_ROOT是Flex应用的根目录,这个目录下的文件就是需要编译的mxml文件
(3)DEPLOY_DIR是编译后文件存放的目录
(4)fileName需要编译的文件名称,不包括扩展名称,扩展名称由下面的属性指定
(5)fileExt需要编译的文件的扩展名称
(6)因为你的源代码目录可能不是和你的APP_ROOT目录在一起,而是通过additional source来加入的工程里的,因此可以设置下源代码的目录,如下:
<compiler.source-path path-element="../srcFX"></compiler.source-path>
- <!-- ***** source file path *******-->
- <compiler.source-path path-element="../srcFX"/>
- <compiler.source-path path-element="../../srcFX"/>
<compiler.source-path path-element="../../srcFX"></compiler.source-path>
(7)另外工程中可能引用了第三方的组件,因此这里在build.xml中定义了第三方组件存放位置的属性
<property value="D:/work/thirdparty/FlexLib" name="THIRD_PARTY"></property>
- <!-- third party swc-->
- <property name="THIRD_PARTY" value="D:/work/thirdparty/FlexLib" />
这样在编译时就可以引用这个目录下的swc文件了,如下:
- <!-- ***** thirdparth swc ***** -->
- <compiler.include-libraries dir="${THIRD_PARTY}" append="true">
- <include name="DistortionEffects.swc" />
- </compiler.include-libraries>
(8)warnings="false"不显示编译时出现的warning提示
最后,到上面所建的ant目录下,在命令行下执行ant -compile就可以编译工程文件了(或者建个批处理文件来执行)。