I've been working w/ANT the last few days, trying to get it to compile a flex app that makes heavy use of Cairngorm, FlexLib, and Flare. All these libraries deliver swc files that can be compiled into the application. This is very easy to do with FlexBuilder, but not so simple for the n00b when trying it out for the first time.
First of all, the mxmlc ant task is poorly documented; apparently, we are supposed to refer to the mxmlc documentation. Fair enough. However, it is not-exactly trivial when the syntax changes ever so slightly.
The first issue I had was getting the .swc files to compile in. The example lists the following:
<!-- List of SWC files or directories that contain SWC files. -->
<compiler.library-path dir="${FLEX_HOME}/frameworks" append="true">
<include name="libs" />
<include name="../bundles/{locale}" />
</compiler.library-path>
My mistake was trying to add my swc files here. See the dir attribute? That refers to flex's interal swc repository. I, unfortunately, was thrown by <include name="libs" />, which I took to mean my project/libs/ directory. I obediently added all my include files here, and fought compiler errors for the next few hours.
Turns out, if you want several swc files from multiple directories, you need to represent each directory tree in its own <compiler.library-path .../> block. My code ended up looking like the following:
<compiler.library-path dir="${flex.sdk.dir}/frameworks/libs" append="true">
<include name="*" />
<include name="../bundles/{locale}" />
</compiler.library-path>
<!-- these are our library swcs (w/a different home directory) -->
<compiler.library-path dir="@{libDirectory}" append="true">
<include name="cairngorm" />
<include name="flexlib" />
<include name="flare" />
</compiler.library-path>
<compiler.library-path dir="@{sourceDirectory}" append="true">
<include name="flexspy" />
</compiler.library-path>
Hopefully, somebody will be saved a lot of time. For reference, here is the macro I am using for compilation:
<macrodef name="macro.mxmlc.compileWithFlexAnt">
<attribute name="sourceDirectory" default="${src.dir}" />
<attribute name="libDirectory" default="${basedir}/libs" />
<attribute name="in" />
<attribute name="out" />
<attribute name="config" default="${flex.config.xml}" />
<attribute name="additional" default="" />
<attribute name="defaultWidth" default="800" />
<attribute name="defaultHeight" default="600" />
<attribute name="debug" default="false" />
<attribute name="incremental" default="false" />
<sequential>
<mxmlc
file="@{in}"
output="@{out}"
actionscript-file-encoding="UTF-8"
keep-generated-actionscript="false"
incremental="@{incremental}"
debug="@{debug}"
>
<!-- Get default compiler options. -->
<load-config filename="@{config}" />
<!-- List of path elements that form the roots of ActionScript class hierarchies. -->
<compiler.source-path path-element="${flex.sdk.dir}/frameworks"/>
<compiler.source-path path-element="@{sourceDirectory}"/>
<!-- List of SWC files or directories that contain SWC files. -->
<!-- these are the built-in flex swcs -->
<compiler.library-path dir="${flex.sdk.dir}/frameworks/libs" append="true">
<include name="*" />
<include name="../bundles/{locale}" />
</compiler.library-path>
<!-- these are our library swcs (w/a different home directory) -->
<compiler.library-path dir="@{libDirectory}" append="true">
<include name="cairngorm" />
<include name="flexlib" />
<include name="flare" />
</compiler.library-path>
<compiler.library-path dir="@{sourceDirectory}" append="true">
<include name="flexspy" />
</compiler.library-path>
<!-- Set size of output SWF file. -->
<default-size width="@{defaultWidth}" height="@{defaultHeight}" />
</mxmlc>
<echo>Compiled @{in} to @{out}</echo>
</sequential>
</macrodef>
Next step, RSLs. :-(
Jun 26, 2008 at 10:21 AM Thanks!
Jul 15, 2008 at 1:48 AM *hugs*
I've wasted so much damned time with these ant tasks.
Sep 16, 2008 at 12:12 PM I made exactly the same mistake. Thank you!
Oct 20, 2008 at 9:21 AM Thanks, saved me lots of time and swearing!
Jan 2, 2009 at 6:35 AM Hi,
I was struggling to do similar things but with use of Groovy and Ant builder - it wasn't trivial but the results are pretty the same as yours:
http://tech4web.blogspot.com/2008/12/mxmlc-ant-flex-task-in-groovy.html
Apr 13, 2009 at 7:21 AM god bless!
May 12, 2009 at 12:38 PM God bless you!
Jun 14, 2009 at 5:19 AM Yeah, the example is misleading. Thank you, you certainly saved me some time. Cheers!
Jul 23, 2009 at 2:19 AM Thanks for this!
Oct 1, 2009 at 2:36 AM Thanks a lot for this :) saved lot of my time.. though i had already wasted few hours bugging this error.
Feb 22, 2010 at 5:56 PM Hi. I'm having this problem and at the risk of sounding like a noob I don't get you solution. do I add those lines to my build file? To the main ant build file? Where do I put the compiler.library-path?
Feb 23, 2010 at 1:50 PM @Eric:
You add those files to the build file (the .ant file).
Mar 5, 2010 at 4:54 PM is there any way to use the swc file but avoid inserting the entire swc file to the final swf file?? make sense ?
I was experimenting with a project that just compiles a simple sprite class.. but when using a swc file with the build.. the swf file was a lot bigger in kb..
thanks!!
Mar 6, 2010 at 10:46 AM @franzco: the swc file is compiled into the resulting swf. Try looking into rsl files instead to keep the functionality in a seperate file for deployment.
Apr 15, 2010 at 3:00 AM Hiya Scott, hopefully you can help me with a quick query: if you have a swc that contains an importable class with the same name as something you have in your source code structure (say they both offer com.client.project.IntroPage) can you get the compiler to choose the SWC version over the .as version?
The reason we'd want to do it here is that you're compiling the source code into the swc, and then compiling an application from your source code that needs something from the swc (it has added graphic attributes to your class via the "Class: " export).
Ta!!
Apr 15, 2010 at 10:15 AM @bakedbeing: I see what you are trying to do, but I don't know of a way (other than inheritance or injection) to pull off what you are trying to do. Really, you are essentially providing the class multiple times. I'm not sure what the compiler rules are around that. Seems like there should be a better design approach.
That said, maybe look into custom namespaces? You might be able to use the custom namespace in the swc, and then reference it in the code. I've never tried this, but it is worth a shot.
Apr 29, 2010 at 4:34 AM I don't think i need these compiler options in ant task. Because if we are mentioning compiler options their then what is the purpose of config file which we are mentioning the the load-config option?
We can easily optimize this through config file. Set all compiler options in config file and then just give that config file in ant task.
Jul 13, 2010 at 1:31 PM Is there a way for Flex builder to recognize the dependencies in the build file so you can use Flex builder without having to add all the dependencies to your project again for a second time?
Jul 13, 2010 at 3:57 PM @Peder: Not that I know of. I think for that sort of thing, you may need something more sophisticated like Maven. Check out Flex Mojos as a possibility.
Apr 27, 2011 at 6:23 AM Can you please tell me how to compile locale in ANT
Dec 15, 2011 at 5:22 AM Oh this was REALLY useful. Thank you!
Jan 10, 2012 at 9:20 PM Wait, I cannot fatohm it being so straightforward.