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進行使用。