07.13
Where I work we have a couple of servers for testing, one of which is the “cutting edge” server that holds the latest swf from subversion. I use a couple of tools that keep it as up to date as possible thanks to an automated build & deploy process that is triggered through SVN updates. So let’s walk through everything and make it happen.
- Install Apache Ant – The build tool that binds it all together. If you’re not familiar with Ant, it makes Flex compiling much easier. It’s somewhat like Make, but with plugins that make magic work (as we’ll see) so we can use ant to do everything rather than worry about command line tools.
- Test ant out by opening a command line and typing “ant”. You should get a message about a build.xml file not existing.
- Create your working directory. Something like “C:\autobuild\”. I’ll refer to it as the autobuild directory from here out
- Create a “libs” directory under autobuild. Since we don’t want to mess up your ant installation, we’re going to leave it alone.
- Create a “source” directory under autobuild. Go ahead and check out you latest into it since we’ll use it to test the flex copiler.
- Install the Flex SDK – Since you’re reading this, you’ve probably already got this going. This is so we can use the Flex Ant Tasks to automate the compilation of the source code. To do this, goto you flex_builder_install/sdks/version/ant directory and copy the flexTasks.jar files to autobuild/libs directory you just created.
- Create a text file named build.properties in the autobuild directory. This is where the ant global variables are defined. You must have a FLEX_HOME defined to use the Flex And Tasks and can define others to make your build file easier to understand. This is probably something along the lines of:
# Where Flex lives FLEX_HOME=C:/Program Files/Adobe/Flex Builder 3 Plug-in/sdks/3.2.0 # Where the Flex framework we want to compile with lives FRAMEWORK=${FLEX_HOME}/frameworks # Directory where the app will be deployed DEPLOY_DIR=./deploy # Directory where the app will be deployed SVN_DIR=./code - Create a text file named build.xml in the autobuild directory. This is where you can define all your settings to make ant work. The file should just be a reference to the build.properties, flexTasks, and then the mxmlc command:
<?xml version="1.0"?> <project default="main" basedir="."> <path id="classpath" > <fileset dir="${basedir}/libs/"> <include name="*.jar" /> </fileset> </path> <property file="${basedir}/build.properties" /> <taskdef resource="flexTasks.tasks"/> <target name="main"> <echo >Using framework from: ${FLEX_HOME}</echo> <mxmlc file="${SVN_DIR}/FlexApp.mxml" output="${DEPLOY_DIR}/FlexApp.swf" use-network="true" actionscript-file-encoding="UTF-8" keep-generated-actionscript="false" debug="false" locale="en_US" incremental="true" show-actionscript-warnings="false" > <load-config filename="${FRAMEWORK}/flex-config.xml" /> <source-path path-element="${FRAMEWORK}" /> </mxmlc> </target> </project>
Then, from the autobuild directory run “ant” and make sure you get a swf in the autobuild/bin directory. If you get a Java heap space error, you’ll have to increase Ant’s memory with a “set ANT_OPTS=-Xmx1024m” command on the command line before running ant to create an environment variable that will make Ant use more memory.
- Now that we can compile the source, it’s time to make svn work. This is a little trickier but SvnAnt helps out. SvnAnt is the most complicated step, but it’s what makes the magic happen. Go ahead and download the zip file, and copy the svnant.jar, svnClientAdapter.jar, and scnjavahl.jar files into your libs directory
- You’ll also need a JavaHL to make it work. Specifically the libsvnjavahl-1.dll from this zip will work. If there’s a better route to the file, let me know. Place the libsvnjavahl-1.dll file in the autobuild directory
- Finally update your build.xml file. Below the project tag add
<path id="classpath" > <fileset dir="${basedir}/libs/"> <include name="*.jar" /> </fileset> </path> <typedef resource="org/tigris/subversion/svnant/svnantlib.xml" classpathref="classpath" />
And then, between your target and mxmlc tags add
<svn username="svnuser" password="svnpass"> <checkout url="url/to/Repos" destPath="${SVN_DIR}" /> </svn>
- Now to test SVN. go ahead and delete everything in your bin and source directories and type “ant” from the command line. You should see a big checkout, then a compile, and get a swf in the bin directory. if there was a problem, check your svn path and make sure you’re checking out the proper directory and not getting the full svn tree
- Now that we have a checkout and build, we just need to ftp the swf onto our server. Ant’s FTP lib will do that wonderfully, we just have to get the proper jar files since they’re not included in the default ant setup. You can download the Jakarta Commons Net binary and then copy the commons-net-2.0.jar and commons-net-ftp-2.0.jar file to your autobuild/libs directory
- Update your build.xml file with a new taskdef (just place it below the flexTasks one):
<taskdef name="ftp" classname="org.apache.tools.ant.taskdefs.optional.net.FTP" />And then actually create an ftp task after the build step:
<ftp server="{server}" userid="{server}" password="{ftpPassword}" remotedir="{dir for swf}" passive="yes"> <fileset dir="${DEPLOY_DIR}"> <include name="**/*.swf"/> </fileset> </ftp>
- The final Ant step is to test it all. Because of the way the Ant FTP library works you have to use “ant -lib libs” to load the commons-net files. You should see a [ftp] action after the [mxmlc] reports the file name and the file should show up on your server. If it does, everything worked out and you’ve automated your build & deployment. Now you just need to trigger it
- First, create a batch file to call Ant and make sure everythings set up properly. Mine looks like this:
c: cd C:\autobuild set ANT_OPTS=-Xmx1024m ant -lib "libs"
- I use Commit Monitor which is a SVN repository monitor than can be set up to check the repository on a schedule and, if changes are found, run a program (or in this case, a batch file). Go ahead and install it and set the batch file to run when new commits are detected.
- Sit back and watch the magic happen
Just to make it simpler, here are the files we created:
#SVN location
urlRepos=<svn address>
# Where Flex lives
FLEX_HOME=C:/Program Files/Adobe/Flex Builder 3 Plug-in/sdks/3.2.0
# Where the Flex framework we want to compile with lives
FRAMEWORK=${FLEX_HOME}/frameworks
# Directory where the app will be deployed
DEPLOY_DIR=./bin
# Directory where the app will be deployed
SVN_DIR=./source
# Server to upload completed swf
HOST_SERVER=<serverURL> |
<?xml version="1.0"?> <project default="main" basedir="."> <path id="classpath" > <fileset dir="${basedir}/libs/"> <include name="*.jar" /> </fileset> </path> <typedef resource="org/tigris/subversion/svnant/svnantlib.xml" classpathref="classpath" /> <property file="${basedir}/build.properties" /> <taskdef resource="flexTasks.tasks"/> <taskdef name="ftp" classname="org.apache.tools.ant.taskdefs.optional.net.FTP" /> <target name="main"> <svn username="svnuser" password="svnpass"> <checkout url="${urlRepos}" destPath="${SVN_DIR}" /> </svn> <echo >Using framework from: ${FLEX_HOME}</echo> <mxmlc file="${SVN_DIR}/FlexApp.mxml" output="${DEPLOY_DIR}/FlexApp.swf" use-network="true" actionscript-file-encoding="UTF-8" keep-generated-actionscript="false" debug="false" locale="en_US" incremental="true" show-actionscript-warnings="false" > <load-config filename="${FRAMEWORK}/flex-config.xml" /> <source-path path-element="${FRAMEWORK}" /> </mxmlc> <ftp server="{server}" userid="{server}" password="{ftpPassword}" remotedir="{dir for swf}" passive="yes"> <fileset dir="${DEPLOY_DIR}"> <include name="**/*.swf"/> </fileset> </ftp> </target> </project> |
c: cd C:\autobuild set ANT_OPTS=-Xmx1024m ant -lib "libs" |
Very nice