使用Flex Ant Tasks编译工程文件

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文件内容如下

build.xml 代码
  1. <!---->xml version="1.0" encoding="utf-8"?>  
  2. <project name="Flex Ant Builder Sample Project" basedir=".">  
  3.     <taskdef resource="flexTasks.tasks" classpath="${basedir}/flexTasks/lib/flexTasks.jar" />          
  4.     <property name="FLEX_HOME" value="C:/Program Files/Adobe/Flex Builder 2/Flex SDK 2"/>  
  5.     <property name="APP_ROOT" value="../srcFX"/>  
  6.     <property name="DEPLOY_DIR" value="bin"/>  
  7.     <!---->  
  8.     <property name="fileName" value="helloworldFX" />  
  9.     <!---->  
  10.     <property name="fileExt" value="mxml" />  
  11.     <!---->  
  12.     <property name="THIRD_PARTY" value="D:/work/thirdparty/FlexLib" />  
  13.     <!---->  
  14.     <property name="mainApp" value="" />  
  15.     <!---->  
  16.     <property name="package" value="" />  
  17.        
  18.        
  19.     <!---->  
  20.     <target name="compile" depends="cleanCompile">  
  21.         <mxmlc    
  22.             file="${APP_ROOT}/${package}${fileName}.${fileExt}"    
  23.             output="${DEPLOY_DIR}/${package}${fileName}.swf"  
  24.             actionscript-file-encoding="UTF-8"  
  25.             keep-generated-actionscript="false"  
  26.             warnings="false"  
  27.             incremental="true"  
  28.             >  
  29.                
  30.             <!---->  
  31.             <compiler.source-path path-element="../srcFX"/>  
  32.             <compiler.source-path path-element="../../srcFX"/>  
  33.                
  34.             <!---->  
  35.             <load-config filename="${FLEX_HOME}/frameworks/flex-config.xml"/>               
  36.             <!---->  
  37.             <source-path path-element="${FLEX_HOME}/frameworks"/>  
  38.             <!---->  
  39.             <compiler.library-path dir="${FLEX_HOME}/frameworks" append="true">  
  40.                 <include name="libs" />  
  41.                 <include name="../bundles/{locale}" />  
  42.             compiler.library-path>  
  43.                
  44.             <!---->  
  45.             <compiler.include-libraries dir="${THIRD_PARTY}" append="true">  
  46.                 <include name="DistortionEffects.swc" />  
  47.             compiler.include-libraries>                                      
  48.         mxmlc>  
  49.         <delete>  
  50.             <!---->  
  51.             <fileset dir="${APP_ROOT}/${package}" includes="${fileName}*.cache" defaultexcludes="false"/>  
  52.         delete>  
  53.     target>  
  54.     <!---->  
  55.     <target name="CompileAndGenerateLinkReport">  
  56.         <mxmlc    
  57.             file="${APP_ROOT}/${package}${fileName}.${fileExt}"    
  58.             link-report="${APP_ROOT}/${package}${fileName}_LinkReport.xml"  
  59.             output="${DEPLOY_DIR}/${package}${fileName}.swf"  
  60.             actionscript-file-encoding="UTF-8"  
  61.             keep-generated-actionscript="false"  
  62.             incremental="true"  
  63.             >  
  64.             <!---->  
  65.             <load-config filename="${FLEX_HOME}/frameworks/flex-config.xml"/>               
  66.             <!---->  
  67.             <source-path path-element="${FLEX_HOME}/frameworks"/>  
  68.             <source-path path-element="D:/study/helloworld/srcFX"/>  
  69.             <!---->  
  70.             <compiler.library-path dir="${FLEX_HOME}/frameworks" append="true">  
  71.                 <include name="libs" />  
  72.                 <include name="../bundles/{locale}" />  
  73.             compiler.library-path>  
  74.             <!---->  
  75.             <default-size width="500" height="600" />  
  76.         mxmlc>  
  77.         <delete>  
  78.             <!---->  
  79.             <fileset dir="${APP_ROOT}/${package}" includes="${fileName}*.cache" defaultexcludes="false"/>  
  80.         delete>  
  81.     target>  
  82.     <!---->  
  83.     <target name="CompileModuleWithLinkReport">  
  84.             <mxmlc    
  85.                 file="${APP_ROOT}/${package}${fileName}.${fileExt}"    
  86.                 load-externs="${APP_ROOT}/${mainApp}_LinkReport.xml"  
  87.                 output="${DEPLOY_DIR}/${package}${fileName}.swf"  
  88.                 actionscript-file-encoding="UTF-8"  
  89.                 keep-generated-actionscript="false"  
  90.                 incremental="true"  
  91.                 >  
  92.                 <!---->  
  93.                 <load-config filename="${FLEX_HOME}/frameworks/flex-config.xml"/>               
  94.                 <!---->  
  95.                 <source-path path-element="${FLEX_HOME}/frameworks"/>  
  96.                 <!---->  
  97.                 <compiler.library-path dir="${FLEX_HOME}/frameworks" append="true">  
  98.                     <include name="libs" />  
  99.                     <include name="../bundles/{locale}" />  
  100.                 compiler.library-path>  
  101.                 <!---->  
  102.                 <default-size width="500" height="600" />  
  103.             mxmlc>  
  104.         <delete>  
  105.             <!---->  
  106.             <fileset dir="${APP_ROOT}/${package}" includes="${fileName}*.cache" defaultexcludes="false"/>  
  107.         delete>  
  108.         target>  
  109.     <!---->  
  110.     <target name="wrapper" depends="cleanWrapper">  
  111.         <html-wrapper    
  112.             title="Flex Ant Builder Sample"  
  113.             width="100%"  
  114.             height="100%"  
  115.             application="flexApp"  
  116.             swf="${fileName}"  
  117.             version-major="9"  
  118.             version-minor="0"  
  119.             version-revision="0"  
  120.             history="true"                 
  121.             template="express-installation"  
  122.             output="${DEPLOY_DIR}/${package}"/>  
  123.         <move file="${DEPLOY_DIR}/${package}index.html" tofile="${DEPLOY_DIR}/${fileName}.html"/>  
  124.     target>  
  125.     <!---->  
  126.     <target name="cleanCompile">  
  127.         <delete dir="${APP_ROOT}/${package}generated"/>  
  128.         <delete>  
  129.             <fileset dir="${DEPLOY_DIR}/${package}" includes="${fileName}*.swf"/>  
  130.         delete>  
  131.     target>  
  132.     <!---->  
  133.     <target name="cleanWrapper">  
  134.         <delete>  
  135.             <!---->  
  136.             <fileset dir="${DEPLOY_DIR}/${package}" includes="history.swf" defaultexcludes="false"/>  
  137.             <!---->  
  138.             <fileset dir="${DEPLOY_DIR}/${package}" includes="playerProductInstall.swf" defaultexcludes="false"/>  
  139.             <!---->  
  140.             <fileset dir="${DEPLOY_DIR}/${package}" includes="${fileName}*.html" defaultexcludes="false"/>  
  141.             <!---->  
  142.             <fileset dir="${DEPLOY_DIR}/${package}" includes="$history.htm" defaultexcludes="false"/>  
  143.             <!---->  
  144.             <fileset dir="${DEPLOY_DIR}/${package}" includes="*.js" defaultexcludes="false"/>                
  145.         delete>       
  146.     target>           
  147. 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>

xml 代码
  1. <!-- ***** source file path *******-->  
  2. <compiler.source-path path-element="../srcFX"/>  
  3. <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>

xml 代码
  1. <!-- third party swc-->  
  2. <property name="THIRD_PARTY" value="D:/work/thirdparty/FlexLib" />  


 这样在编译时就可以引用这个目录下的swc文件了,如下:

xml 代码
  1. <!-- ***** thirdparth swc ***** -->  
  2. <compiler.include-libraries dir="${THIRD_PARTY}" append="true">  
  3.     <include name="DistortionEffects.swc" />  
  4. </compiler.include-libraries>     


(8)warnings="false"不显示编译时出现的warning提示

最后,到上面所建的ant目录下,在命令行下执行ant -compile就可以编译工程文件了(或者建个批处理文件来执行)。

你可能感兴趣的:(xml,ant,cache,Flex,actionscript)