Flex build numbers using Ant and flexTasks

There are times when it is quite handy to be able to verify the version of a Flex Object which is loaded into the browser. These times include occasions when you think the browser is caching content (swf), when there is an errant build process, or when you are swapping a swf application, or even subcomponents in and out for testing some behavior. Having the Ant builder automatically set a build number and timestamp on the swf, by setting a public var with the values, allows you to put the value setting code in a single module, or in multiple modules and use the same mechanism.

To facilitate this with minimal disruption of the remainder of the code, it is handy to utilize the include directive for Flex builder/mxmlc. The import directive is so commonly used, that the include directive is often forgotten. Adobe Livedoc on include directive. Here is an example:

<?xml version="1.0"?>

<!-- example with Version.as -->

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

    <mx:Script><![CDATA[

        include "Version.as";

        import  mx.controls.Alert;

    ]]></mx:Script>

    <mx:Button id="versionButton" label="Show current version"

               click="Alert.show(build_number + ' ' + build_time);"/>

</mx:Application>

And here is an example of a template Version.as.template file, prior to copying over to Version.as and replacing the @ tokens:

      public var build_number:String="@buildnumber@";

      public var build_time:String="@buildtime@";

Version.as file with @buildnumber@ and @buildtime@ tokens replaced.

      public var build_number:String="1.0.12";

      public var build_time:String="JUL 08 07 10:01 am";

Ant script to copy the template Version.as and then modify the result with the generated build number and buildtime.  Note that this requires a variable set up for modeldir  (the name modeldir is not important, it’s the path to the directory where where the Version.as file will be).

<taskdef resource="flexTasks.tasks"

         classpath="${ANT_HOME}/lib/flexTasks.jar" />

<target name="cvsbuild">

     <buildnumber file="mybuild.number" />

     <tstamp prefix="build"/>

     <tstamp>

         <format property="build.time" pattern="MMM dd yy hh:mm aa"

              offset="-1" unit="hour"/>

     </tstamp>

     <echo>Build Number ${build.number} ${build.time}</echo>

     <copy file="${modeldir}/Version.as.template"

              tofile="${modeldir}/Version.as" />

     <replace file="${modeldir}/Version.as"

            token="@buildnumber@" value="${build.number}" />

     <replace file="${modeldir}/Version.as"

            token="@buildtime@" value="${build.time}" />

</target>

The nice thing about setting vars like this, is it is possible to reach into any such object and pull out the version number for verification. This could be checked at runtime, or in your UnitTesting, or manually to follow up on a user reported issue.

Ant References: BuildNumber , tstamp, replace .

Advertisements

4 Responses to Flex build numbers using Ant and flexTasks

  1. […] and compiled it (Thanks to Jeff at Alagad). This worked right away for me, so with the help of this post from Howard Scholtz, I was able to add in revsion #/time directly into the app without using […]

  2. I found I had to put the overwrite=”true” to get this to work, on the line

  3. How do you implement the Ant script?
    what file name does it need to be and where?

    Adding the include and Versions.as to my project is simple.
    You just fail to say how to implment the ANT part..

    James

  4. buy poppers says:

    Thank you for another informative site. Where else could I get that kind of information written in such a perfect way?
    I’ve a project that I’m just now working on, and I’ve been on the look out for such info.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: