Intro to Building and Deploying Flex Applications with Ant

Unless your project is as simple as HelloWorld application, compilation and deployment of this application has to be automated. Creating a build script for any decent size application (i.e. hundreds of files in Flex and server-side Java) is a project on its own. Java programmers typically use one of the open source build tools such as Ant or Maven to formally describe and execute all steps required to build and deploy their projects.

If you did not have a chance to use any of the build tools before, this section is for you. This is a mini primer on how use Ant, a popular Java-based build tool from Apache.

Even deployment of such simple application as HelloWorld may consists of several steps, for example

1. Using mxmlc compile HelloWorld.mxml into swf file.
2. Create an HTML wrapper file(s) from one of the HTML templates.
3. Create or clean the directory in your Web server where HelloWorld files will be deployed.
4. Copy or ftp HelloWorld.swf and supporting files to this directory.

Now imagine that your HelloWorld application is a part of a larger application that includes Java modules that also need to be compiled and deployed under some server location. Deployment process of even a small size project usually involves at least a dozen of dependent tasks that have to be executed in a particular order. Creating and maintaining of a build script is well worth the effort

Building HelloWorld with Ant

With Ant, you describe your project in a build file written in xml (it’s typically called build.xml). An Ant project consists of targets (i.e. compile), which in turn consist of tasks (i.e. mkdir, exec). Listing below shows a build.xml that will create an output directory bin, and compile our HelloWorld application into this directory. This Ant build file has two targets: init and compile.

<project name=”HelloWorld” default=”compile”>
<property name=”flex.mxmlc” location=”C:\Program Files\Adobe\Flex Builder 2 Plug-in\Flex SDK 2\bin\mxmlc.exe” />
<property name=”dest.dir” value=”bin” />

<target name=”init”>
<delete dir=”${dest.dir}” />
<mkdir dir=”${dest.dir}” />

<target name=”compile” depends=”init”>
<exec executable=”${flex.mxmlc}” failonerror=”true”>
<arg line=”-output ‘${dest.dir}/HelloWorld.swf'”/>
<arg line=”HelloWorld.mxml”/>

The init target consists of two tasks: delete and create the directory called bin.

The compile target will compile HelloWorld.mxml producing bin\HelloWorld.swf. If we’d need to manually write a command that compiles HelloWorld.mxml into the bin directory, it’d look like this:

mxmlc -output bin/HelloWorld.swf HelloWorld.mxml

Besides targets and tasks, our build.xml contains two property tags. One of them defines the property called dest.dir that has a value bin, which is the name of the output directory for our compiled HelloWorld.swf:

<property name=”dest.dir” value=”bin” />

This means that wherever Ant will encounter dest.dir surrounded with ${ and }, it’ll substitute it with the value of this property, which is bin in our case.

The above project also defines a property called flex.mxmlc that tells Ant where mxmlc compiler is located. The target compile uses Ant’s task exec, which will run mxmlc compiler passing it arguments specified in <arg> tags.

Let’s download Ant from Installation is simple: just unzip the archive and add the path its bin directory (i.e. C:\apache-ant-1.6.5\bin) to the environment variable path of your OS.

Now we can open a command window, get into directory where HelloWorld.mxml and build.xml are located, and enter the command ant that will start the execution of the default target of our project, which is compile. But since compile target is marked as dependent on init, ant will execute the init target first.

Ant has extensive set of predefined tasks like copy, zip, jar, War, FTP, Mail, Condition, and many more described at Ant’s manual.

For example, to make our build file work in both Windows and Unix environments, we can specify the location of mxmlc compiler as follows:
<condition property=”flex.mxmlc” value=”${sdkdir}/bin/mxmlc”>
<os family=”unix”/>
<condition property=”flex.mxmlc” value=”${sdkdir}/bin/mxmlc.exe”>
<os family=”windows”/>

You can pass values of the properties from ant command line using –Dpropery=value option. For example, if we use the code snippet above, the property sdkdir can be passed from a command line:

ant –Dsdkdir=/opt/usr/freeflex

You can write more sophisticated ant scripts. For example, you can automate creation of an html wrapper for swf files with replaceregex task applied to one of the provided html templates. You can write build scripts that will merge Flex generated artifacts with Java Web applications. Automating builds with Ant can get you pretty far.

There are lots of online articles and books on Ant, but do not miss Ant in Anger by Steve Loughran, where he discusses some core practices, naming conventions and team development process with Ant.


4 thoughts on “Intro to Building and Deploying Flex Applications with Ant

  1. Thanks Yakov, lovely primer,

    Perfect timing as well as i needed this type of automation for compiling my app, rsl’s and modules.
    Very easy to follow, now i will get my Java guys to help me with more advanced questions :)

    Peace comrades

  2. This is cool post, which will help to many enterpise developers facing automating of flex builds.

    I also think that now is the time to to integrate Flex with Maven. :)

Comments are closed.