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 .
November 29, 2007 at 4:51 pm
[...] 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 [...]