Using ANT to build an Application
Building a Flex project with an ANT build file is not a skill that every Flex developer will need, but once you get into Enterprise Application Development it really becomes a necessity. The interconnection between developers, system admins, and managers (that may or may not know anything) is a complicated beast to keep up.
It is wonderfule to be able to use ANT to build the entire enterprise application without you – the Flex dev – always having to be present.
If you have worked with ANT and building Flex Apps with ANT before this may be very simple for you, if you haven’t, this will get you right into it very easily. Over the next few weeks plan to see many more installments into this series, ending with an entire Continuous Integration Stack suitable for any Enterprise Environment.
Let’s Begin
Your application is made, now you need to make a build file to be run by ANT. My application being shown is really simple, just a Label in the Application, nothing special. What we are creating is just an XML file that gives basic commands to ANT that will compile your application just as if you were compiling your application in the Flash/Flex Builder.
First, the App
I know, completely blew your mind right?
Defining the ANT Build Files
My ANT setup includes two files, the first build.xml. Most people name their ANT build.xml, so there is nothing too special here. This build file includes all the targets and tasks to tell the ANT compiler what steps to take and in what order.
The second file is the properies file, labeled build.properties. This is a file full of properties to be used by the build file to run the build script. Traditionally this file is not included in your repository and unique to each computer. This makes it easier so that each computer can have their own properties without overriding other’s configurations. Also, you will notice some crazy names in this properties file, that is because you can use almost any naming syntax that you want in this file. I prefer to use a simple naming style such as name.type, for example: debug.boolean, or deploy.dir. A quick way to let people know what the property is describing. My main point is that these properties and their naming isn’t eluding to another sort of syntax and nothing that should freak you out at all.
Setting up the ANT Build Files
As mentioned previously, an ANT file is just an XML file, and an ANT file all starts with a project tag.
1 2 3 | <?xml version="1.0" encoding="UTF-8"?> <project> </project> |
We will now add a bit more information to our XML. The project name (shown in outliner), the base directory (to determine relative linkage to the build file), a description (so future devs can understand what you are doing), and finally the link to the build.properties (to include the build properties). I also include a timestamp property so I can access it easier later for timestamped build files. You’ll notice that I use “.” for the baseDir, this states that the current directory level is the base directory level.
Our file now looks like…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?xml version="1.0" encoding="UTF-8"?> <project name="Build File" basedir="."> <!-- file description --> <description>Build Script</description> <!--location of property file --> <property file="./build.properties" description="properities for builds" /> <!-- timestamp --> <tstamp> <format property="timestamp" pattern="yyyyMMdd" /> </tstamp> </project> |
FlexTasks
Now we need to include the FlexTasks jar to include the builder functionality provided by Adobe. To include this jar is a very simple matter, just add the taskdef.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?xml version="1.0" encoding="UTF-8"?> <project name="Build File" basedir="." default="compileProject"> <!-- file description --> <description>Build Script</description> <!--location of property file --> <property file="./build.properties" description="properities for builds" /> <!-- timestamp --> <tstamp> <format property="timestamp" pattern="yyyyMMdd" /> </tstamp> <!-- additional tasks --> <taskdef resource="flexTasks.tasks" classpath="${FlexTasks.file}" /> </project> |
Adding this one line allows for the use of the mxmlc and compc functions in ANT. You will also notice rather than a direct path I wrote ${FlexTasks.file}, this is an ANT variable that corresponds to a variable in the build.properties, specifically:
1 | FlexTasks.file=flexTasks/flexTasks.jar |
Targets
Now we need to define the steps that ANT will take to process our application. These steps are also called targets and our targets can be run via the outliner or your own ANT running GUI.
The main step that I am going to want ANT to accomplish is compileProject. This target has some subtasks or depends which are to clean the deploy file and compile the application. Below is my compileProject target.
1 2 3 | <target name="compileProject" depends="cleanDeploy,compileApplication" description="compiles application"/> |
Now we need to actually define the targets that compileProject depends on.
Clean and Create
The clean and create step is simple. We delete the old deploy folder and create a new one. This ensures a clean build.
1 2 3 4 5 6 7 8 | <target name="cleanDeploy" description="Cleans the deploy file"> <echo>Deleting Deploy Directory...</echo> <delete dir="${Deploy.dir}" failOnError="false" includeEmptyDirs="true" /> <echo>Deleted Deploy Directory</echo> <echo>Creating Deploy Directory...</echo> <mkdir dir="${Deploy.dir}" /> <echo>Created Deploy Directory</echo> </target> |
Again I am referring to some ANT variables, but you can see that I am deleting and making the deploy directory. One new tag you may not know as a Flex/Flash dev is the echo tag. This just writes out a string to the console as it is being run. Basically, we are just keeping other devs aware of what is going on in case the ANT file craps out.
Compile The Application
The only task we have left is to actually build our application. For this step I am going to use the mxmlc compiler provided by Adobe via the FlexTasks.jar. Here I will load up the MXML application file, set its output, give the application a few properties, load in the Flex config file provided by Flex itself, add metadata, and finally compile in the source. The ANT code to accomplish this is shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <target name="compileApplication"> <echo>Compiling SWF To Deploy</echo> <mxmlc file="${Src.dir}/${Application.name}.mxml" incremental="false" actionscript-file-encoding="UTF-8" output="${Deploy.dir}/${Application.name}${timestamp}.swf" debug="${Debug.Boolean}" keep-generated-actionscript="false"> <load-config filename="${FLEX_HOME}/frameworks/flex-config.xml" /> <default-background-color>0xFFFFFF</default-background-color> <metadata> <creator>Jonathan Campos</creator> <publisher>UnitedMindset</publisher> <language>EN</language> </metadata> <compiler.source-path path-element="${Src.dir}" /> </mxmlc> <echo>Compiled SWF To Deploy</echo> </target> |
Expert Note:
One quick way to know the compiler options Flex/Flash Builder uses is to add this tag into your compiler options:-dump-config your/drive/to/store/the/xmlfile.xml
This will output the entire ant compiler options and you can copy off the parts you want to keep.
Default Task
One last thing I like to do is to make sure to add a default target to my application. This makes it so you can select to run the ANT file and the ANT file knows which target to run by default. The change to my project tag looks as such:
1 2 3 4 | <?xml version="1.0" encoding="UTF-8"?> <project name="Build File" basedir="." default="compileProject"> ... </project> |
The Application
In case you didn’t do it earlier, you can check out all the files I have via view source with the included Flex file.
FLEX_HOME
Make sure to include in your build file a reference titles “FLEX_HOME” and point it to a copy of the Flex SDK, this is required to build the Flex Application.
Memory Issues
If your build file keeps failing complaining about “not enough memory” there is one little change you will need to make to your JVM. Go to Run > External Tools > Open External Tools Dialog. Select your ANT File, the switch the tabs to JRE and look for VM Arguments. Make sure your options include (at least) -Xmx1024m. This allows for enough RAM to be dedicated to the VM during the build process.






