分享

Flex compc & ant 编译

 地域-幽灵 2011-05-25

当我们的类库在Flex Builder中可以编译通过时,那自动化编译(ant)主要是根据在Flex Builder中设置的参数一致,基本就可以采用compc命令编译出一个swc文件。在编译过程中可能会遇到各种问题,不过不要烦躁,找到每个问题的原因,自然就找到了解决办法,下面总结一下在做Flex自动化编译过程中遇到的问题,希望可以帮助到大家。

在做ant编译之前,先看一下工程的具体信息,右键工程—属性,切换到Flex Library Build Path中,我们可以看到四个选项卡,Classes\Assers\Source Path\Library Path,那也就是说明我们用编写脚本的时候只需要指定这四项就可以了,另外,在Flex Library Compiler中可以看到是en_US\zh_CN,证明要做中英文资源化,因此我们自动化编译的时候需要将资源化文件引入,具体国际化会在后面介绍。

1.    Flex Embed资源错误Unable to transcode asset处理

<mx:Image id="img_border" source="@Embed('../assets/border.png')" visible="false" width="100%" height="100%"/>

一段简单的代码flex编译居然报错:

Unable to transcode ../assets/border.png.

解决方法很简单,

将 '../assets/border.png' 改为 '/../assets/border.png'

就是在前面加个斜杠。

2.    Flex国际化

首先先介绍下国际化:

Flex国际化一般采用的是类似struts的bundle类似的方法,至于好处嘛会使用STRUTS的人都应该知道。
直接在项目中写.properties文件,具体做法如下:
在项目上点击右键,选择Properties。
然后选择Flex Compiler,在Additional Compiler arguments下面已经配置好了语言包,

默认为-locale en_US。
这时我们可以用一个locale目录来简单定制我们额外设置(当然不包括Flex内部控件的语言)的语言设置。
比如改为:-locale=en_US -source-path+=g:\flexproj\locale\{locale}。这样在g盘的flexproj目录下建立一个locale目录。
然后目录下放置包含我们要扩展的语言文件的文件夹就可以了。比如:g:\flexproj\locale\en_US。
注意:locale下面的目录名应该和-locale=设置的名称一致。
那么这样,我们就可以使用额外的语言设置了。

举个例子吧:
查看项目属性里Additional Compiler arguments配置为
-locale+=en_US -source-path+=g:\flexproj\testgoufang\locale\{locale}
然后在对应的locale目录下添加国际化资源文件,
g:\flexproj\testgoufang\locale\en_US\strings.properties
这一部分跟struts相似。
里面的内容为键=值的形式,如:
Title=测试项目
User=用户名
Password=密码
…………
…………
使用的时候可以通过[ResourceBundle]元数据标签来绑定locale文件,如:
<mx:Metadata>
                              [ResourceBundle("strings")]
</mx:Metadata>
即绑定上文提到的strings.properties文件,然后我们可以通过ResoueceManager来读出其中的内容,比如:
var Title : String = resourceManager.getString("strings", 'Title');
或者绑定到控件:
[Bindable]
private var Title:String;
…………
Title = resourceManager.getString("strings", 'Title');
…………
<mx:Label text="{Title}"/>
如此多个项目共享统一资源

如果编译的时候没有引用资源文件,则会报相应的资源文件未找到。

在build.xml中添加资源文件即可

<source-path path-element="${project.dir}/locale/en_US"/>

<source-path path-element="${project.dir}/locale/zh_CN"/>

3.    编译后的mx文件夹下所有图片大小为0字节

Mx中包括controls和container文件夹,主要包括用到的Flex控件的图片,因此需要在source-path中指定图片的位置,否则找不到图片,大小就为0喽;

加入以下脚本即可:

<source-path path-element="${projects.framework}/src"/>

大概目录位置是E:\Program Files\Adobe\Flex Builder 3\sdks\3.2.0\frameworks\projects\framework\src 里面包括一个mx文件夹,是Flex用到的所有资源图片的存在位置。

4. 工程目录中包括mxml时编译不到swc中

