Products
Technologies

Developer resources

Compiling Flex libraries with Ant

Not yet rated.

Problem

Compile Flex library with Ant using .flexLibProperties to know which files to include.

Solution

Create a configuration file and add the classes/assets in it using an xsl formatting of .flexLibProperties file.

Detailed explanation

Here's a small trick to get Flex Libraries (SWC) compiled with ant using the .flexLibProperties file.

1) Make a first compilation of your library in the Flex IDE using the "-dump-config myconfigfile.xml" argument in the compiler options.
2) Check the dumped file for errors (or for tweakings). I'll put mine as an example.

3) Compiling with classes :
3.1) The config
In the myconfigfile.xml created before, add the following lines after the <compiler></compiler> xml tag :

<include-classes>
$include.classes$
</include-classes>


The $include.classes$ variable can be replaced by anything you want. It must be unique in the file.

3.2) The Ant File :

<target
name="update-libs-build-config">
<xslt
in=".flexLibProperties"
    out="build-config.xml"
style="config_classes.xsl">
</xslt>
<loadfile property="embed-classes" srcfile="build-config.xml"/>
<copy file=" myconfigfile.xml" tofile="config_libs.xml"/>
<replace file="config_libs.xml" token="$include.classes$" value="${embed-classes}"/>
</target>


Explanations :
We first apply an XSL file to transform .flexLibProperties into a suitable set of <class>MyClass</class> tags and put the result in a file (here build-config.xml).
We then read the created file and replace put its content into an Ant property.
We make a copy of the myconfigfile.xml : the replacement of the $include.classes$ would only work once if we don't make a copy of this file.
We then replace the $include.classes$ in the copy of myconfigfile.xml by the file's content.

3.3) Compilation
When this is done, the only thing to do is to put the config_libs.xml in the load-config filename argument of the compc Ant Task.
Example :

<compc output="bin/Libs.swc">
    load-config filenameconfig_libs.xml"/>
    <include-sources dir="src/" includes="*.mxml"/>
    <include-sources dir="src/" includes="*.as"/>
</compc>



The same thing can be done with Assets :
4) Compiling with assets
4.1) The config
In the myconfigfile.xml created before, add the following lines after the <compiler></compiler> xml tag :

$include.files$
The $include.files$ variable can be replaced by anything you want. It must be unique in the file.

Change the config_assets.xsl with the complete path to your assets directory.

4.2) The Ant File :

<target
name="update-libs-build-config">
<xslt
in=".flexLibProperties"
    out="build-config.xml"
style="config_assets.xsl">
</xslt>
<loadfile property="embed-classes" srcfile="build-config.xml"/>
<copy file=" myconfigfile.xml" tofile="config_libs.xml"/>
<replace file="config_libs.xml" token="$include.files$"
" value="${embed-classes}"/>
</target>



Explanations :
We first apply an XSL file to transform .flexLibProperties into a suitable set of

<include-file>
<name>name</name><path>path</path>
</include-file>


tags and put the result in a file (here build-config.xml).
We then read the created file and replace put its content into an Ant property.
We make a copy of the myconfigfile.xml : the replacement of the $include.files$
would only work once if we don't make a copy of this file.
We then replace the $include.files$
in the copy of myconfigfile.xml by the file's content.

4.3) Compilation
When this is done, the only thing to do is to put the config_libs.xml in the load-config filename argument of the compc Ant Task.
Example :

<compc output="bin/Libs.swc">
    <load-config filenameconfig_libs.xml"/>
    <include-sources dir="src/" includes="*.mxml"/>
    <include-sources dir="src/" includes="*.as"/>
</compc>



A combination of the two systems would be easy to do and take care of all the cases.

XSLT Files :
config_classes :

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:template match="/">
        <xsl:apply-templates select="node()|@*" />
    </xsl:template>
   
    <xsl:template match="node()|@*">
        <xsl:apply-templates select="node()|@*" />
    </xsl:template>
   
    <xsl:template match="classEntry">
        <class>   
            <xsl:value-of select="@path"/>
        </class>
    </xsl:template>
   
</xsl:stylesheet>



The config_assets.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:template match="/">
        <xsl:apply-templates select="node()|@*" />
    </xsl:template>
   
    <xsl:template match="node()|@*">
        <xsl:apply-templates select="node()|@*" />
    </xsl:template>
   

    <xsl:template match="resourceEntry">
        <include-file>
            <name><xsl:value-of select="@destPath"/></name><path>c:/MyWorkspace/MyProject/src/<xsl:value-of select="@sourcePath"/></path>
        </include-file>
    </xsl:template>
   
</xsl:stylesheet>


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes