Shan’s Simple Examples: Start to Finish, using Ant with Flex for the first time

Disclaimer: This post is not about best practices or anything of that sort. This is just a recount of my first attempt at using Ant with an open-source Flex library project.

Here are the steps I went through (well, minus the steps that did nothing) to get my first Ant build script up & running;

Installing Ant

There were a few steps to get a Flex Builder standalone install up to speed to do stuff with Ant. Eclipse users might or might not need to do these steps.

  • Install Ant (Thanks, Judah!)
    1. In Flex Builder, go to Help > Software Updates > Find and Install
    2. Search for new features to install
    3. Select “The Eclipse Project Updates”
    4. Select The Eclipse Project Updates > Eclipse 3.3.2 > Eclipse Java Development Tools…
    5. Finish the installation process, including restarting Flex Builder
  • Install SVNAnt (Thanks, Phill!)
    1. Download the SVNAnt binary from http://subclipse.tigris.org/svnant.html
    2. Unzip the download, place all the .jar files in /lib/ in your Ant Install directory, which for Flex Builder is Flex Builder 3/plugins/org.apache.ant_${version_number}/lib/
  • Add needed libraries for FTP features
    1. Download the Apache Commons Net and Jakarta-ORO binaries
    2. Unzip both downloads, place all the .jar files in /lib/ in your Ant Install directory (same as above)
  • Finish configuring Ant
    1. Go to Window > Preferences > Ant > Runtime
    2. Click the Ant Home… button
    3. Choose your Ant Install directory (Flex Builder 3/plugins/org.apache.ant_${version_number}/). Be sure to NOT select the lib directory

Your Build Script

So you’ve got everything configured, now what? Let’s start with a build script. Explainations are in the comments. Keep in mind that these directory paths are for OS X, so Windows users will need to switch operating systems or use Windows paths. Probably easier to switch your OS.

  1. Create a build.xml in your project. Populate it with your build code (see my code exmaple below)
  2. Add the Ant view by going to Window > Other Views > Ant
  3. In the new Ant tab, right click and select Add Buildfiles. Select your build.xml file.
  4. Now, whenever you want to make a push, just hit the run button in the Ant tab.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<project name="MyProject" default="svn.live" basedir=".">
    <!-- set the taskdef's -->
    <taskdef resource="svntask.properties"/>
    <taskdef resource="flexTasks.tasks"/>
   
    <!-- set some properties -->
    <property name="TEMP_DIR" value="/tmp/MyProject_Temp/"/>
    <property name="FLEX_HOME" value="/Applications/Adobe Flex Builder 3/sdks/3.0.0/"/>
    <property name="APP_ROOT" value="~/Documents/Flex Builder 3/MyProject/"/>
   
    <target name="svn.live">
        <!-- clear out the temp dir -->
        <delete dir="${TEMP_DIR}"/>
       
        <!-- let's get the latest build from SVN to a temp directory -->
        <svn>
            <export srcUrl="http://svn.mysite.com/MyProject/trunk/" destPath="${TEMP_DIR}MyProject/"/>
        </svn>
        <echo>SVN Export Complete</echo>
       
        <!-- build the ASDocs from the source -->
        <exec executable="${FLEX_HOME}/bin/asdoc" failonerror="true">
            <arg line="-source-path ${TEMP_DIR}MyProject/"/>
            <arg line="-doc-sources ${TEMP_DIR}MyProject/"/>
            <arg line="-exclude-classes com.adobe.serialization.json.JSON com.adobe.serialization.json.JSONDecoder com.adobe.serialization.json.JSONEncoder com.adobe.serialization.json.JSONParseError com.adobe.serialization.json.JSONToken com.adobe.serialization.json.JSONTokenizer com.adobe.serialization.json.JSONTokenType"/>
            <arg line="-main-title 'My Big Project Docs'"/>
            <arg line="-output ${TEMP_DIR}MyProject/docs/"/>
        </exec>
        <echo>Docs Created</echo>
       
        <!-- build the latest swc from the source -->
        <exec executable="${FLEX_HOME}/bin/compc" failonerror="true">
            <arg line="-source-path ${TEMP_DIR}MyProject/"/>
            <arg line="-include-sources ${TEMP_DIR}MyProject/"/>
            <arg line="-output ${TEMP_DIR}MyProject/bin/MyProject.swc"/>
        </exec>
        <echo>SWC Built</echo>
       
        <!-- create zip files to upload -->
        <mkdir dir="${TEMP_DIR}zip/"/>
        <exec executable="zip" dir="${TEMP_DIR}">
            <arg line="-r ${TEMP_DIR}zip/MyProject.zip MyProject/"/>
        </exec>
        <echo>Source Zip Created</echo>
        <exec executable="zip" dir="${TEMP_DIR}MyProject/bin/">
            <arg line="${TEMP_DIR}/zip/MyProject.swc.zip MyProject.swc"/>
        </exec>
        <echo>SWC Zip Created</echo>
       
        <!-- upload files -->
        <ftp server="ftp.mysite.com"
            userid="username"
            password="password"
            passive="true"
            remotedir="path/to/upload"
        >
            <fileset dir="${TEMP_DIR}zip/"/>
        </ftp>
        <echo>Zip Files Uploaded</echo>
        <ftp server="ftp.mysite.com"
            userid="username"
            password="password"
            passive="true"
            remotedir="path/to/upload/docs"
        >
            <fileset dir="${TEMP_DIR}MyProject/docs/"/>
        </ftp>
        <echo>Zip Files Uploaded</echo>
    </target>
</project>

I’m not going to cover asdoc/compc/mxmlc command line options here, there’s plenty of great docs out there.
You can also read more about the svn task, exec task and ftp task at their respective documentation pages.

Now that you’ve got a start to finish example, go forth and Ant!

Comments