Wednesday, February 13, 2013

Ant and FDT

I've been using Apache Ant to automate my projects for probably a few years now. I am not an Ant expert by any means. I know enough to automate my build:

  • Compiling a swf for different environments (development, production, etc.)
  • Copying files to web server
  • Replacing tokens
  • Running the app in the browser
It's rather basic stuff. And my editor of choice is FDT. Ant and FDT work nicely together and FDT even has some special Ant tasks which you can use from FDT. I prefer using these tasks as they perform very well. Better than the Flex mxmlc when it comes to compiling. Plus the FDT versions are much easier to work with. But clients usually want to be able to run the Ant builds themselves. And they don't always have FDT to build with. They may have to run Ant from the command line. So in the past I usually had two versions of certain tasks. One that uses the FDT specific Ant tasks and one which uses the mxmlc Ant tasks.


I continually try to improve myself and my coding practices. So I decided to see if I could do some conditional stuff in Ant rather than forcing myself and users to choose specific tasks depending on the environment they are building from. One task to compile them all.

Ant to check if in FDT


First I needed to see if I was building from FDT or not. I figured that I could check to see if any of the FDT specific Ant tasks were available. After searching around, I found that I could use the condition Ant task and the typefound Ant task.

<!-- Determine if FDT's special Ant commands are available. -->
<condition property="isFDT">
 <typefound name="fdt.loadProjectProperties" />
</condition>

The code above sets a property isFDT to true if Ant has found the fdt.loadProjectProperties task (I probably could have checked for any FDT Ant task, I just happened to choose this one). If the specified task is not available, then isFDT will not be set to true.

Conditional compiling

Now I want one task which will compile the swf using the FDT Ant task if building from FDT and use the mxmlc in all other cases. So here is my one task which will try to run both the FDT and mxmlc compile tasks:

<target name="Compile">
 <antcall target="Compile App MXMLC" />
 <antcall target="Compile App FDT" />
</target>

Now I don't want both to run. The way to have one run and not the other is to use the if and unless attributes of the target tag:

<!-- Compile the app using the Flex mxmlc compiler -->
<target name="Compile App MXMLC" unless="isFDT">
 <!-- MXMLC stuff in here -->
</target>

<!-- Compile the app using the FDT compiler -->
<target name="Compile App FDT" if="isFDT">
 <!-- FDT stuff in here -->
</target>

So the if basically says if the isFDT property is true then run this task. The unless is saying don't run this task if isFDT is set to true.

I don't know if this is the best way of doing this, but it seems rather simple and works very well.

No comments: