首先,要使用ant編譯flex項目必須滿足以下條件
1. java環境
2. apache ant
3. flex sdk
如果具備以上條件了,那我們開始吧~@~
flex sdk中帶的編譯器幾種:
如:
%flexsdk_home%version/asdoc 文档归档
%flexsdk_home%version/mxmlc mxml 编译器
%flexsdk_home%version/compc as类编译器
我们主要是利用mxmlc进行flex编译 , 还是看例子吧
build.xml
<?xml version="1.0"?>
<project name="ManangementSys" default="deploy">
<!--根据操作系统设置编译环境-->
<condition property="platformSpecificPropertyFile" value="build.win.properties">
<os family="windows" />
</condition>
<condition property="platformSpecificPropertyFile" value="build.unix.properties">
<os family="unix" />
</condition>
<!--导入变量配置-->
<property file="./setup/config/${platformSpecificPropertyFile}" />
<property file="./build.properties" />
<target name="properties">
<fail unless="asdoc.exe">The "asdoc.exe" property must be set in ${build.dir}/build.properties.</fail>
<fail unless="compc.exe">The "compc.exe" property must be set in ${build.dir}/build.properties.</fail>
<fail unless="mxmlc.exe">The "mxmlc.exe" property must be set in ${build.dir}/build.properties.</fail>
</target>
<!-- 这是一个ant扩展,使用它可以使用如"for" 和 "propertyregex"插件 -->
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="${setup.lib}/ant-contrib-1.0b3.jar" />
</classpath>
</taskdef>
<!-- Cleans project -->
<target name="clean">
<delete dir="${build}" />
<delete dir="${dist}" />
</target>
<!-- creates buid/dist folders -->
<target name="init" depends="clean">
<mkdir dir="${build}" />
<mkdir dir="${build.config}" />
<mkdir dir="${dist}" />
</target>
<!-- compile the project -->
<target name="complieProject" depends="init"><!--copy配置文件到构造目录中,以备使用-->
<copy tofile="${build.config}/flexlib-config.xml" file="${setup.config}/flexlib-config.template.xml" overwrite="true" />
<!--设置flex版本--->
<replace file="${build.config}/flexlib-config.xml">
<replacefilter token="@FLEX_LIBS_HOME@" value="${flexsdk.lib}" />
<replacefilter token="@FLEX_BUILD_VERSION@" value="${flex.version}" />
</replace>
<!--在此进行编译-->
<exec executable="${mxmlc.exe}" dir="${basedir}" logerror="on">
<arg line="'${src.dir}/${application.name}.mxml'" /><!--源码文件-->
<arg line="-o ${dist}/${application.name}.swf" /><!--编译结果输出文件-->
<arg line="-load-config '${flexsdk.dir}/frameworks/flex-config.xml'" /><!--加载配置,这是flex库-->
<arg line="-load-config ${build.config}/flexlib-config.xml" /><!--其它一些参数设置--->
</exec>
</target>
<!--资源copy -->
<target name="copyAssets" depends="init">
<copydir dest="${dist}/assets" src="${src.dir}/assets">
</copydir>
<copydir dest="${dist}/com/amusement" src="${src.dir}/com/amusement" excludes="**/*.mxml">
</copydir>
</target>
<!--copy html模板-->
<target name="copyWebRequired">
<copydir dest="${dist}" src="${setup.webrequired}" excludes="**/${html.template.file}">
</copydir>
<copyfile dest="${dist}/index.html" src="${setup.webrequired}/${html.template.file}" />
<replace file="${dist}/index.html">
<replacefilter token="@FLASH_SWF@" value="${application.name}" />
</replace>
</target>
<target name="test" depends="complieProject,copyAssets">
<!--测试-->
<exec executable="${flashDebugPlayer.exe}" spawn="yes">
<arg line="${dist}/${application.name}.swf" />
</exec>
</target>
</project>
build.unix.properties
deploy.webapp.home=/usr/local/tomcat/webapps/mg
# The location of the Flex SDK on your system.
flex.home = /opt/flex
flex.version = 4.0.0
# conditional compilation variables, whether you're building against flex 3 or 4
# note: both variables must be set, both are actually used in the code base. They
# obviously should be set to opposite values :)
flex.version3 = false
flex.version4 = true
# Name for the Flex compiler binaries. The path will be derived from the flex home + version.
asdoc.exe.name = asdoc
mxmlc.exe.name = mxmlc
compc.exe.name = compc
# path to flash debug player binary
flashDebugPlayer.exe = flashplayer
build.win.properties
deploy.webapp.home=D\:\\Program Files\\Apache Foundation\\tomcat-6.0.29\\webapps\\mg
# The location of the Flex SDK on your system.
# flex3--
# flex.home = C:/Program Files/Adobe/Flash Builder 4 Plug-in
# flex.version = 4.0.0
# flex 4
flex.home = D:/Program Files/Adobe/Adobe Flash Builder 4
flex.version = 4.0.0
# conditional compilation variables, whether you're building against flex 3 or 4
# note: both variables must be set, both are actually used in the code base. They
# obviously should be set to opposite values :)
flex.version3 = false
flex.version4 = true
# name for the Flex compiler binaries. The path will be derived from the flex home + version.
asdoc.exe.name = asdoc.exe
mxmlc.exe.name = mxmlc.exe
compc.exe.name = compc.exe
# path to flash debug player binary
# The debug player is necessary here because it writes trace statements to a flashlog.txt
# file. This allows us to examine the .txt file and determine the status of unit tests
# in an automated fashion.
flashDebugPlayer.exe = ${flex.home}/Player/win/FlashPlayer.exe
flexlib-config.template.xml
<?xml version="1.0"?>
<flex-config>
<compiler>
<source-path>
<path-element>../../src</path-element>
</source-path>
<library-path>
<path-element>../../libs</path-element>
<path-element>@FLEX_LIBS_HOME@/libs</path-element>
<path-element>@FLEX_LIBS_HOME@/locale/{locale}</path-element>
</library-path>
</compiler>
<compiler.debug>false</compiler.debug>
<compiler.optimize>true</compiler.optimize>
<compiler.keep-as3-metadata>
<name>Bindable</name>
<name>Managed</name>
<name>ChangeEvent</name>
<name>NonCommittingChangeEvent</name>
<name>Transient</name>
</compiler.keep-as3-metadata>
</flex-config>
注意:
1. 如果在项目中使用embed内嵌资源,在编译过程中出现找不到资源的错误 ,这种情况是路径不正确引起的,所在要引用资料时路径设置为从根目录开始 ,就以项目下assets文件夹下test.png为例,
应该这样写 embed(source=''/assets/test.png") 不要写为embed(source=''assets/test.png")
2. 需要对第个module进行编译