Using Flex Ant tasks

You can use the Flex Ant tasks in your existing projects or create new Ant projects that use them. There are three tasks that you can use in your Ant projects:

  • mxmlc — Invokes the application compiler. You use this compiler to compile applications, modules, resource modules, and CSS SWF files.

  • compc — Invokes the component compiler. You use this compiler to compile SWC files and Runtime Shared Libraries (RSLs).

  • html-wrapper — Generates the HTML wrapper and supporting files for your application. By using this task, you can select the type of wrapper (with and without deep linking support, with and without Express Install, and with and without Player detection), as well as specify application settings such as the height, width, and background color.

  • asdoc — Generates ASDoc output for the specified classes in your project.

To use the custom Flex Ant tasks in your Ant projects, you must add the flexTasks.jar file to your project’s lib directory, and then point to that JAR file in the taskdef task. A taskdef task adds a new set of task definitions to your current project. You use it to add task definitions that are not part of the default Ant installation. In this case, you use the taskdef task to add the mxmlc, compc, and html-wrapper task definitions to your Ant installation. In addition, for most projects you set the value of the FLEX_HOME variable so that Ant can find your flex-config.xml file and so that you can add the frameworks directory to your source path.

Use the Flex tasks in Ant:

  1. Add a new taskdef task to your project. In this task, specify the flexTasks.tasks file as the resource, and point to the flexTasks.jar file for the classpath. For example:

    <taskdef resource="flexTasks.tasks" classpath="${basedir}/flexTasks/lib/flexTasks.jar"/>
  2. Define the FLEX_HOME and APP_ROOT properties. Use these properties to point to your Flex SDK’s root directory and application's root directory. Although not required, creating properties for these directories is a common practice because you will probably use them several times in your Ant tasks. For example:

    <property name="FLEX_HOME" value="C:/flex/sdk"/> 
    <property name="APP_ROOT" value="myApps"/>
  3. Write a target that uses the Flex Ant tasks. The following example defines the main target that uses the mxmlc task to compile the Main.mxml file:

    <target name="main"> 
        <mxmlc file="${APP_ROOT}/Main.mxml" keep-generated-actionscript="true"> 
            <load-config filename="${FLEX_HOME}/frameworks/flex-config.xml"/> 
            <source-path path-element="${FLEX_HOME}/frameworks"/> 
        </mxmlc> 
    </target>

    The following shows the complete example:

    <?xml version="1.0" encoding="utf-8"?> 
    <!-- mySimpleBuild.xml --> 
    <project name="My App Builder" basedir="."> 
        <taskdef resource="flexTasks.tasks" 
            classpath="${basedir}/flexTasks/lib/flexTasks.jar"/> 
        <property name="FLEX_HOME" value="C:/flex/sdk"/> 
        <property name="APP_ROOT" value="myApp"/> 
        <target name="main"> 
            <mxmlc file="${APP_ROOT}/Main.mxml" keep-generated-actionscript="true"> 
                <load-config filename="${FLEX_HOME}/frameworks/flex-config.xml"/> 
                <source-path path-element="${FLEX_HOME}/frameworks"/> 
            </mxmlc> 
        </target> 
    </project>

    This example shows how to use different types of options for the mxmlc task. You can specify the value of the keep-generated-actionscript option as an attribute of the mxmlc task's tag because it does not take any child tags. To specify the values of the load-config and source-path options, you create child tags of the mxmlc task's tag. For more information on using options, see Working with compiler options.

  4. Execute the Ant project, as shown in the following example:

    > ant -buildfile mySimpleBuild.xml main

    If you did not copy the flexTasks.jar file to Ant’s lib directory as described in Setting up Flex Ant tasks, you must include the JAR file by using Ant’s -lib option. For example:

    > ant -lib c:/ant/lib/flexTasks.jar -buildfile mySimpleBuild.xml main

The output of these commands should be similar to the following:

Buildfile: mySimpleBuild.xml 
main: 
    [mxmlc] Loading configuration file C:\flex\sdk\frameworks\flex-config.xml 
    [mxmlc] C:\myfiles\flex4\ant_tests\apps\Main.swf (150035 bytes) 
BUILD SUCCESSFUL 
Total time: 10 seconds 
>

To embed fonts in your Flex ant tasks, you must be sure to include the font manager in your build file. For more information, see Embedding Fonts Using AS3 and Flex 4 Ant Tasks.