Good introduction to ant! A note regarding FLEX_HOME, for enterprise development I have found that it’s helpful to store it as an environment variable of the OS and then reference it in the ant script:
This has worked on Windows, Mac OS and UNIX so you don’t have to have separate build scripts depending on where you are building the project.
That is a good point. I have found that there are a fair amount of people that don’t want to / feel comfortable getting into their environment variables. That’s why I lean more to including the FLEX_HOME in the build properties.
[...] post is in continuation from my last post: Using ANT to build an Application. So hopefully you read that last post or already have a working knowledge of ANT as we start [...]
[...] building off the first post in this series: Using ANT to Build an Application, we are going to add another bit of functionality to our build.xml. This time, we will be building [...]
[...] we have two ANT build files, one for the project and one for the library. What if we want the build file to build the library, store the swc, and [...]
how to make two build files correlated to one another during build in ant…?
@RAAJ
2
3
<ant antFile="${YOUR_OTHER_ANT_FILE}"/>
</target>
HTH
I am having three build(build.xml) which contains one jar file in each build file and there is one master build file(build.xml).In three build file second jar file(second build.xml) is dependent on third jar file(third build.xml)
@Raaj, part of the ant task is the ability to inheritRefs.
hi,
I am having one master build file and sub project build files i.e one.xml,two.xml,three.xml….the main aim of these three sub build file is to generate jar files,but the thing is while i creating jar file in two.xml file but this file is dependent on three.xml(before compiling the class files in two.xml i want compile some file in three.xml) and vice versa also…that is circular dependency..but i dont no what exactly it is and i also dont no about project dependency…..
package1 and package2 are two packages. To compile the files in package1 I need the files in package2 already compiled. But for the compilation of package2 files I need the files in package2.
What I will do is comment out the code in one package(package1) that has cyclic dependency, jar it. Use the jar to compile the second(pacakge2). Then again recompile package1 by uncommenting the code. But for dynamic building this is giving me problems as I don’t want to comment and uncomment the code manually. Please gimme the solution. Thanks in advance.