Using Ant to compile Flex Projects
In this post I am going to explain to you that how you can use Ant to compile your flex projects using the Flex Builder 3.
To know about how to enable the support for Ant in Flex Builder please click here
Step 1 - Create a new Flex project
Ok, so first we will create a new flex project and then will create a really simple mxml application. Here is what I created
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
<mx:Label text="Hello World!"/>
</mx:Application>
So now my entire project is having only one mxml application, which is located under the src folder.
Step 2 - Keep the FlexTasks.jar file inside your project directory
The FlexTasks.jar file can be found at the following location on your machine:
[Flex Builder Install directory]\sdks\3.0.0\ant\lib
Copy the jar file (flexTasks.jar) from there and place it under the libs folder inside your flex project.
Step 3 - Create build.xml file
Under the root folder of your project, create new file using the File -> New… -> File menu.
name this file as ‘build.xml’
Step 4 - Define the project in the build.xml
Add the following inside your empty build.xml file:
<?xml version="1.0" encoding="utf-8"?>
<project name="My App Builder" basedir=".">
</project>
This defines the name of the project and sets the base directory as the root directory of the project
Step 5 - Add the taskdef tag
Inside the project tag, add the following tag:
<taskdef resource="flexTasks.tasks" classpath="${basedir}/libs/flexTasks.jar"/>
This node defines that where is the flexTasks.jar located.
Note that we are using a classpath which is relative to the base directory which we defined in the previous step. In my project I have placed this jar under the libs folder. If you wish to keep your flexTasks.jar file elsewhere, you must give the appropriate path here.
Step 6 - Adding the properties
In this example we are going to define only three main properties, namely, FLEX_HOME, APP_ROOT, and DEPLOY_DIR, in which we are going to define the paths containing the Flex frameworks directory, the directory which our application is residing, and, directory where the output will be placed, respectively. Here is how these properties tag look:
<property name="FLEX_HOME" value="C:\Program Files\Adobe\Flex Builder 3\sdks\3.0.0"/>
<property name="APP_ROOT" value="src"/>
<property name="DEPLOY_DIR" value="D:\output"/>
Note: Modify the value of the FLEX_HOME accordingly if your installation path of Flex builder 3 is different.
At this point of time, your build.xml should look like the following:
<project name="My App Builder" basedir=".">
<taskdef resource="flexTasks.tasks" classpath="${basedir}/libs/flexTasks.jar"/>
<property name="FLEX_HOME" value="C:\Program Files\Adobe\Flex Builder 3\sdks\3.0.0"/>
<property name="APP_ROOT" value="src"/>
<property name="DEPLOY_DIR" value="D:\output"/>
</project>
Step 7 - Adding the target
Now we will add the target node as following, below the properties:
<target name="main">
</target>
This target name can be anything that you prefer. It is just a name that we are assigning to a target that can be executed using Ant.
Now, within this target, we will add the necessary properties for compiling using the mxmlc node, which looks like following:
<mxmlc file="${APP_ROOT}/Main.mxml"
output="${DEPLOY_DIR}/Main.swf">
<load-config filename="${FLEX_HOME}/frameworks/flex-config.xml"/>
<source-path path-element="${FLEX_HOME}/frameworks"/>
</mxmlc>
In these nodes we are specifying the following four things:
- The name and location of the application file, using the file attribute of mxmlc node
- The name and location of the output swf file, using the output attribute of the mxmlc node
- The file name and location of the flex-config,xml file
- The source path to flex frameworks directory using the path-element attribute and source-path node
Note that we are using the properties that we defined in step 6
And that’s it. This script can now compile the application. This how my final build.xml looks:
<project name="My App Builder" basedir=".">
<taskdef resource="flexTasks.tasks" classpath="${basedir}/libs/flexTasks.jar"/>
<property name="FLEX_HOME" value="C:\Program Files\Adobe\Flex Builder 3\sdks\3.0.0"/>
<property name="APP_ROOT" value="src"/>
<property name="DEPLOY_DIR" value="D:\output"/>
<target name="main">
<mxmlc file="${APP_ROOT}/Main.mxml"
output="${DEPLOY_DIR}/Main.swf">
<load-config filename="${FLEX_HOME}/frameworks/flex-config.xml"/>
<source-path path-element="${FLEX_HOME}/frameworks"/>
</mxmlc>
</target>
</project>
Step 8 - Run As Ant Build
Save the build.xml file.
Right click on the build.xml file and from the context menu point to ‘Run As’ and select ‘Ant Build’
And you should see the messages from Ant Build in the console window. After it says that build is successful, you can open the output directory and launch the swf file.
Further Reading – Generating the html wrapper using Ant
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.
Comments
Thanks Taryn,
This is the first readable guide on Flex-Ant. Adobe could learn something by your example.
Hi Taryn,
Your sample build.xml worked great but I notice that my output swfs did not end up in the DEPLOY_DIR, but in the usual bin-debug instead.
Am I missing something?
Thanks
Hi Taryn,
I have a problem with compilation. I compile around 11 mxml files in my project. During this compilation my CPU goes to 100% is not usable, when I open my task manager there are around 11 mxmlc processes running. Also it takes around 8 minutes to compile. Is there a way to speed up the process?
Thanks,
Amar
@Ged Mc
Thanks for the appreciation Ged. For me the output swf gets deployed to the DEPLOY_DIR path only. Do you see the following traces in the console panel mentioning the output?
main:
[mxmlc] Loading configuration file C:\Program Files\Adobe\Flex Builder 3\sdks\3.0.0\frameworks\flex-config.xml
[mxmlc] D:\output\Main.swf (152332 bytes)
BUILD SUCCESSFUL
Total time: 8 seconds
If yes, then everything is fine, the files you see in the usual output folder are probably old ones. And if the output is not shown as the path that you are giving in the build.xml, probably you missed something.
@Amar,
I believe that it is a problem that many flex developers right now are facing, actually it is a memory leak issue in the compiler, I will recommend that you should log on and vote for the memory leak bug at the flex bugs site, here is the link: http://bugs.adobe.com/jira/browse/FB-13781. And to speed up the process a little, you can uncheck ‘copy non-embedded files to output folder’ - but well, it will only marginally improve the performance if you do not have many non embedded resources. Also, if you are having embedded fonts/other assets, you can try to avoid it.. these are more like my guesses.. to give a definitive answer, it will take some time for me… if you happen to find a better solution, do share it.
Does anyone know how to include an external swc file inside build.xml?
I don’t know what the syntax is!
Thanks guys.
[...] to my post of compiling flex projects using Ant script, in this post I am going to tell that how you can generate the html wrapper file for the swf that [...]
[...] everything worked as intended. And now that I know what to look for (sigh) I’ll point you to these step-by-step instructions! March 26th, [...]
Great example thanks for sharing! But it’s missing something that drove me nuts today!
I ended up with this script:
<!– –>
<!– –>
<!– –>
which i ran time and time again using 2 different pc’s and it just never worked. I thought for a long time flexTasks.jar was corrupt (amoungst other things!). I guess i must have missed something like adding a custom compiler argument in Flex but anyway running the script above did nothing until i added a default attribute to the project node:
Et voila it finally worked!!!
Perhaps you could change your post to show how you get Ant to associate the target with the build data as at the moment there is no association - leaving me and a few others probably scratching our heads for a good few hours.
p.s. there a really nice article and example here:
http://www.adobe.com/devnet/flex/articles/flex_ant_pt1_print.html
peace
sorry i got parsed!
Here’s my scripts:
BEFORE -> http://www.dougstudio.co.uk/temp/build_does_nothing.xml
AFTER -> http://www.dougstudio.co.uk/temp/build_does_work.xml
with the only difference being the addition of the default=”main” attribute to the project node.
I hope this helps someone as possibly @Ged Mc had the same issue - as is the Ant script will complete without throwing any errors as it never sees the target node.
Thanks very much for this clear, helpful example.
I note that one also must have properly installed flextasks.jar into the lib dir of one’s ant installation, per the destructions in the flextasks readme file.
Hi,
I’d appreciate some help with an error I can’t get rid of.
Compiling a mxml file in Flex Builder IDE works fine, but from ANT the compiler seems to have problems finding components provided by Flex.
If I run the ANT script with the HelloWorld example used here, it works, but when I try to run the script replacing the HelloWorld example with my mxml, it doesn’t.
Error message is:
Error: Could not resolve to a component implementation.
Any idea about how to tell the compiler where to find whatever needed?
Thanks a lot
Forget my question and sorry about that.
Flex Builder IDE SDK includes datavisualization_sdk3.3.
Flex SDK doesn’t. It has to be downloaded.
Issue fixed.
Is it possible to use flex debugger features such as breakpoints when using an ant script to compile the application and generate the html wrapper?
Does anybody know why the swf file is bigger when compiled by Ant, as opposed to when it’s done by the Flex Builder. It is even smaller (1/2 the size) when I use the “Export Release Build” option.
I am using optimize=”true” and debug=”false” in the mxml task, but it does not seem to be enough.
hi
can you tell me how to include/compile the modules when we compile the main application using ant task?


[...] - using ant to compile flex projects saved by LaraKid2008-10-24 - dispatchEvent " Embed Almost Anything in Your SWF saved by [...]