Compiling SWFs with ANT + MTASC + Eclipse + FDT

The responsibilities of the tools involved:

Ant: Runs arbitrary commands on your computer. i.e.: Launch applications, run command line tools, FTP files, etc. in this case we are using it to launch MTASC and trigger commands in Eclipse to view a swf.
MTASC: Compiles ActionScript code and injects it into an exisitng swf file.
Eclipse: Acts as a platform and UI for running Ant and MTASC and viewing SWFs. MTASC does not compile swfs from flas.

Overview

We're going to use ANT to launch MTASC and compile ActionScript into an existing swf. Then call a command in Eclipse to view the swf file.

Setup

  1. Setup Eclipse and FDT (Ant should come installed with Eclipse.)
  2. Extract hamtasc.zip to C:\Program Files\hamtasc\
  3. Choose an existing Flash project in Eclipse or:
    1. Create a new Flash project in Eclipse
    2. Create a .fla file
    3. Compile your .fla to create a .swf file.
  4. Create a build.xml file in your Flash project.

    Example build.xml
    
    <project name="My Flash Build" default="Compile and Launch" basedir=".">
    
    
    
        <property name="target_swf" value="my_movie.swf"/>
    
        <property name="main_class" value="MyClass.as"/>
    
        <property name="mtasc" location="C:\Program Files\hamtasc\mtasc.exe"/>
    
        
    
        <target name="Compile and Launch">
    
            <antcall target="Compile Swf" />
    
            <antcall target="Launch Swf" />        
    
        </target>
    
        
    
        <target name="Compile Swf">
    
            <exec executable="${mtasc}" dir="." failonerror="true">
    
                <arg line="-cp f:\"/>
    
                <arg line="-cp .\classes\"/>
    
                <arg line="-v -version 8 -mx -swf"/>
    
                <arg line="${target_swf} ${main_class}"/>
    
            </exec>
    
        </target>
    
    
    
        <target name="Launch Swf">
    
            <dirname property="swf_path" file="${target_swf}"/>
    
            <fdt.viewDocument location="${swf_path}\${target_swf}"/>
    
        </target>
    
        
    
    </project>
  5. Make sure the
    target_swf
    property points to the swf you want to compile file, relative to the build.xml file.
    e.g. ..\my_movie.swf
  6. For whatever reason, you need to point to a class to make the compiler happy. Make sure the
    main_class
    property points to a class. I haven't figured out why yet.
    e.g. com\package\AClass.as
  7. Make sure the
    mtasc
    property points to your mtasc compiler. It already should if you followed the instructions.
    e.g. C:\Program Files\hamtasc\mtasc.exe
  8. In the
    <exec>
    node, under the "Compile Swf" target, make sure you add any class paths you need. The path can absolute or relative to your project.
    e.g.
    
    <arg line="-cp f:\"/>
    
    <arg line="-cp .\classes\"/>
  9. Open the Ant view in Eclipse. Window > Show View > Other...

  10. Drag your build.xml onto the Ant view.
  11. In the Ant View, expand the "My Flash Build" Node. You should see 3 targets (or commands): "Compile and Launch","Compile Swf", and "Launch Swf".

  12. Go to Run Menu > External Tools > External Tools...
  13. On the left, select your "Ant build" and click new. On the right, select the JRE tab. Then select "Run the same JRE as the workspace". Hit Apply then Close. Don't ask.

  14. In the Ant view, double click 'Compile and Launch'. Magic.

  15. At this point you'll see one of two things. A swiftly copmiled swf or a bunch of errors cause your code sucks.

Oh but there's more you can do!

Commands in ANT are called tasks. To launch MTASC we used the exec task. To view the swf file we used a custom task from FDT called fdt.viewDocument. Here's a list of other tasks that ANT supports:
http://ant.apache.org/manual/CoreTasks/

Task Examples