There are several reasons to use Apache Ant to build your ActionScript and Adobe Flex projects:
1) If your team uses Continuos Integration.
2) Your project has lots of dependencies that have version branches of their own.
3) You don’t want to shell out the bucks for Flash Builder.
4) You want to quickly build a project without having to import the project into Flash Builder.
There are quite a few blog posts out there on how to do this, so I’ll keep this short and simple so you can get started.
To start, download the zip file here, I’ll discuss the contents of the zip below. If you are impatient or simply don’t like to read, open a Terminal window, change to the directory where you unzipped the file and type “ant”, and press Enter. The script will build the project, create ASDocs, and then create a release zip file of the built project.
If you are on Windows, you’ll need to download and install Apache Ant. You will also need the Flex SDK installed on your system, and the location of the SDK is specified in the “build.properties” file you will find in the zip.
Rather than use a “Hello World” app, we’ll start with an OSMF-based player that is as simple as it gets. This is the bare-bones code to play a video with the OSMF (Open Source Media Framework). You’ll find a ./src and a ./libs folder in the zip. The ./libs folder contains the OSMF.swc built for Flash Player 10.1. This is the same SWC file available at osmf.org.
The ./src folder contains one ActionScript file, OSMFSimple.as:
[SWF(backgroundColor="0x000000", frameRate="25", width="640", height="360")]
* This is as simple as an OSMF-based media player can get.
public class OSMFSimple extends Sprite
private static const MEDIA:String =
private var mediaPlayerSprite:MediaPlayerSprite;
public function OSMFSimple()
mediaPlayerSprite = new MediaPlayerSprite();
new VideoElement(new StreamingURLResource(MEDIA));
The Ant Build Script
You will notice two files in the zip, “build.properties” and “build.xml”.
The build.properties file contains the settings that can be modified by developers to customize the build for their development environment. The idea here is: developers can tweak this file for their dev environments without having to touch the build script. This particular file contains only locale and the Flex SDK version location, but it could contain properties tied to environment variables, for example.
FLEX_HOME=/Applications/Adobe Flash Builder 4/sdks/4.1.0
The build.xml file is the build script. Download the zip file and take a look at the build.xml file. You will see tasks for cleaning the bin-release folder, cleaning the deploy folder, cleaning the ASDocs folder, creating the HTML wrapper files, building the release, building the ASDocs, and creating the zip file.
Notice at the top of file, in the project tag you see default=”main”. This tells Ant to look for a task named “main”. Now look at the bottom of file and you’ll see the main task. This simply kicks off the tasks required to build a release of this project.
To run the script, simply open a Terminal window, change to the directory, type “ant” and hit enter. Because the default task is defined as “main”, no other arguments are required.
If you wanted to run a particular task, you could supply that on the command line, such as:
You don’t need to name your Ant script “build.xml”. If you don’t, you need to supply the filename on the command line like this:
ant -f master-build.xml
And then to specify a task within that build script:
ant -f master-build.xml release-build
Run the script by opening a Terminal window, change to the folder where you unzipped the file, and type “ant”, hit Enter. You should see output similar to this:
[echo] cleaning deploy folder...
[echo] cleaning asdocs...
[mxmlc] Loading configuration file /Applications/Adobe Flash Builder 4/sdks/4.1.0/frameworks/flex-config.xml
[mxmlc] /Users/charles/Downloads/OSMFSimple/bin-release/OSMFSimple.swf (114460 bytes)
[echo] building asdocs...
[asdoc] Loading configuration file /Applications/Adobe Flash Builder 4/sdks/4.1.0/frameworks/flex-config.xml
[asdoc] Documentation was created in /Users/charles/Downloads/OSMFSimple/docs/
[zip] Building zip: /Users/charles/Downloads/OSMFSimple/deploy/OSMFSimple-release.zip
Total time: 17 seconds
If you were building a library (a SWC file) rather than a SWF, you would use the <compc> task rather than the <mxmlc> task. For documentation on the Ant tasks supported by the Flex SDK see this page.
There are many other things you can do with Ant, such as sync with your Perforce depot, deploy to an FTP server, etc.
The only thing I haven’t figure out how to do with Ant (yet) is duplicate the “include source” check box in Flash Builder when exporting a release build. This is handy when creating sample projects. If anyone has figured this out please post a comment here.
I hope this helps you get started with ActionScript and Ant. It has made life much easier for our development team.