Ant and Flex 用Ant編譯MXML檔案 - 螞蟻咬斷鬆緊帶(^_^)

YuLimin發表於2006-04-24
Ant and Flex 用Ant編譯MXML檔案 - 螞蟻咬斷鬆緊帶(^_^)

http://iamin.blogdriver.com/iamin/1173623.html

以下的描述均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">
<attribute name="mxmlfilepath"/>
<sequential>
<exec executable="${webbrowser}" spawn="true">
<arg value="${webserver}/@{mxmlfilepath}.swf"/>
</exec>
</sequential>
</macrodef>

<macrodef name="runswfbyflash">
<attribute name="mxmlfilepath"/>
<sequential>
<exec executable="${flashplayer}" spawn="true">
<arg value="${projectpath}/${f}.swf"/>
</exec>
</sequential>
</macrodef>

<!-- Build .mxml to .swf and Access By Internet Explorer -->
<target name="mxml2swf" depends="init">
<makemxml2swf mxmlfilepath="${app.dir}/${f}"/>
<runswfbyie mxmlfilepath="${app.dir}/${f}"/>
</target>

<target name="runflash" depends="init">
<makemxml2swf mxmlfilepath="${app.dir}/${f}"/>
<runswfbyflash mxmlfilepath="${app.dir}/${f}"/>
</target>

<!-- Delete swf File -->
<target name="clean" description="clean up">
<delete file="${app.dir}/${f}.swf"/>
</target>
<!-- Delete the ${build} and ${dist} directory trees -->
<target name="cleanAll" description="clean up">
<delete dir="${dest.dir}"/>
</target>

<target name="all" depends="mxml2swf">
</target>
</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格式進行返回,也沒有出現中文亂碼問題
設定如下
flex-config.xml
<http-service-proxy>
<whitelist>
<unnamed>
<url>http://{localserver}/*</url>
<url>https://{localserver}/*</url>

3、Web Services訪問形式編譯執行也成功
同樣也要注意在mxml里加上<mx:HTTPService useProxy="false"
並且用wsdl代替serviceName進行訪問,要不然不能編譯透過的。注意哦。
設定如下
flex-config.xml
<web-service-proxy>
<whitelist>
<unnamed>
<url>http://{localserver}/*</url>
<url>https://{localserver}/*</url>

BTW:
1、編譯後的.swf沒有.mxml有Title:),只能自己去建立一個html包一包了。
今後正式部署就把html和swf往生產的機器上面部署即可了。

2、今後用Flex 2.0後,再對2.0進行使用。

相關文章