天天看点

Ant and Flex 用Ant编译MXML文件 - 蚂蚁咬断松紧带(^_^)

ant and flex 用ant编译mxml文件 - 蚂蚁咬断松紧带(^_^)

<a href="http://iamin.blogdriver.com/iamin/1173623.html">http://iamin.blogdriver.com/iamin/1173623.html</a>

以下的描述均flex 1.5为例

一、flex server

1、安装后,我们可以在%flex_home%目录下看到三个.war文件flex.war profiler.war samples.war不用多介绍了吧。

2、%flex_home%/bin目录下看到许多文件,以下几个执行文件是我们关注的

compc、mxmlc:都是 macromedia flex builder ,两个文件是一模一样的作用,连文件内容也是一样的,不信自己去beyond compare一下

可以运行 compc -version 或 mxmlc -version 得到它们的版本号信息 macromedia flex build: 87315.134646

使用方法如下:当然我们可以在ant里面调用执行文件来进行编译,但是不是很爽啦:(

usage: compc [-version] [-configuration path] [-flexlib path] [-libpath path] [-systemclasses path] [-g] [-o0] [-profile] [-o path] [-headless] [-contextroot root] [-proxyurl url] [-proxyhttpsurl url] [-proxyallowurloverride] [-gatewayurl url] [-gatewayhttpsurl url] [-remoteallowurloverride] [-webroot directory] [-aspath path] [-genlibdir dir] [-encoding file_encoding] [-namespace uri manifestfile] [-report] [-loglevel error|warn|info|debug] [-manifest manifest] [-root root] foo.mxml

fdb:是调试工具,没有用过,直接运行它,可以看到信息 macromedia fdb (flash player debugger) [build 87315.134646]

输入help可以看到所有的操作命令与功能简介,quit退出,看自带的文档进行详细查阅,没有研究过它

licensetool:license工具

usage: license [-install licensekey] [-info] flex-war-filename|flex-war-directory

3、%flex_home%/lib目录下,赫赫,这个应当是我们更加想关注的哦。

里面有 compc.jar mxmlc.jar fdb.jar licensetool.jar 和 flex-tools.jar 五个.jar文件,不过前四个都只有1k,咋一看,有点晕,肯定是啥也没有的东东,何用?不过。。。

打开一看,里面均是只有meta-inf/manifest.mf一个文件

细看mxmlc.jar有main-class: flex.tools.mxmlc

../classes/ flex-tools.jar flex-bootstrap.jar xercespatch.jar oscache.jar xercesimpl.jar xmlparserapis.jar jakarta-oro-2.0.7.jar batik-awt-util.jar batik-bridge.jar batik-css.jar batik-dom.jar batik-ext.jar batik-gvt.jar batik-parser.jar batik-script.jar batik-svg-dom.jar batik-util.jar batik-transcoder.jar batik-xml.jar axis.jar commons-discovery.jar commons-logging.jar jaxrpc.jar saaj.jar wsdl4j.jar concurrent.jar commons-collections.jar

看到这里应当说就有点儿豁然开朗了吧。

继续

../classes/这个在%flex_home%下没有发现

flex-tools.jar在这个目录下有

其它的也都没有。。。不过,应当一眼扫一下就知道在哪里有了:)

ok,在%flex_home%/flex.war里/web-inf/lib/目录下可以发现flex-bootstrap.jar、flexgateway.jar、commons-collections.jar、commons-beanutils.jar

在%flex_home%/flex.war里/web-inf/flex/jars/目录下可以发现其它的jar包了。

这样基本上所需的枪炮都找到了。要开工了。

二、准备ant编译所需的文件

1、基本工作,解压%flex_home%/flex.war到你的项目工作目录%your_project%下面,或者就扔到%tomcat_home%/webapps/下面,让tomcat帮你解压:)

2、把%flex_home%/flex-tools.jar拷贝到%your_project%/web-inf/flex/jars/下面

3、把%your_project%/web-inf/flex/license.properties拷贝到%your_project%/web-inf/flex/jars/下面

要不然会报告

license service: unable to open license file - ./web-inf/flex/jars/license.properties (系统找不到指定的文件。)

warning license service: the flex 1.5 trial edition evaluation period will expire in 55 days.

license service: flex 1.5 trial edition enabled

warning: applications compiled into standalone swfs using the trial and developer editions of macromedia flex expire 1 day after creation.  this restriction is only in place for the trial and developer editions of macromedia flex.

三、建立ant编译文件和任务

在%your_project%下建立build.properties和build.xml文件

1、build.properties文件,内容如下

#flex libs path

flex.lib=./web-inf/flex/jars

#internet explorer path

webbrowser=d:/program files/internet explorer/iexplore.exe

#flashplayer

flashplayer=e:/program files/macromedia/flex/bin/saflashplayer.exe

#webserver url

webserver=http://localhost:9080/flex

#project path

projectpath=f:/opensource/eclipse/workspace/flex

#.mxml file path without .mxml suffix

mxmlfilepath=01module/user

说明webbrowser、flashplayer、projectpath为绝对路径,请进行相应的修改

重要的!!!webserver为你的web应用访问地址,这个是用来进行开发remotingobject时要设置gatewayurl的,如-gatewayurl ${webserver}/amfgateway

mxmlfilepath为你要编译的.mxml文件路径,不要带后缀名

编译时只要ant -df=yourpath/yourmxml就是编译你的yourpath/yourmxml.mxml了,编译成功后就会用浏览器打开它;

