Prerequisite knowledge
Required products Sample files  

A basic knowledge of Adobe Flex 3, and a working knowledge of object-oriented programming principles are needed. Knowledge of compiling Flex applications with the command prompt is helpful, but not vital.

Flash Builder 3 (Download trial) (58 KB)
User level      
In Part 1 of this article, I showed you how to create a basic Ant script that made use of Flex Ant tasks to automate your project's build process.
In this article, I'll expand on what I covered in Part 1 and add more commonly used tasks to enhance the Ant build script. The following list includes the most typical types of tasks a developer would need to automate in their build script.
  • ASDoc creation
  • HTML wrapper
  • RSL creation
  • SWC creation
Beyond just compiling a SWF file, these tasks automate more of the build process and allow other developers on your team to create new releases of your Flex project. There are many variants that can be implemented with your script, and you can remove or add these tasks at your discretion.

Setting up the project

If you've read Part 1 of this article, you probably have Ant installed already. For this article, you'll also need to download the new sample files. After you do, start up Flex Builder 3 and create a new Flex project called flex_ant_pt2_Tasks. Point it to the flex_ant_pt2_Tasks folder you downloaded and unzipped. Figure 1 shows the Flex project directory structure.
The flex_ant_pt2_Tasks directory structure.
Figure 1: The flex_ant_pt2_Tasks directory structure.
You'll notice the same and build.xml files in the root of the application, as siblings of the "src" folder. Open the file by right clicking it and selecting Open With > Text Editor. It's the same file from Part 1, but with a few new variables.
# defines the Flex framework files location FLEX_FRAMEWORK = ${FLEX_HOME}/frameworks/libs # this property defines the doc directory, which will store your # created # ASDocs later in the article DOC_DIR =${basedir}/DOC # defines the title for your ASDoc pages DOC_TITLE ="Adobe Flex Ant Tasks" # defines the footer for your ASDoc pages DOC_FOOTER = "Copyright 2009 My Company" # points to your asdoc.exe for ASDoc creation later. Make sure the path # is right according to your operating system asdoc.exe =${FLEX_HOME}/bin/asdoc.exe
Starting at line 15, you'll see the FLEX_FRAMEWORK, DOC_DIR, DOC_TITLE, DOC_FOOTER, and asdoc.exe variables defined in order.
FLEX_FRAMEWORK defines the location of your Flex framework files. This will be used for the RSL (run-time shared libraries) task later.
DOC_DIR is a folder that the Ant script will create when it runs. This folder will hold all the HTML files created from the ASDoc task.
The DOC_TITLE and DOC_FOOTER simply set the title and footer values for the HTML pages created by the ASDoc tool. The remaining asdoc.exe variable specifies the location of the tool on the machine running the build script.

New and improved build script

Open the build.xml file by right clicking on it and selecting Open With > Ant Editor (assuming you installed Ant as a Flex Builder plug-in). You'll notice the script has gained a lot of weight since you last saw it in Part 1.
I'm going to dissect it piece by piece to show you what changed from the Part 1 tasks and how the new tasks work.
The first change to note is in the project node at the very top. It no longer has the default attribute pointing to the "compile flex project" task, but rather, it points to a new task named "compile flex ant tasks project", which is defined on line 9:
<target name="compile flex ant tasks project" depends="init, compile flex project, build doc, copyRSLs, wrapper, createSWC" />
Its purpose is to serve as a kind of entry point to wrap all the other tasks in the script (see Figure 2). The depends attribute lists all the tasks in order, so when the script is run it'll make sure all tasks are completed. In contrast, the Part 1 build script simply deleted and recreated the DEPLOY directory, and compiled the SWF file into it. By wrapping all the tasks together here, you have that one all-encompassing target to run all the tasks, or you can run each separately if you choose.
The "compile flex ant tasks project" task's only purpose is to link together all the other tasks and ensure the script will run them all.
Figure 2: The "compile flex ant tasks project" task's only purpose is to link together all the other tasks and ensure the script will run them all.
Further down in the build.xml file, you can see the "init" task now also creates and deletes the DOC directory set by the DOC_DIR variable in the file, which will become the new home for the HTML files created by the ASDoc tool.
<target name="init"> <delete dir="${DEPLOY_DIR}" /> <mkdir dir="${DEPLOY_DIR}" /> <delete dir="${DOC_DIR}" /> <mkdir dir="${DOC_DIR}" /> </target>

