After a couple of days creating an ant build script for a Flex app previously only ever built from within Eclipse I thought I’d share my pain.
Asset references
To avoid compilation errors when the mxmlc task can’t find your assets refer to images, fonts, etc with an absolute path (relative to the flex src directory), e.g…
<mx:Image source="@Embed(source='/../assets/an_icon_with_a_sensible_absolute_path.png')"/>
This also has the happy side effect of making the paths of these assets more sensible than the alternative crazy relative path approach, e.g. the above line replaced a line similar to the following…
<mx:Image source="@Embed(source='../../../../../../../assets/an_icon_with_a_crazy_relative_path.png')"/>
Eclipse environment variables
Increase the Java Heap space if you get an OutOfMemory exception during the mxmlc task when running the ant build from within Eclipse.
Select the JRE used in your Eclipse project and add this to the command line… -Xmx512m
See http://www.themorphicgroup.com/blog/2009/07/10/compiling-using-ant-in-flex-builder-error-java-heap-space/ for more details.
Command line environment variables
export ANT_OPTS=-Xmx512m
export ANT_HOME=<ant 1.7 installation dir>
export PATH=$ANT_HOME/bin:$PATH
export JAVA_HOME=<jdk 1.6 installation dir>
export JAVACMD=${JAVA_HOME}/bin/java
Flex Builder Pro license
If using Data Visualization Components (charts, etc) then to avoid the trial watermark provide the mxmlc task with the Flex Builder Pro license number, e.g. ..
<license product="flexbuilder3" serial-number="000000000000000000000000"/>
Note the lowercase product name and the format of the serial-number.
If running the ant build on a machine with Flex Builder Pro installed be aware that the Flex Builder Pro license can be picked up from a variety of locations, see http://livedocs.adobe.com/flex/3/html/help.html?content=configuring_environment_2.html#212596 for details.
Embed fonts with url not local
To increase the portability of your Flex app embed Fonts using urls (preferably part of your version controlled Flex assets), e.g…
@font-face { src: url("/../assets/fonts/FancyFont.ttf"); fontFamily: myFancyFont; }
See https://gerrymclarnon.wordpress.com/2010/05/25/flex-on-a-mac-and-gcgetglyphdevicemetrics for more details.
Roll your own index.html
The html-wrapper task doesn’t handle a fancy dan index.template.html very well, so you can roll your own task, e.g…
<target name="fancy.dan.html-wrapper"> <copy file="${basedir}/html-template/AC_OETags.js" tofile="${STAGING_LOCATION}/AC_OETags.js" /> <copy file="${basedir}/html-template/playerProductInstall.swf" tofile="${STAGING_LOCATION}/playerProductInstall.swf" /> <copy file="${basedir}/html-template/swfMouseWheel.js" tofile="${STAGING_LOCATION}/swfMouseWheel.js" /> <copy file="${basedir}/html-template/index.template.html" tofile="${STAGING_LOCATION}/index.html" /> <replace file="${STAGING_LOCATION}/index.html" token="$${swf}" value="${APP_NAME}"/> <replace file="${STAGING_LOCATION}/index.html" token="$${application}" value="${APP_NAME}"/> <replace file="${STAGING_LOCATION}/index.html" token="$${width}" value="100%"/> <replace file="${STAGING_LOCATION}/index.html" token="$${height}" value="100%"/> <replace file="${STAGING_LOCATION}/index.html" token="$${bgcolor}" value="#000000"/> <replace file="${STAGING_LOCATION}/index.html" token="$${version_major}" value="9"/> <replace file="${STAGING_LOCATION}/index.html" token="$${version_minor}" value="0"/> <replace file="${STAGING_LOCATION}/index.html" token="$${version_revision}" value="28"/> </target>
Don’t rely on Eclipse
Just to state the obvious it’s best not to rely on any Eclipse generated class files, relocated properties files, etc. If you do rely on Eclipse then this will only lead to confusion when someone, who hasn’t first of all built the app from within Eclipse, runs your ant file from the command line and gets different results to you!