想用flashplayer打开,运行ant runflash -df=yourpath/yourmxml就是编译你的yourpath/yourmxml.mxml并用flashplayer打开。

2、build.xml文件,内容如下

&lt;project name="flexmxmlascompiler" default="all" basedir="."&gt;

 &lt;property file="${basedir}/build.properties"/&gt;

 &lt;property name="flex.dist.lib" value="${flex.lib}" /&gt;

 &lt;property name="app.dir" value="." /&gt;

 &lt;property name="config.xml" value="${app.dir}/web-inf/flex/flex-config.xml" /&gt;

 &lt;property name="src.dir" value="${app.dir}/src" /&gt;

 &lt;property name="dest.dir" value="${app.dir}/bin" /&gt;

 &lt;property name="library.dest.dir" value="${dest.dir}/libs" /&gt;

 &lt;property name="mxmlfile" value="${mxmlfilepath}"/&gt;

 &lt;property name="f" value="${mxmlfile}"/&gt;

 &lt;!-- include all needed .jar files --&gt;

 &lt;path id="lib.class.path"&gt;

  &lt;fileset dir="${flex.dist.lib}"&gt;

   &lt;include name="*.jar"/&gt;

  &lt;/fileset&gt;

 &lt;/path&gt;

 &lt;!-- init --&gt;

 &lt;target name="init"&gt;

  &lt;mkdir dir="${dest.dir}" /&gt;

  &lt;mkdir dir="${library.dest.dir}" /&gt;

 &lt;/target&gt;

 &lt;!-- compile .mxml to .swf --&gt;

 &lt;macrodef name="makemxml2swf"&gt;

     &lt;attribute name="mxmlfilepath"/&gt;

  &lt;sequential&gt;

   &lt;!-- compile --&gt;

   &lt;java classname="flex.tools.mxmlc" dir="${app.dir}" fork="true" failonerror="true" classpathref="lib.class.path"&gt;

    &lt;arg line="-flexlib '${flex.dist.lib}' -gatewayurl ${webserver}/amfgateway -configuration ${config.xml} -webroot . -o @{mxmlfilepath}.swf @{mxmlfilepath}.mxml" /&gt;

   &lt;/java&gt;

  &lt;/sequential&gt;

 &lt;/macrodef&gt;

 &lt;!-- run .swf by internet explorer --&gt;

 &lt;macrodef name="runswfbyie"&gt;

     &lt;sequential&gt;

     &lt;exec executable="${webbrowser}" spawn="true"&gt;

     &lt;/exec&gt;

    &lt;/sequential&gt;

 &lt;macrodef name="runswfbyflash"&gt;

     &lt;exec executable="${flashplayer}" spawn="true"&gt;

      &lt;arg value="${projectpath}/${f}.swf"/&gt;

 &lt;!-- build .mxml to .swf and access by internet explorer --&gt;

 &lt;target name="mxml2swf" depends="init"&gt;

  &lt;makemxml2swf mxmlfilepath="${f}"/&gt;

  &lt;runswfbyie mxmlfilepath="${f}"/&gt;

 &lt;target name="runflash" depends="init"&gt;

  &lt;runswfbyflash mxmlfilepath="${app.dir}/${f}"/&gt;

 &lt;!-- delete swf file --&gt;

 &lt;target name="clean" description="clean up"&gt;

  &lt;delete file="${app.dir}/${f}.swf"/&gt;

 &lt;!-- delete the ${build} and ${dist} directory trees --&gt;

 &lt;target name="cleanall" description="clean up"&gt;

  &lt;delete dir="${dest.dir}"/&gt;

 &lt;target name="all" depends="mxml2swf"&gt;

&lt;/project&gt;

3、%your_project%目录下建立01module/user.mxml文件进行测试之

集成到gel、eclipse、idea、jbuilder等等ide工具里就不用详细说了吧:)

四、结果测试

1、通过以上的构建,在程序中以remoteobject与服务器进行通讯的方式编译运行没有问题,当然要设置好remoteobject的配置;

懒人就是设置*了:)

flex-config.xml

&lt;remote-objects&gt;

 &lt;whitelist&gt;

  &lt;unnamed&gt;

  &lt;source&gt;*&lt;/source&gt;

2、httpservice访问形式编译运行也成功,但是有相对路径问题,浏览器里可以用相对路径,如果想用flashplayer进行测试,那就得在mxml里写死绝对的url了:(,可以通过as设置一个全局变量来设置网址,其它文件引用使用之即可用flashplayer来进行测试了。

注意在mxml里加上&lt;mx:httpservice useproxy="false"

访问xml数据统一以utf-8格式进行返回,也没有出现中文乱码问题

设置如下

&lt;http-service-proxy&gt;

  &lt;unnamed&gt;   

   &lt;url&gt;http://{localserver}/*&lt;/url&gt;

   &lt;url&gt;https://{localserver}/*&lt;/url&gt;

3、web services访问形式编译运行也成功

同样也要注意在mxml里加上&lt;mx:httpservice useproxy="false"

并且用wsdl代替servicename进行访问,要不然不能编译通过的。注意哦。

&lt;web-service-proxy&gt;

btw:

1、编译后的.swf没有.mxml有title:),只能自己去建立一个html包一包了。

今后正式部署就把html和swf往生产的机器上面部署即可了。

2、今后用flex 2.0后,再对2.0进行使用。

继续阅读