因为我们编译的时候首先是读取src文件夹中的所有类,然后指定compc参数的include-classes参数,这时只是指定的所有as文件,而mxml是不属于某个包里面的,因此这里需要使用include-sources参数来指定。

具体代码如下:

    <include-sources dir = "${project.dir}\src\com\supermap\web\controls" includes = "Compass.mxml" />

Build.xml文件内容如下所示:

<project name="Web.swc" basedir="." default="main" >

<taskdef resource="flexTasks.tasks" classpath="E:\Flex\flexTasks\lib\flexTasks.jar" />

<property name="FLEX_HOME" value="E:\Progra~1\Adobe\FlexBu~1\sdks\3.2.0" />  

<property name="project.dir" value ="E:\FlexProject\FlexClient60\FlexClientLib" />

<property name="output.file" value="Web.swc" />

<property name="projects.framework" value="E:\Program Files\Adobe\Flex Builder 3\sdks\3.2.0\frameworks\projects\framework"/>

<target name="main" depends="clean, log, compc" />

<!-- deletes and recreates the compc directory -->

<target name="clean">  

         <delete dir="${project.dir}\compile" failonerror="true"/>  

         <mkdir dir="${project.dir}\compile"/>

         <echo>${Root}\bin</echo>

</target>

<!-- runs the compc.exe compiler on the source -->

<target name="compc">  

         <echo>${Root}/src</echo>

                   <fileset dir="${project.dir}\src" id="src.files">    

                            <include name="**\**"/>    

                    </fileset>   
     
                   <!—遍历src文件夹下的所有as文件-->

                   <pathconvert    

                     property="evaFramework_classes"    

                     pathsep=" "    

                     dirsep="."    

                     refid="src.files"    

                   >   
                           <map from="\" to="/"/>    

                            <map from="${project.dir}\src\" to=""/>    

                            <mapper>    

                                  <chainedmapper>    

                                        <globmapper from="*.as" to="*"/>    

                                   </chainedmapper>    

                            </mapper>    

                    </pathconvert>                  

                   <!--输出所有类-->

                   <echo>${evaFramework_classes}</echo>

                   <!--开始编译类库文件-->

                   <compc output="${project.dir}\compile\${output.file}"    

                             locale="en_US,zh_CN"   

                             include-classes="${evaFramework_classes}"    

                             optimize="true"

                             benchmark="true"

                            strict = "true"

                            debug="true"

                            as3="true"

                            actionscript-file-encoding = "utf-8"

                            allow-source-path-overlap = "true"

                            use-resource-bundle-metadata = "true"

                 >                            
                            <!--编译源文件-->

                            <source-path path-element="${project.dir}/src" />

<!--如果类库做了国际化,那么需要引入国际化资源文件,也就是工程目录\locale\下面的所有资源文件(类型为.properties)-->

                            <source-path path-element="${project.dir}/locale/en_US"/>

                            <source-path path-element="${project.dir}/locale/zh_CN"/>
                          
<!—需要指定引用图片的资源文件,否则在编译好的mx文件夹下的所有图片都是0字节-->

                       <source-path path-element="${projects.framework}/src"/>

                             <!-- List of SWC files or directories that contain SWC files. -->  

                             <!—注意这里可以指定类库文件的目录啊,呵呵-->

                             <compiler.include-libraries dir="${FLEX_HOME}" append="true">

                                     <include name="/frameworks"/>

                             </compiler.include-libraries>

            <compiler.include-libraries dir="${project.dir}" append="true">   

                <include name="/lib" />   

            </compiler.include-libraries>   

        </compc>   

</target>  

<!-- writes compc output to log file: compc-log.log -->

<target name="log">  

         <record name="${project.dir}\compile\compc-log.log" action="start" append="true" />

</target>

</project>


借鉴资源:

http://livedocs.adobe.com/flex/3/html/help.html?content=anttasks_1.html

http://ericyou./blog/298336

http://www./showdiary.jsp?id=219

http:///questions/1400458/why-are-some-of-my-assets-0-byte-in-size-when-i-build-a-component-using-compc

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多