Adobe Solution Partner

April 14, 2008

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

Filed under: Shan's Simple Examples — Tags: , , , — Shannon Hicks @ 8:01 pm

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.
<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!

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Technorati
  • TwitThis

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

 

Server Down?

Maximize Web application uptime by drawing upon Webapper's years of experience tuning and stabilizing many of the world's largest ColdFusion Web applications. Contact us today!