Skip to content

ANT and Flex

by Colin on May 4th, 2010

While writing my framework about 6 months ago, I wanted to create ASDocs for it so anyone using the framework would know how to work with it. I also love the idea of compiling all the source into a SWC file to make sending that source very easy to do. It's a very cumbersome process to create ASDocs and compile to a SWC through the executable files by itself, and upon research discovered ANT.
For those who don't know, ANT can be seen as the middle man between you and sending commands to a Java build tool. From there, you send ANT an XML file of the commands to run to the appropriate Java file. Sound easy? Heck ya it is.
I use Flex, however you can install ANT with any Eclipse based IDE. Please note, the majority of my build.xml is a combination of source I've found on multiple blogs, this post is the verdict of what I've learned with what I've found.
First, let's create out XML document. Title it "build.xml". The "project name" is the name reference you'll see in the ant window.

 
<project name="SWC and ASDoc Build" default="main" >

Second, we want to create variables within the script so it makes referencing files and folders easier.

<!-- CREATE THE VARS -->
<property name="FLEX_HOME" value="/Applications/Adobe Flex Builder 3/sdks/4.0.0" />
<property name="asdoc.exe" value="${FLEX_HOME}/bin/asdoc" />
<property name="project.path" value="/Users/Duffy/Desktop/Projects/Clients/INC/Framework" />
<property name="output" value="${project.path}/local" />
<property name="src" value="${project.path}/src" />
<property name="version" value="1.0" />
 
<taskdef resource="flexTasks.tasks" classpath="${FLEX_HOME}/ant/lib/flexTasks.jar" />
 
<!-- SWC Related -->
<property name="output.swc" value="${output}/swc" />
<property name="swc.title" value="duff" />
 
<!-- AS DOC Related -->
<property name="output.docs" value="${output}/asdocs" />
<property name="doc.title" value="Duff API v.${version}" />
 

Here you'll see I'm referencing where the .exe ("asdoc.exe") and .jar ("flexTasks.tasks") files exist on my machine so ant knows how to use the compiler. I also reference where my project is located, as well as the source folder ("src") and output folders("output.swc" and "output.docs").

Note
I have periods delimit spaces with my variables with ANT, however you can name them however you want: "outputSWC", "output.swc", "output-swc" as examples.

Next, I'm going to tell ANT what functions it'll need to run. First and foremost, I want ANT to not "think" about overwriting files, so I'll delete a directory, then recreate it to save it the trouble. After that's done, I'll have it create the docs and the swc.

 
<target name="main" depends="clean-asdoc-directory, create-docs, clean-swc-directory, generate-swc" />
 
<target name="clean-asdoc-directory" >
   <delete dir="${output.swc}" />
   <mkdir  dir="${output.swc}" />
</target>
 
<!-- runs the asdoc.exe compiler on the source -->
<target name="create-docs" >
    <exec executable="${asdoc.exe}" failonerror="true" >
    	<arg line="-doc-sources '${src}'" />
    	<arg line='-window-title "${doc.title}"'/>
    	<arg line="-main-title '${doc.title}'" />
    	<arg line="-output ${output.docs}" />
    </exec>
	<echo>ASDOC created successfully</echo>
</target>

The first function is "clean-asdoc-directory", which deletes the directory then recreates it. We'll use the same functionality for compiling the SWC. Next, we have it create the documentation by running our ASDoc executable file. Each "arg line" is an argument that's sent to the executable so it receives the required variables. As you can see about, I have many "${}" lines, which are references to the "property" (aka variables) we created above. We also want the executable build to fail if there's any problems at all.

Now, let's compile that swc.

<!-- deletes and recreates the swc directory -->
<target name="clean-swc-directory" >
   <delete dir="${output.swc}" />
   <mkdir  dir="${output.swc}" />
</target>
 
<!-- generate the swc -->
<target name="generate-swc" description="Compile the SWC file">
 
	<fileset id="sources" dir="${src}">
		<include name="**/*.as" />
	</fileset>
<pathconvert property="classes" pathsep=" " refid="sources">
		<chainedmapper>
			<globmapper from="${src}/*" to="*" />
<mapper type="package" from="*.as" to="*" />
		</chainedmapper>
	</pathconvert>
 
	<compc output="${output.swc}/${swc.title}.${version}.swc" include-classes="${classes}">
		<source-path path-element="${src}" />
	</compc>
	<echo>SWC created successfully</echo>
</target>
</project>

The beauty about this "generate-swc" method is that it compiles all .as files within your "src" directory, whereas normally you would either have to manually input each class or each package.
You can download the final result here.

From → Flex

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS