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文件,内容如下
<project name="flexmxmlascompiler" default="all" basedir=".">
<property file="${basedir}/build.properties"/>
<property name="flex.dist.lib" value="${flex.lib}" />
<property name="app.dir" value="." />
<property name="config.xml" value="${app.dir}/web-inf/flex/flex-config.xml" />
<property name="src.dir" value="${app.dir}/src" />
<property name="dest.dir" value="${app.dir}/bin" />
<property name="library.dest.dir" value="${dest.dir}/libs" />
<property name="mxmlfile" value="${mxmlfilepath}"/>
<property name="f" value="${mxmlfile}"/>
<!-- include all needed .jar files -->
<path id="lib.class.path">
<fileset dir="${flex.dist.lib}">
<include name="*.jar"/>
</fileset>
</path>
<!-- init -->
<target name="init">
<mkdir dir="${dest.dir}" />
<mkdir dir="${library.dest.dir}" />
</target>
<!-- compile .mxml to .swf -->
<macrodef name="makemxml2swf">
<attribute name="mxmlfilepath"/>
<sequential>
<!-- compile -->
<java classname="flex.tools.mxmlc" dir="${app.dir}" fork="true" failonerror="true" classpathref="lib.class.path">
<arg line="-flexlib '${flex.dist.lib}' -gatewayurl ${webserver}/amfgateway -configuration ${config.xml} -webroot . -o @{mxmlfilepath}.swf @{mxmlfilepath}.mxml" />
</java>
</sequential>
</macrodef>
<!-- run .swf by internet explorer -->
<macrodef name="runswfbyie">
<sequential>
<exec executable="${webbrowser}" spawn="true">
</exec>
</sequential>
<macrodef name="runswfbyflash">
<exec executable="${flashplayer}" spawn="true">
<arg value="${projectpath}/${f}.swf"/>
<!-- build .mxml to .swf and access by internet explorer -->
<target name="mxml2swf" depends="init">
<makemxml2swf mxmlfilepath="${f}"/>
<runswfbyie mxmlfilepath="${f}"/>
<target name="runflash" depends="init">
<runswfbyflash mxmlfilepath="${app.dir}/${f}"/>
<!-- delete swf file -->
<target name="clean" description="clean up">
<delete file="${app.dir}/${f}.swf"/>
<!-- delete the ${build} and ${dist} directory trees -->
<target name="cleanall" description="clean up">
<delete dir="${dest.dir}"/>
<target name="all" depends="mxml2swf">
</project>
3、%your_project%目录下建立01module/user.mxml文件进行测试之
集成到gel、eclipse、idea、jbuilder等等ide工具里就不用详细说了吧:)
四、结果测试
1、通过以上的构建,在程序中以remoteobject与服务器进行通讯的方式编译运行没有问题,当然要设置好remoteobject的配置;
懒人就是设置*了:)
flex-config.xml
<remote-objects>
<whitelist>
<unnamed>
<source>*</source>
2、httpservice访问形式编译运行也成功,但是有相对路径问题,浏览器里可以用相对路径,如果想用flashplayer进行测试,那就得在mxml里写死绝对的url了:(,可以通过as设置一个全局变量来设置网址,其它文件引用使用之即可用flashplayer来进行测试了。
注意在mxml里加上<mx:httpservice useproxy="false"
访问xml数据统一以utf-8格式进行返回,也没有出现中文乱码问题
设置如下
<http-service-proxy>
<unnamed>
<url>http://{localserver}/*</url>
<url>https://{localserver}/*</url>
3、web services访问形式编译运行也成功
同样也要注意在mxml里加上<mx:httpservice useproxy="false"
并且用wsdl代替servicename进行访问,要不然不能编译通过的。注意哦。
<web-service-proxy>
btw:
1、编译后的.swf没有.mxml有title:),只能自己去建立一个html包一包了。
今后正式部署就把html和swf往生产的机器上面部署即可了。
2、今后用flex 2.0后,再对2.0进行使用。