tags:

views:

4248

answers:

4

I am just getting started with flex and am using the SDK (not Flex Builder). I was wondering what's the best way to compile a mxml file from an ant build script.

+6  A: 

The Flex SDK ships with a set of ant tasks. More info at:

http://livedocs.adobe.com/flex/3/html/help.html?content=anttasks_1.html

Here is an example of compiling Flex SWCs with ant:

http://www.mikechambers.com/blog/2006/05/19/example-using-ant-with-compc-to-compile-swcs/

mike chambers

mikechambers
Can I get a mxmlc SWC file somewhere or do I have to compile it myself? I am able to compile actionscript fine using the mxmlc ant tag but the same script doesn't work for mxml files. I've trying to use the java jar file in my target but with no success. Thanks!
thaiyoshi
A: 

If you're open to Maven, try the flex-compiler-mojo plugin:

http://code.google.com/p/flex-mojos/

Christiaan

+2  A: 

I would definitely go with the ant tasks that are included with Flex, they make your build script so much cleaner. Here is a sample build script that will compile and then run your flex project

<?xml version="1.0"?>

<project name="flexapptest" default="buildAndRun" basedir=".">

    <!-- 
        make sure this jar file is in the ant lib directory 
        classpath="${ANT_HOME}/lib/flexTasks.jar" 
    -->
    <taskdef resource="flexTasks.tasks" />
    <property name="appname" value="flexapptest"/>
    <property name="appname_main" value="Flexapptest"/>
    <property name="FLEX_HOME" value="/Applications/flex_sdk_3"/>
    <property name="APP_ROOT" value="."/>
    <property name="swfOut" value="dist/${appname}.swf" />
    <!-- point this to your local copy of the flash player -->
    <property name="flash.player" location="/Applications/Adobe Flash CS3/Players/Flash Player.app" />

    <target name="compile">
        <mxmlc file="${APP_ROOT}/src/${appname_main}.mxml"
            output="${APP_ROOT}/${swfOut}" 
            keep-generated-actionscript="true">

            <default-size width="800" height="600" />
            <load-config filename="${FLEX_HOME}/frameworks/flex-config.xml"/>
            <source-path path-element="${FLEX_HOME}/frameworks"/>
            <compiler.library-path dir="${APP_ROOT}/libs" append="true">
                <include name="*.swc" />
            </compiler.library-path>
        </mxmlc>
    </target>

    <target name="buildAndRun" depends="compile">
        <exec executable="open">
            <arg line="-a '${flash.player}'"/>
            <arg line="${APP_ROOT}/${swfOut}" />
        </exec>
    </target>

    <target name="clean">
        <delete dir="${APP_ROOT}/src/generated"/>
        <delete file="${APP_ROOT}/${swfOut}"/>
    </target>

</project>
jgormley
+1  A: 

There is another option - it's called Project Sprouts.

This is a system built with Ruby, RubyGems and Rake that provides many of the features found in Maven and ANT, but with a much cleaner syntax and simpler build scripts.

For example, the ANT script shown above would look like this in Sprouts:

require 'rubygems'
require 'sprout'

desc 'Compile and run the SWF'
flashplayer :run => 'bin/SomeProject.swf'

mxmlc 'bin/SomeProject.swf' do |t|
  t.input = 'src/SomeProject.as'
  t.default_size = '800 600'
  t.default_background_color = '#ffffff'
  t.keep_generated_actionscript = true
  t.library_path << 'libs'
end

task :default => :run

After installing Ruby and RubyGems, you would simply call this script with:

rake

To remove generated files, run:

rake clean

To see available tasks:

rake -T

Another great benefit of Sprouts, once installed, is that it provides project, class and test generators that will get any development box ready to run with a couple simple command line actions.

# Generate a project and cd into it:
sprout -n mxml SomeProject
cd SomeProject

# Compile and run the main debug SWF:
rake

# Generate a new class, test case and test suite:
script/generate class utils.MathUtil

# Compile and run the test harness:
rake test
Luke Bayes