在Eclipse下使用Ant编译Flex程序
我的机器内存1G,如果使用Eclipse Flex插件编译程序,用不了多久系统内存就会耗尽,机器会变得极其缓慢。使用Flex Builder 2.01似乎情况要好一点,但是觉得Eclipse似乎更加好用一点,比如Ctrl + Alt + LEFT可以将一块代码向左移……Flex Builder好像没有类似的快捷键,虽然有类似的命令。另外还要进行ColdFusion,js开发,如果总是将IDE换来换去多麻烦啊。
在网上浏览时看到使用ant来编译可以提高速度、减少内存占有率。当然也可以直接使用命令行mxmlc等命令来编译,但是不如使用ant方便——ant其实也是使用mxmlc命令来编译。
Adobe labs提供了ant tasks工具包并有一些使用方法,是英文并且不怎么详细。有几个中文Blog文章有写相关,但是大多是针对旧版本的Flex SDK,我现在使用的SDK是2.0正式版,较之那些教程上的版本新,导致那些教程上的例子不能执行。
以Eclipse3.2.1为例,一步步来吧。
1. 从Adobe labs上下载最新的ant tasks工具包,地址:http://labs.adobe.com/wiki/index.php/Flex_Ant_Tasks。
2. 解压缩。
3. 将flexTasks.jar拷贝到eclipse的ant lib根目录下面,ant lib目录一般在eclipse根目录下,如\plugins\org.apache.ant_ant版本号\lib。
4. 在项目根目录中新建flexTasks文件,拷入ant tasks中的几个文件加:lib和resources。
5. 在项目根目录中建立build.xml。build.xml文件内容可参考:http://nstar.iteye.com/blog/129653, 这里也有一个示例,ant的命令可以到 ant的官方网站去查,今天在我的火狐2.0中地址栏输入ant move,居然FF自动进入了ant官方网站并且显示task move
是不是FF的开发人员也用ant,为了方便查找而设置的,因为输入其他关键字一般进入的Google:
<?xml version="1.0" encoding="utf-8"?>
<project name="Flex Ant Builder Sample Project" basedir=".">
<taskdef resource="flexTasks.tasks" classpath="${basedir}/flexTasks/lib/flexTasks.jar" />
<property name="FLEX_HOME" value="D:/eclipse/Adobe/Flex Builder 2 Plug-in/Flex SDK 2"/>
<property name="APP_ROOT" value="${basedir}"/>
<property name="DEPLOY_DIR" value="bin"/>
<!-- compiled mxml/as file Name -->
<property name="fileName" value="ActorLines" />
<!-- compiled mxml/as file ext -->
<property name="fileExt" value="mxml" />
<!-- third party swc-->
<!--<property name="THIRD_PARTY" value="D:/work/thirdparty/FlexLib" />-->
<!-- modular application's main application fileName -->
<property name="mainApp" value="" />
<!-- output package direction,end with / -->
<property name="package" value="" />
<property name="FLASHPLAYER" value="D:/eclipse/Adobe/Flex Builder 2 Plug-in/Player/debug/SAFlashPlayer.exe" />
<property name="IE" value="C:\Program Files\Internet Explorer\iexplore.exe" />
<!-- compile a mxml/as file -->
<target name="compile" depends="cleanCompile">
<mxmlc
file="${APP_ROOT}/${package}${fileName}.${fileExt}"
output="${DEPLOY_DIR}/${package}${fileName}.swf"
actionscript-file-encoding="UTF-8"
keep-generated-actionscript="false"
warnings="false"
incremental="true"
>
<!-- ***** source file path *******-->
<compiler.source-path path-element="${basedir}"/>
<!-- Get default compiler options. -->
<load-config filename="${FLEX_HOME}/frameworks/flex-config.xml"/>
<!-- List of path elements that form the roots of ActionScript class hierarchies. -->
<source-path path-element="${FLEX_HOME}/frameworks"/>
<!-- List of SWC files or directories that contain SWC files. -->
<compiler.library-path dir="${FLEX_HOME}/frameworks" append="true">
<include name="libs" />
<include name="../bundles/{locale}" />
</compiler.library-path>
<!-- ***** thirdparth swc ***** -->
<!--<compiler.include-libraries dir="${THIRD_PARTY}" append="true">
<include name="DistortionEffects.swc" />
</compiler.include-libraries> -->
<!-- Set size of output SWF file.
<default-size width="500" height="600" />
-->
</mxmlc>
<delete>
<!-- Deletes cache file -->
<fileset dir="${APP_ROOT}/${package}" includes="${fileName}*.cache" defaultexcludes="false"/>
</delete>
</target>
<!-- compile mxml file and Generate a Link Report for the embed module -->
<target name="CompileAndGenerateLinkReport">
<mxmlc
file="${APP_ROOT}/${package}${fileName}.${fileExt}"
link-report="${APP_ROOT}/${package}${fileName}_LinkReport.xml"
output="${DEPLOY_DIR}/${package}${fileName}.swf"
actionscript-file-encoding="UTF-8"
keep-generated-actionscript="false"
incremental="true"
>
<!-- Get default compiler options. -->
<load-config filename="${FLEX_HOME}/frameworks/flex-config.xml"/>
<!-- List of path elements that form the roots of ActionScript class hierarchies. -->
<source-path path-element="${FLEX_HOME}/frameworks"/>
<source-path path-element="${APP_ROOT}"/>
<!-- List of SWC files or directories that contain SWC files. -->
<compiler.library-path dir="${FLEX_HOME}/frameworks" append="true">
<include name="libs" />
<include name="../bundles/{locale}" />
</compiler.library-path>
<!-- Set size of output SWF file. -->
<default-size width="500" height="600" />
</mxmlc>
<delete>
<!-- Deletes cache file -->
<fileset dir="${APP_ROOT}/${package}" includes="${fileName}*.cache" defaultexcludes="false"/>
</delete>
</target>
<!-- compile Modular mxml file with mainApp's Link Report -->
<target name="CompileModuleWithLinkReport">
<mxmlc
file="${APP_ROOT}/${package}${fileName}.${fileExt}"
load-externs="${APP_ROOT}/${mainApp}_LinkReport.xml"
output="${DEPLOY_DIR}/${package}${fileName}.swf"
actionscript-file-encoding="UTF-8"
keep-generated-actionscript="false"
incremental="true"
>
<!-- Get default compiler options. -->
<load-config filename="${FLEX_HOME}/frameworks/flex-config.xml"/>
<!-- List of path elements that form the roots of ActionScript class hierarchies. -->
<source-path path-element="${FLEX_HOME}/frameworks"/>
<!-- List of SWC files or directories that contain SWC files. -->
<compiler.library-path dir="${FLEX_HOME}/frameworks" append="true">
<include name="libs" />
<include name="../bundles/{locale}" />
</compiler.library-path>
<!-- Set size of output SWF file. -->
<default-size width="500" height="600" />
</mxmlc>
<delete>
<!-- Deletes cache file -->
<fileset dir="${APP_ROOT}/${package}" includes="${fileName}*.cache" defaultexcludes="false"/>
</delete>
</target>
<!-- wrapper a swf with html express-installation template-->
<target name="wrapper" depends="cleanWrapper">
<html-wrapper
title="Actor's lines in web"
width="100%"
height="100%"
application="flexApp"
swf="${fileName}"
version-major="9"
version-minor="0"
version-revision="0"
history="true"
template="express-installation"
output="${DEPLOY_DIR}/${package}"/>
<move file="${DEPLOY_DIR}/${package}index.html" tofile="${DEPLOY_DIR}/${fileName}.html"/>
<copy todir="${DEPLOY_DIR}/assets">
<fileset dir="./assets" />
</copy>
</target>
<!-- clean preview previous compile file -->
<target name="cleanCompile">
<delete dir="${APP_ROOT}/${package}generated"/>
<delete>
<fileset dir="${DEPLOY_DIR}/${package}" includes="${fileName}*.swf"/>
</delete>
</target>
<!-- clean preview previous wrapper file -->
<target name="cleanWrapper">
<delete>
<!-- Deletes history.swf -->
<fileset dir="${DEPLOY_DIR}/${package}" includes="history.swf" defaultexcludes="false"/>
<!-- Deletes playerProductInstall.swf -->
<fileset dir="${DEPLOY_DIR}/${package}" includes="playerProductInstall.swf" defaultexcludes="false"/>
<!-- Deletes ${fileName}.html -->
<fileset dir="${DEPLOY_DIR}/${package}" includes="${fileName}*.html" defaultexcludes="false"/>
<!-- Deletes history.htm -->
<fileset dir="${DEPLOY_DIR}/${package}" includes="$history.htm" defaultexcludes="false"/>
<!-- Deletes history.js and AC_OETags.js -->
<fileset dir="${DEPLOY_DIR}/${package}" includes="*.js" defaultexcludes="false"/>
<!-- Deletes all assets -->
<fileset dir="${DEPLOY_DIR}/assets" includes="*.*"/>
</delete>
</target>
<target name="run" depends="compile" description="Compile and run it.">
<exec executable="${FLASHPLAYER}">
<arg value="${DEPLOY_DIR}/${package}${fileName}.swf" />
</exec>
</target>
<target name="runie" depends="compile" description="Compile and run it.">
<exec executable="${IE}">
<arg value="${basedir}\${DEPLOY_DIR}\${package}${fileName}.html" />
</exec>
</target>
</project>
弄完build.xml之后,基本上就完成ant的设置,现在可以享受ant带来的速度与便捷了。
如何使用ant运行任务
1. 在命令行里面输入ant targetname;
2. 建立cmd文件,里面输入ant targetname,以后就只要双击这个cmd文件。
3. 在eclipse中选择build.xml,然后右键选择Run as -> ant bulid,在弹出的对话框中选择Targets,然后选择一个Target,在Build中去掉Build before lunch前面的勾,现在可以Run啦。在Eclipse的控制台里面可以看到信息显示。
转自:http://www.cnblogs.com/fzhenmei/archive/2007/12/07/986733.html