Automating Flex Compilation Using ANT

June 15th, 2008 by Greg Jastrab

When we first started developing Flex applications for clients when the time would come to send the SWF over, I would build the application in Flex Builder and send off the generated SWF. This got the job done, but it imposed a few limitations since I was the only Flex developer in our office:

  • I was the only one that knew how to compile the application
  • If someone else wanted to try to compile the application, they’d have to install Flex Builder

After reading a blog post by Marc Hughes I realized it was time we put in place a more versatile environment for building Flex applications.


I settled on using ANT since the Flex SDK includes a set of ANT tasks to simplify compiling using ANT. I was quickly able to follow the examples to make an ANT script to compile a simple application:

Simple Build File

<?xml version=”1.0″?>
<project name=”TestANT” basedir=”.” default=”compile”>

  <taskdef resource=”flexTasks.tasks” classpath=”${basedir}/flexTasks/lib/flexTasks.jar” />

  <property name=”FLEX_HOME” value=”/var/lib/flex” />

  <target name=”compile”>
    <mxmlc file=”TestANT.mxml”>
     <load-config filename=”${FLEX_HOME}/frameworks/flex-config.xml” />
     <source-path path-element=”${FLEX_HOME}/frameworks” />
    </mxmlc>
  </target>

</project>

This works fine for a simple Flex project, but most projects we use utilize external libraries that have been compiled into a SWC. Getting the Cairngorm SWC compiled into my test application by following the documentation proved harder than it looked. I first tried the following build file to compile a project using Cairngorm:

Build File Attempting to Include Cairngorm

<?xml version=”1.0″?>
<project name=”TestANT” basedir=”.” default=”compile”>

  <taskdef resource=”flexTasks.tasks” classpath=”${basedir}/flexTasks/lib/flexTasks.jar” />

  <property name=”FLEX_HOME” value=”/var/lib/flex” />

  <target name=”compile”>
    <mxmlc file=”TestANT.mxml”>
     <load-config filename=”${FLEX_HOME}/frameworks/flex-config.xml” />
     <source-path path-element=”${FLEX_HOME}/frameworks” />
     <compiler.library-path dir=”${FLEX_HOME}/frameworks” append=”true”>
      <include name=”/var/lib/swcs/Cairngorm.swc” />
     </compiler.library-path>
    </mxmlc>
  </target>

</project>

I thought I had followed the instructions properly, but unfortunately this was yielding:

[mxmlc] /home/greg/blah/test.mxml(6): Error: Definition com.adobe.cairngorm.control:CairngormEvent could not be found.

After playing around with some parameters and re-reading the documentation a few times, i finally got it to include SWCs using the following code:

Build File to Properly Include SWCs

<?xml version=”1.0″?>
<project name=”TestANT” basedir=”.” default=”compile”>

  <taskdef resource=”flexTasks.tasks” classpath=”${basedir}/flexTasks/lib/flexTasks.jar” />

  <property name=”FLEX_HOME” value=”/var/lib/flex” />
  <property name=”LIBS” value=”/var/lib/swcs” />

  <target name=”compile”>
    <mxmlc file=”TestANT.mxml”>
     <load-config filename=”${FLEX_HOME}/frameworks/flex-config.xml” />
     <source-path path-element=”${FLEX_HOME}/frameworks” />
     <compiler.library-path dir=”${LIBS}” append=”true”>
      <include name=”Cairngorm.swc” />
     </compiler.library-path>
    </mxmlc>
  </target>

</project>

The documentation is confusing since it says to specify the ${FLEX_HOME}/frameworks directory as the dir attribute to the <compiler.library-path> tag. I had thought my modification of their example would work, since I was assuming the append attribute was indicating to also include the paths in addition to the frameworks directory. I guess that’s not the case, and by specifying the <compiler.library-path> parameter with append="true" included you are indicating to do the equivalent to mxmlc -l+=somepath on the command line.

7 Responses to “Automating Flex Compilation Using ANT”

  1. Hem Talreja says:

    Hi!
    You do not need to include individual swc’s in flex 3

    Here is the ant script that I’ve used that works…

    //////////////////////////////////////////////////////////////////////////////
    ANT SCRIPT (Build.xml)
    //////////////////////////////////////////////////////////////////////////////


    <!– –>

    ///////////////////////////////////////////////////////////////
    Call to the Build.xml

    ant -buildfile Build.txt main
    ///////////////////////////////////////////////////////////////

  2. You not going to believe it but I have wasted all day searching for some information about this. Thanks for this, it was a wonderful read and really helped me out. Thanks again,Dynalite

  3. Toks says:

    Does the above applies to AIR project, mxmlc is meant for flex main application?

    Also, if I have a mxml component specified within the main application mxml (i.e. WindowedApplication), how do I automatte the compilation?

  4. Greg Jastrab says:

    Toks: This specific example applies to just Flex, since it’s mxmlc. For AIR, there is an amxmlc compiler which just passes alternate options to mxmlc. You can either use the exec Ant tag to run amxmlc or pass the appropriate parameters to mxmlc (specifying the air-config.xml instead of flex-config.xml, etc).

  5. Toks says:

    Hi Greg

    Thank you. I’ll try it and get back to you later.

    Toks

  6. Toks says:

    Hi Greg,

    Sorry stressing you! Am confused.

    Let me layout my ambition clearly:

    Am developing a small Educational Authoring tool, to be used by teachers (without programming skill), to create interactive multiple lessons within each topic of interest. The tool generates an xml file which is converted to main mxml file (as wrapper) and custom components (also mxml files – one for each lesson).

    Problem:
    The teacher should be able to click a button on the authoring tool to build the new application (Similar to debug or run on the flex builder 3).

    How do I implement the command line compiler or ant on click of a button.

    Greatful if you can provide a way out.

    Toks

  7. Toks says:

    Hi Greg,

    Any clue to my ealier request?

    In the mean-time, I’m using a batch file to execute the command line: to compile and run the application thus:

    import flash.net.URLRequest;
    private function mxmlFilesComplied():void {

    var request : URLRequest = new URLRequest
    (’C:\\Users\\CELfm\\Desktop\\test.bat’);
    navigateToURL(request );
    }

    But not satisfy with this.

    Could you help?

    Toks

Leave a Reply

Greg Jastrab joined SmartLogic Solutions in June 2006. He is now the Technical Project Manager and oversees all Flash/Flex/AIR development. Follow @gjastrab on twitter

Greg Jastrab's posts