Table of Contents

Apache Ant

Ant is an XML language used to automate compiling of software similar to Make. It’s mostly used by Java developers but is becoming more common among Flash developers because it’s available in Eclipse IDE and it works well with MTASC. Commands can be added to Ant through plugins written in Java.

As2ant - Swf, Mtasc and Swfmill Ant Tasks

To integrate mtasc or swfmill into your ant build script, you can either work with the executable task directly, as explained in the next item, or you can use ant tasks that were specifically created for this. Using specific ant tasks, instead of the executable ant task, feels much more natural, is easier to learn and provides you in some cases with some extra and useful functionalities.

You can use the:

While the Mtasc and Swfmill Ant Tasks act mainly as mediator between ant and mtasc or swfmill respectively, the Swf Ant Task merges the possible usages of mtasc and swfmill into one task. This provides you with an easy way to compile classes into your swf and to include library symbols like movieclips and fonts and shared libraries into your swf.

Following is a sample of how your build.xml file may look like. Take a look at the comments of what is being done with certain declarations.

<project name="Swf Ant Task Samples - As2lib - Open Source ActionScript 2.0 Library - " default="usage" basedir=".">
  
  <!-- Declares references to needed directories. -->
  <property name="src.dir" location="src"/>
  <property name="lib.dir" location="lib"/>
  <property name="build.dir" location="build"/>
  
  <!-- Defines the Swf Ant Task by setting a name and the path to the task. -->
  <taskdef name="swf" classname="org.as2lib.ant.Swf" classpath="../../src"/>
  
  <!-- Default target that is used if none has been specified, prints list of available targets. -->
  <target name="usage">
    <echo message=""/>
    <echo message="------------------------------------------------------"/>
    <echo message="Among the available targets are:"/>
    <echo message=""/>
    <echo message="sample"/>
    <echo message="------------------------------------------------------"/>
    <echo message=""/>
  </target>
  
  <!-- Target that uses the Swf Ant Task. -->
  <target name="sample" description="builds sample swf">
    <!-- References the Swf Ant Task by the name given it in the task definition. -->
    <!-- 'src' is the main class, that declares a static 'main' method used by MTASC to start the application. -->
    <!-- 'dest' is the the destination file that must be a swf. -->
    <!-- 'width' and 'height' specify the size of the destination swf. -->
    <!-- 'framerate' is number of frames per second of the destination swf. -->
    <!-- 'bgcolor' is the swf's background color. -->
    <swf src="${src.dir}/com/simonwacker/ant/Sample.as" dest="${build.dir}/sample.swf" width="300" height="100" framerate="31" bgcolor="FF8A00">
      <!-- Includes the graphic simonwacker.jpg into the library that can be attached by identifier 'simonwacker'. -->
      <clip id="simonwacker" import="${lib.dir}/simonwacker.jpg"/>
      <!-- Embeds the font PixelClassic.ttf into the library and gives it the identifier 'pixel'. -->
      <font id="pixel" import="${lib.dir}/PixelClassic.ttf"/>
    </swf>
  </target>
  
</project>

This works basically the same with the Swfmill and Mtasc Ant Tasks, the supported attributes are of course different.

For more information on these tasks take a look at the samples provided in the donwloadable zip file and at the api documentation of each task which also includes sample code and the list of supported attributes.

Download As2ant - Swf, Mtasc and Swfmill Ant Tasks (these tasks were created by Simon Wacker and are part of the As2lib - Open Source ActionScript 2.0 Library).

Ant Examples for Flash Development

Here is a simple build.xml file for Eclipse that can compile your project using MTASC using the convenient green arrow button at the top of the screen. To get this to work, create a new file at the root of your project and save it as build.xml. Next open the file and paste the following XML into it. Then you will need to modify the values of all the property tags appropriately and save the file. You should now be able to run the Ant task from the green button.

<?xml version="1.0" encoding="utf-8"?>
<project name="YourProject" default="compileMTASC">
	<!-- user modifiable properties -->
	<property name="ASFile" value="Z:\Path\to\your\main\class\ApplicationDriver.as"/>
	<property name="SWFFile" value="Z:\Path\to\your\flash\main.swf"/>
	<property name="classPath" value="Z:\Path\to\your\flashclasses"/>
	<property name="MacromediaClassPath" value="C:\Documents and Settings\yourUserName\Local Settings\Application Data\Macromedia\Flash MX 2004\en\Configuration\Classes"/>
	<property name="additionalParams" value="-mx"/>
	
	<target name="compileMTASC">
		<exec executable="C:\Program Files\mtasc-1.07\mtasc.exe">
			<arg line='"${ASFile}" ${additionalParams} -swf "${SWFFile}" -cp "${classPath}"  -cp "${MacromediaClassPath}"'/>
		</exec>
	</target>
</project>

Here’s another example. It creates a swf with swfmill, uses mtasc to inject bytecode, and then, only if all goes well, displays the swf in the standalone player.

