Header Image - charles newman's blog

Category Archives

2 Articles

Building ActionScript 3 projects with Apache Ant

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.

The Code

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:

package
{
    import flash.display.Sprite;

    import org.osmf.elements.VideoElement;
    import org.osmf.media.MediaPlayerSprite;
    import org.osmf.net.StreamingURLResource;

    [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 =
            "rtmp://cp67126.edgefcs.net/ondemand/mp4:mediapm/osmf/content/test/spacealonehd_sounas_640_700.mp4";

	private var mediaPlayerSprite:MediaPlayerSprite;

	public function OSMFSimple()
	{
		mediaPlayerSprite = new MediaPlayerSprite();
		addChild(mediaPlayerSprite);

		mediaPlayerSprite.media =
                   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.

build.properties

#SDKLocation:
FLEX_HOME=/Applications/Adobe Flash Builder 4/sdks/4.1.0
SDK_VERSION=4.1.0.16076
LOCALE=en_US

build.xml

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:

ant release-build

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:

charles:OSMFSimple$ ant
Buildfile: /Users/charles/Downloads/OSMFSimple/build.xml

clean:
     [echo] cleaning...

clean-deploy:
     [echo] cleaning deploy folder...

clean-asdocs:
     [echo] cleaning asdocs...

release-build:
     [echo] building...
    [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)

wrapper:

build-asdocs:
     [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-deploy:
      [zip] Building zip: /Users/charles/Downloads/OSMFSimple/deploy/OSMFSimple-release.zip

main:

BUILD SUCCESSFUL
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.

Mac Ant Build – Error: PermGen space

This is worthy of a post because it took me a while to figure it out.

I created a fairly large Ant build script that kicks off builds for a project’s dependencies and it was working great until I added a few more things. It started failing with this:

[compc] Loading configuration file /Applications/Adobe Flash Builder 4/sdks/4.1.0/frameworks/flex-config.xml
[compc] Error: PermGen space

I’m not a hard core Java guy but I figured this had something to do with the Java Virtual Machine running out of memory. So after digging a bit and trying a few different things the solution I came up with was this:

export ANT_OPTS=-XX:MaxPermSize=512m

Try it on the command line first. If that works for you, you might want to add it to your .bash_profile. Once I added that, my build script was able to complete.