The ASDoc task

On line 20 in the build script is the newly added "build doc" task, which looks like this:
<!-- ASDoc creation --> <target name="build doc" depends="init"> <exec executable="${asdoc.exe}" failonerror="true"> <arg line='-doc-sources ${SRC_DIR}' /> <arg line='-warn-level-not-supported=false'/> <arg line='-main-title ${DOC_TITLE}' /> <arg line='-window-title ${DOC_TITLE}' /> <arg line='-footer ${DOC_FOOTER}' /> <arg line='-output ${DOC_DIR}' /> </exec> </target>
You can include any argument in this task that you'd normally use with the ASDoc tool at the command prompt. Here, the script points the executable attribute to the asdoc.exe variable set in the file. The failonerror attribute is set to true so if there are problems, it will be brought to your attention right away.
The -doc-sources argument points to the project's main src folder. Further down, the script makes use of the DOC_TITLE and DOC_FOOTER variables set in the file. These will set the title and the footer note on each HTML page within the created files (see Figure 3). For a full list of the supported arguments, see Running the ASDoc tool.
Output documentation showing the DOC_TITLE and DOC_FOOTER values set in the file.

Figure 3: Output documentation showing the DOC_TITLE and DOC_FOOTER values set in the file.
The compile with RSLs tasks

The "compile flex project" task is a bit bigger now. That's because it has more to do than just compile the SWF file. You'll see a few new options listed inside this task that are needed for the RSL deployment option.
<!-- Build and output the Main.swf--> <target name="compile flex project" depends="init"> <mxmlc file="${SRC_DIR}/Main.mxml" output="${DEPLOY_DIR}/Main.swf"> <load-config filename="${FLEX_HOME}/frameworks/flex-config.xml"/> <source-path path-element="${FLEX_HOME}/frameworks"/> <compiler.debug>false</compiler.debug> <runtime-shared-library-path path-element="${FLEX_FRAMEWORK}/framework.swc"> <url rsl-url="framework_3.2.0.3958.swf"/> <url rsl-url="framework_3.2.0.3958.swz"/> </runtime-shared-library-path> </mxmlc> </target>
The runtime-shared-library-path node tells the mxmlc compiler that you want to use RSLs, and the path-element attribute points to the installed framework.swc that comes with the Flex SDK.
The url nodes specify the location of the SWF and SWZ files to be loaded at run time via the rsl-url attribute. This script shows the bare necessities needed to use RSLs; there are many other options you can employ. There's one more step here–copying the RSLs to the DEPLOY folder–but I'll cover it later in the article since it involves a different task. For now, this is how you'd compile the Flex application and have it make use of RSLs for Flash Player caching.
See About cross-domain RSLs for other options, XML configuration file nodes, and attributes you can set in your Ant script that are passed to the mxmlc compiler.

The HTML wrapper task