<project default="compile" basedir=".">
	<description>simple build file for flash projects</description>
	
	<!-- project specific properties -->
	<property name="targetswf" value="xxx.swf"/>
	<property name="mainclass" value="xxx.as"/>
	<property name="swfdata" value="swfdata.xml"/>
	<property name="flashplayer" value="C:\Program Files\Macromedia\Flash MX 2004\Players\SAFlashPlayer.exe"/>
	
	<!-- classpath info -->
	<property name="classpath1" value="C:\Documents and Settings\username\Local Settings\Application Data\Macromedia\Flash MX 2004\en\Configuration\Classes"/>
	
	<!-- tool info -->
	<property name="mtasc" location="C:/mtasc/mtasc.exe"/>
	<property name="swfmill" location="C:/swfmill/swfmill.exe"/>
	
	<!-- folder info -->
	<property name="source" location="source"/>
	<property name="deploy" location="deploy"/>
	
	<target name="compile">
		<exec executable="${swfmill}" dir="." failonerror="true">
			<arg value="simple"/>
			<arg value="${source}/${swfdata}"/>
			<arg value="${deploy}/${targetswf}"/>
		</exec>
		<exec executable="${mtasc}" failonerror="true">
			<arg value="-cp"/>
			<arg value="${classpath1}"/>
			<arg value="-cp"/>
			<arg value="${source}"/>
			<arg value="-swf"/>
			<arg value="${deploy}/${targetswf}"/>
			<arg value="-main"/>
			<arg value="-v"/>
			<arg value="-strict"/>
			<arg value="${source}/${mainclass}"/>
		</exec>
		<exec executable="${flashplayer}" spawn="true">
			<arg value="${deploy}/${targetswf}"/>
		</exec>
	</target>
</project>

Scott Whittaker: The following is an example of a simple Ant task which allows you to define the properties of your .swf, compiles it with MTASC and then opens an HTML page which contains the Flash movie. It uses a combination of techniques used in the examples above and includes a couple of comments to explain some of the ANT syntax used:

<?xml version="1.0" encoding="utf-8"?>
<project name="Flash Compiler" default="compile" basedir=".">
 
	<!-- Project properties -->
	<property name="app" value="Application.as"/>
	<property name="swf" value="index.swf"/>
 
	<!-- SWF properties -->
	<property name="width" value="760"/>
	<property name="height" value="570"/>
	<property name="framerate" value="31"/>
	<property name="bgcolor" value="FFFFFF"/>
 
	<!-- Additional properties -->
	<property name="additionalParams" value="-strict -main"/>
	<property name="html" value="index.html"/>
 
	<!-- Folders (project AS source and .swf destination) -->
	<property name="src_dir" value="src"/>
	<property name="deploy_dir" value="deploy"/>
 
	<!-- Classpaths -->
	<property name="lib_dir" value="C:\Flash\classLibrary"/>
	<property name="mm_dir" value="C:\Documents and Settings\username\Local Settings\Application Data\Macromedia\Flash MX 2004\en\Configuration\Classes"/>
 
	<!-- Tools -->
	<property name="mtasc" location="C:\Program Files\mtasc\mtasc.exe"/>
	<property name="browser" location="C:\Program Files\Internet Explorer\IEXPLORE.exe"/>
 
	<target name="compile">
 
		<!-- The "failonerror" attribute means that if MTASC fails to compile, the ANT task
		     will not continue and open the HTML page. -->
 
		<exec executable="${mtasc}" failonerror="true">
			<arg line='"${src_dir}/${app}"'/>
			<arg line='-swf "${deploy_dir}/${swf}"'/>
			<arg line='-header "${width}:${height}:${framerate}:${bgcolor}"'/>
			<arg line='-cp "${src_dir}" -cp "${lib_dir}" -cp "${mm_dir}"'/>
			<arg line='${additionalParams}'/>
		</exec>
 
		<!-- An ANT task will not end until until all processes created with "exec" have ended.
		     This means that the ANT task will wait until the browser window is closed before it
		     will finish. The "spawn" option lets ANT continue and finish without waiting for 
		     the browser window to be closed. -->
 
		<exec executable="${browser}" dir="." spawn="true">
			<arg line='${basedir}/${deploy_dir}/${html}'/>
		</exec>
	</target>
</project>

For more a more detailed guide to Ant, see the Ant Wiki, specifically the AntNewbies and AntTutorials pages.

You can also read Carlos Rovira‘s article Far Beyond Open Source Flash Development for AMES development related examples and useful hints.

Using Ant to unpack SWC Files

Ralb Bokelberg: The following script unpacks a F7 component. It also provides an example of ant macros.

<project name="UnpackSWC example" default="usage" basedir=".">
 
 <target name="usage">
   <echo message=""/>
    <echo message="------------------------------------------------------"/>
    <echo message="Replace 'myLib' in target unpackSWC with the basename of your swc file"/>
    <echo message="------------------------------------------------------"/>
    <echo message=""/>
 </target>
 
<target name="unpackSWC">
	<m_unpackSWC swcName="myLib"/> 
</target>
 
 <macrodef name="m_unpackSWC">
	<attribute name="swcName" default=""/>
	<attribute name="targetDir" default="swc"/>	
	
	<sequential>
                <!-- unzip the component --> 
		<unzip dest="@{targetDir}/@{swcName}/">
			<patternset>
			        <include name="**/*.asi"/>
			</patternset>	
			<fileset file="@{swcName}.swc"/>
		</unzip>
                <!-- copy the asi files intro the correct directory structure  -->
		<copy todir="@{targetDir}/@{swcName}/">
		    <fileset dir="@{targetDir}/@{swcName}" includes="**/*.asi"/>
			<mapper type="unpackage" from="*.asi" to="*.as"/>
	  	</copy>
                <!-- clean up -->
		<delete>
			<fileset dir="@{targetDir}/@{swcName}" excludes="**/*.as"/>
		</delete>	
	</sequential>	
</macrodef>