Using Ant with Flash
An introduction to Ant, and how to use it with Mtasc to compile swfs. 2005-07-27
Ant for Flash Primer
So you're a Flash developer, and you've perhaps heard of Ant.
And perhaps you've heard of Mtasc. And
you're curious (or more probably need to know for work) what the heck
Ant is, and what it can do for you.
Assumed knowledge: XML, ActionScript 2.0, and basic command line usage.
Ant is a 'build tool', which means its used to compile and build software.
When you're working in Macromedia Flash MX2004, your build tool is Ctrl-Enter.
But generally with larger software projects, you aren't working with compiling
just one file, but many. Perhaps hundreds or thousands. On big projects,
pressing Ctrl-Enter a thousand times is not an option. Perhaps you'd also
like to zip the files, and notify someone via email that you've compiled.
And so build tools like Ant were born.
Installing Ant
You can download Ant from Ant's web
site.
You will also need Java.
After downloading, unzip the files to a directory. From now on, this
directory will be called c:/ant
Next we need to set your 'environment variables'. On Windows go to Control
Panel>System>Advanced>Environment Variables. You'll need to create
two new variables by clicking New:


These point to your Java and Ant directories. You then need to edit the
Path variable to include the ant executable.

Add:
;%ANT_HOME%\bin
To the end of the Variable value field.
Et voila! You should now be able to open up a command prompt and use
Ant by typing 'ant'. Although this is one way of doing it, depending on
your setup, you may be able to call Ant from within your text editor.
jEdit's AntFarm plugin does exactly that, and Ant is also built into Eclipse.
I have jEdit set to compile with Ant using Ctrl-Enter, works great.
So How Does Ant Work?
When you type 'ant' at the command prompt, ant looks for a file called
'build.xml' in the current directory and attempts to run the instructions
inside it.
Lets look at a simple build.xml:
<project name="Hello World" default="compile" > <target name="compile"> <echo message="Hello World!"/> </target> </project>
You might have noticed that Ant's build files are XML. This is a Very
Good Thing.
You can execute this build by typing 'ant' in the command prompt in the
directory of the build.xml:
Z:\chris\ant_primer>ant
Buildfile: build.xml
compile:
[echo] Hello World!
BUILD SUCCESSFUL
Total time: 0 seconds
This is a simple build file that contains one 'target' (something for
ant to do) named 'compile'. This is the <project/>'s default target
(default="compile") so when we run ant without any arguments
it will be the target called. Inside our target we have an echo 'task'
which simply echos out the text 'Hello World!'.
Ant could be really useful! So how do I use it with Flash?
Mtasc is an open source ActionScript compiler, freely downloadable off
the web. Now we'll use Ant to automate building of a blank swf file with
Mtasc. First Download and unzip
Mtasc to a directory.
Then create this file as 'build.xml':
<project name="Blank swf with MTASC" default="compile" basedir=".">
<description>How to create a swf with Ant and Mtasc</description>
<property name="mtasc" location="C:\Program Files\mtasc-1.09\mtasc.exe"/>
<property name="swfOut" location="blank.swf"/>
<target name="compile">
<echo message="Compiling ${swfOut}."/>
<exec executable="${mtasc}" failonerror="true">
<arg value="-swf"/>
<arg value="${swfOut}"/>
<arg value="-header"/>
<arg value="800:600:20"/>
<arg value="-main"/>
<arg value="MainClass.as"/>
</exec>
</target>
</project>
Mtasc needs a class to compile, in this example we'll call it MainClass.as.
Create this file in the same folder as build.xml with the following:
class MainClass.as{
public static function main(){
//nada
}
}
Also make sure the location attrib of the mtasc <property/> points
to the correct location of mtasc.
This is closer to a full Ant project, with a description, variables,
and compilation. We assign two variables with the <property/> tag,
the location of mtasc, and the name of the swf we want to build. Then
the target named 'compile' echoes out the name of the file we're building,
and then runs Mtasc via the <exec/> task. The <exec/> task
has a list of <arg/> tags, that are different paramters Mtasc requires
to run. ( Check Mtasc's site for
more info on the parameters ).
Notice also that Ant's very smart when it comes to file locations. Ant
was built to be a cross-platform build tool. Except for
the location of mtasc which needs to be changed, this file will build
on Linux, Mac and Windows without problem.
And those are the Ant basics! There is a plethora of Ant tasks available
that can generate documentation, zip files, delete files, create directories,
send email and more. Ant also has fantastic documentation, available
online covering all the ant tasks, their arguments, and general usage.
Bigger And Better
As flash projects get bigger and better, Ant will most likely become
the standard for building files with Flash. This should get you compiling
flash with Ant, but barely touches upon the cool features. Ant has tons
of 'tasks' to automate your coding experience. For more information I
recommend Ant: The Definitive
Guide.
|