The HTML wrapper task, starting on line 48 of build.xml, is another Flex Ant task that gives you many options. In this case the options are used in creating the files that you deploy with your Flex application.
<!-- HTML Wrapper --> <target name="wrapper"> <html-wrapper title="Flex Ant Tasks Part II" file="flext_ant_pt_II_tasks.html" height="500" width="500" bgcolor="#000000" application="Main" swf="Main" version-major="9" version-minor="0" version-revision="0" history="true" template="express-installation" output="${DEPLOY_DIR}"/> </target>
This task is simply another target containing an html-wrapper node with some attributes you may want to set for your HTML page. The application attribute points to Main.mxml (the .mxml extension is automatically appended, so don't add it here).
The template attribute is set to use the express-installation template in conjunction with the history attribute set to true. This particular combination will also output the deep-linking files necessary for history management, but there are other combinations that are beyond the scope of this article.
The output attribute points to the DEPLOY directory, where the task will place the HTML, AC_OETags.js, and any deep linking files. Use the file attribute to specify the name of the HTML page and the swf attribute to specify the name of the output SWF file. The rest of the attributes are self-explanatory. To keep things simple in this article, I'm only listing the basic options for each task; for more details, see Using the html-wrapper task.

The create SWC task

Starting on line 66 of the build.xml file is the "createSWC" task:
<!-- create SWC --> <target name="createSWC"> <compc output="${DEPLOY_DIR}/MyComps.swc" include-classes="com.pxldesigns.flexTasks.LabelBuilder"> <source-path path-element="${SRC_DIR}"/> </compc> </target>
To create the SWC, the task invokes the compc compiler. For this article, the task points to a single ActionScript class: the LabelBuilder class. The include-classes attribute in this case points to the one class, but if you were to add multiple classes in the creation of your SWC, you'd provide a space-delimited list of class names.
The output attribute points to your DEPLOY directory and names the SWC file MyComps.swc. The path-element attribute of the source-path node sets the source directory of the project so the compc compiler knows where to find your ActionScript files when the task is run.
Unlike the include-classes attribute, you would not use a space-delimited list of names to include multiple assets, such as images, in your SWC. Instead you would use <include-file> nodes.
This is another very simple example; to read more on the options for this task, see Using the compc task.

One more step in the build script

Remember that earlier, I mentioned one more step regarding RSLs? This is it. The last task in the build script, shown on line 75, is named "copyRSLs". When you run the script, the "compile flex project" task will set your SWF to use the RSLs, but it won't copy them into the DEPLOY folder for you. This task performs the copy operation.
<!-- copy only the Flex Framework files into the DEPLOY_DIR to use for RSLs --> <target name="copyRSLs"> <copy todir="${DEPLOY_DIR}" file="${FLEX_HOME}/frameworks/rsls/framework_3.2.0.3958.swf"/> <copy todir="${DEPLOY_DIR}" file="${FLEX_HOME}/frameworks/rsls/framework_3.2.0.3958.swz"/> </target>
This article is about automating your build process to make things as easy as possible in a team environment. So this is another cool way to take advantage of Flex Ant tasks to do some more work for you. You'll recall from Part 1 that Flex Ant tasks extend Java Ant tasks. As a result, you have access to everything the Java Ant tasks are capable of, like creating directories, copying and moving files, deleting folders, and so on.
This last task in the build script uses the copy todir command to copy the Flex framework RSLs from their default location to the DEPLOY folder for easy deployment. Just like the "init" task, which uses delete and mkdir commands to remove and recreate the DEPLOY and DOC folders, you can have this task use file operations to make life easier for everyone on the team.
The copy node points to the DEPLOY folder with the todir attribute, and the file attribute points to the default location of the framework RSL that you're using in this script.

Running the newly improved build script

If you've installed the Flex Ant plug-in, simply drag the build.xml file into the Ant view and double click it within the view. This will set off all the tasks you chained together in the "compile flex ant tasks project" target in the beginning of the script. If you want to run one particular task, just double click it inside the Ant view in Flex Builder.
To run the build script with stand-alone Ant, open a command prompt, navigate to your base directory for the project (where the build.xml is located), and type:
ant -lib ${basedir}/libs/flexTasks.jar
This will ensure that developers running the script who don't have access to the flexTasks.jar file can use the one you provide them in the libs directory.
Whether you used the Ant plug-in in Flex Builder or the stand-alone version, you will see output in the console listing the commands for each task as they run, and at the bottom you should see something like:

BUILD SUCCESSFUL Total time: 21 seconds


The DEPLOY directory now holds files created by Flex Ant tasks.
Figure 4: The DEPLOY directory now holds files created by Flex Ant tasks.
Inside of the DEPLOY and DOC directories, you can now find all the files created by each task in the Ant script (see Figure 4).

Where to go from here

In this article you added new functionality, taking advantage of Flex Ant tasks to fully automate the build process with some of the more commonly used features in Flex projects.
There's still some creative license involved in constructing your own build script. You've seen some very simple examples in this article, but I would strongly recommend that you read through Adobe's help pages to learn more about the different options available for each task.