Pages

Sunday, December 5, 2010

Ant macros for Flex, Part 1

Ant macros are a nice way to reduce the size of build scripts, especially when you have lot's of modules and runtime shared libraries. The macrodef task always contains a sequential task containing all the actions to be done and multiple attributes and elements.

The first example is a macro for compiling modules using the mxmlc task (for applications you would not want use external libraries):

<macrodef name="compile-module">
  <attribute name="source-dir" default="${source.dir}"/>
  <attribute name="build-dir" default="${build.dir}"/>
  <attribute name="module-name"/>
  <element name="args" optional="true"/>
  <sequential>
    <echo message="Compiling module @{module-name}"/>
    <mxmlc debug="true" file="@{source-dir}/@{module-name}.mxml" incremental="true" link-report="@{build-dir}/@{module-name}-link-report.xml" locale="en_US" optimize="true" output="@{build-dir}/@{module-name}.swf">
      <source-path path-element="@{source-dir}"/>
      <external-library-path append="false" file="${libs.dir}/*.swc"/>
      <external-library-path append="false" file="${FLEX_HOME}/frameworks/locale/{locale}"/>
      <external-library-path append="false" file="${FLEX_HOME}/frameworks/libs/*.swc"/>
      <external-library-path file="${flex.playerglobal}"/>
      <args/>
    </mxmlc>
  </sequential>
</macrodef>

As you can see, the macro has three attributes, source-dir, build-dir and module-name as which get substituted in the sequential task. Additionally there is a args element which is also referenced in sequential task.

The following example shows how to use the macro:

<target name="compile-mymodule">
  <compile-module module-name="MyModule" source-dir="${source.dir}-mymodule">
    <args>
      <external-library-path file="${build.dir}/Core.swc" append="false"/>
    </args>
  </compile-module>
</target>

The macro compile-module is called with the attributes module-name and source-dir. All attributes with a default value can be left out, like build-dir. The args element is used to pass additional compiler arguments, like an external-library-path.

Macros are very useful for standardizing the build process. If you want to create link reports for all your modules and transform them using xslt for better readability, you can do all this in the macro definition.

The second part will cover macros for compiling libraries and creating runtime shared libraries.

1 comment:

  1. Nice tutorial.

    I have been working with modules on my Flex Projects during a long time. It's definitely essential to customize your Application and modules, beside the fact that allows the team to share the same build configurations.


    Follow below an example that I like to reuse on my projects.

    ReplyDelete