In Loki We Trust The many projects of Lokkju, Inc

participate
home
25Aug/100

Supporting dynamic FlexFileSets in Flex’s compc and mxmlc Ant tasks

If you are using the mxmlc and compc tasks in Ant to compile flex code, there is no documented way to make the fileset-like children accept a dynamic include pattern - that is, one you set based on conditionals.

In my case, I need to have a list of included libraries in my build.properties, and only include those ones in my compc task. The solution is to use patternsets and a custom ant macro. See the source below, but essentially you do the following:

  1. Create a new patternset, assigning it an id
  2. Use the append.to.patternset macro to add a new patternset for each pattern in your list of library patterns (as defined in you build.properties, or dynamically, or...)
  3. assign that patternset to the library-path of compc



Important: All the patterns must decend from the same root directory as set in the library-path. If you need multiple root directories, you must use multiple library-path directives and multiple patternset refids.


build.xml:

<?xml version="1.0" encoding="utf-8"?>
<project name="My Component Builder" basedir=".">
  <taskdef resource="flexTasks.tasks" classpath="${basedir}/flexTasks/lib/flexTasks.jar" />
  <property file="build.properties"/>
  <property name="FLEX_HOME" value="C:/flex/sdk"/>
  <property name="DEPLOY_DIR" value="c:/jrun4/servers/default/default-war"/>
  <property name="COMPONENT_ROOT" value="components"/>
  
  <macrodef name="append.to.patternset">
    <attribute name="patternset"/>
    <element name="nested" optional="yes" implicit="true"/>
    <sequential>
      <patternset id="tmp">
        <patternset refid="@{patternset}"/>
        <nested/>
      </patternset>
      <patternset id="@{patternset}"><patternset refid="tmp"/></patternset>
      <patternset id="tmp"/>
    </sequential>
  </macrodef>
  <patternset id="compc.library-path" />
  <for list="${compc.libraries}" param="lib">
    <sequential>
      <append.to.patternset patternset="compc.library-path">
        <patternset>
          <include name="@{lib}" />
        </patternset>
      </append.to.patternset>
    </sequential>
  </for>
  <target name="main">
    <compc 
      output="${DEPLOY_DIR}/MyComps.swc"
      include-classes="custom.MyButton custom.MyLabel">
      <source-path path-element="${basedir}/components"/>
      <include-file name="f1-1.jpg" path="assets/images/f1-1.jpg"/>
      <include-file name="main.css" path="assets/css/main.css"/>
      <library-path append="true" dir="${compc.libdir}">
        <patternset refid="compc.library-path" />
      </library-path>
    </compc>
  </target>
  <target name="clean">
    <delete>
      <fileset dir="${DEPLOY_DIR}" includes="MyComps.swc"/>
    </delete>
  </target>
</project>

build.properties:

compc.libdir=${rootdir}/libs/
compc.libraries=lib1.swc,plib*.swc,**/type.swc
podcast
   
trademarks
e-mail