2
Nov

Compile Flex applications using ANT

A quick setup and example of how to compile a Flex application using ANT. This example will show the basics of how to run the ANT script to compile your flex application, I will walk through more complex examples in later posts but to get started look through the script and properties files below or download the attached project files at the bottom of this post.

ANT is a very powerful tool that, once you get into using it, will make your development, deployment and automation a lot simpler as well a lot less prone to error. If you are new to ANT there are a lot of resources out on the web that will help you get up to speed. This post and others to follow will focus specifically on the uses of ANT with Flex in a CI build process.

Ant Script

<?xml version="1.0" encoding="utf-8"?>
<project name="FlexANTBuildSample" default="build" basedir=".">
 
	<!--
	DEFINE import our build properties file and define our tasks
	-->
	<property file="./build.properties" />
 
	<path id="project.classpath">
		<fileset dir="${tools.dir}">
			<include name="**/*.jar"/>
		</fileset>
	</path>
 
	<taskdef resource="flexTasks.tasks" classpath="${flexTasks.jar}" />
	<taskdef resource="com/adobe/ac/ant/tasks/tasks.properties" classpath="${flexAntTasks.jar}"/>
	<taskdef resource="net/sf/antcontrib/antcontrib.properties" classpathref="project.classpath"/>
 
 
	<!--
	TARGETS
	-->
	<target name="build" depends="init, clean, makeWrapper, compile"/>
 
 
	<!--
	INITIALIZE
	-->
	<target name="init" description="Initializes the build">
		<condition property="environment.display" value=":1">
			<os name="Linux"/>
		</condition>
		<tstamp/>
		<echo message="============================================="/>
		<echo message="${project.name}-${project.version} [${TODAY}]"/>
		<echo message="Copyright (c) ${project.year} ${project.owner}"/>
		<echo message="OS : ${os.name}" />
		<echo message="Author: ${author}" />
		<echo message="=============================================="/>
	</target>
 
 
	<!--
	CLEAN the existing output folder and files and then re-generate the output folder
	-->
	<target name="clean" description="deletes and recreates the metadata destination directory">
		<delete verbose="${verbose}" dir="${deploy.dir}"/>
		<mkdir dir="${deploy.dir}"/>
		<echo message="Deploy directory created" />
	</target>
 
 
	<!--
	COMPILE
	-->
	<target name="compile" description="compiles the Main app" >
		<mxmlc context-root="main"
			file="${mainApp.src}"
			output="${mainApp.swf}"
			actionscript-file-encoding="${encoding}"
			keep-generated-actionscript="false"
			incremental="false"
			locale="${locale}">
 
			<load-config filename="${FLEX_HOME}/frameworks/flex-config.xml"/>
 
			<compiler.source-path path-element="${FLEX_HOME}/frameworks"/>
			<compiler.source-path path-element="${srcpath.dir}"/>
			<compiler.library-path dir="${FLEX_HOME}/frameworks" append="true">
				<include name="libs" />
			</compiler.library-path>
			<compiler.library-path dir="${basedir}" append="true">
				<include name="libs" />
			</compiler.library-path>
		</mxmlc>
	</target>
 
 
	<!--
	MAKE WRAPPER
	-->
	<target name="copyTemplates">
		<copy todir="${deploy.dir}">
			<fileset dir="${htmltemplate.dir}">
				<exclude name="**/index.template.html" />
			</fileset>
		</copy>
	</target>
 
	<target name="makeWrapper" description="copies index.html wrapper and needed files from html-template directory" depends="clean, copyTemplates">
		<property name="pageTitle" value="${mainApp.name}"/>
		<copy file="${htmltemplate.dir}/index.template.html" tofile="${deploy.dir}/index.html" />
		<replace file="${deploy.dir}/index.html" token="$${title}" value="${pageTitle}"/>
		<replace file="${deploy.dir}/index.html" token="$${swf}" value="${mainApp.name}"/>
		<replace file="${deploy.dir}/index.html" token="$${width}" value="100%"/>
		<replace file="${deploy.dir}/index.html" token="$${height}" value="100%"/>
		<replace file="${deploy.dir}/index.html" token="$${bgcolor}" value="${mainApp.bgColor}"/>
		<replace file="${deploy.dir}/index.html" token="$${application}" value="${mainApp.name}"/>
		<replace file="${deploy.dir}/index.html" token="$${version_major}" value="9"/>
		<replace file="${deploy.dir}/index.html" token="$${version_minor}" value="0"/>
		<replace file="${deploy.dir}/index.html" token="$${version_revision}" value="28"/>
	</target>
 
</project>

Build.properties

######################################
## Author information
######################################
author			=	Kenneth Lejnieks
project.owner		=	Lejnieks Consulting
project.owner.url	=	http://www.lejnieks.com
project.fullname		=	Build CI POC 1
project.version		=	1.0.0 alpha  # major.minor[.revision[.build]]
project.name		=	POC Sample 1
project.year		=	2009
application.name	=	POC
 
 
 
######################################
## Path information
######################################
FLEX_HOME		=	sdks/flex-3.2-sdk
 
htmltemplate.dir	=	../html-template
srcpath.dir		=	../src
libs.dir  		=	../libs
tools.dir		=	tools
 
deploy.dir		=	../deploy
 
 
 
######################################
## Build arguments
######################################
verbose			=	true
 
 
 
######################################
## Application information
######################################
mainApp.name		=	FlexCISample1
mainApp.src		=	${srcpath.dir}/${mainApp.name}.mxml
mainApp.swf		=	${deploy.dir}/${mainApp.name}.swf
mainApp.bgColor		=	#f6f6f6
locale			=	en_US
encoding		=	UTF-8
 
 
 
######################################
## Build Tools
######################################
flexTasks.jar		=	${FLEX_HOME}/ant/lib/flexTasks.jar
flexAntTasks.jar	=	${tools.dir}/FlexAntTasks.jar
ant-contrib.jar		=	${tools.dir}/ant-contrib.jar

Project source files including necessary jars can be found here

There's 0 Comment So Far

Share your thoughts, leave a comment!