05
Apr

Automatically version a Flex application with Maven

Today I had an issue debugging a Flex application.

Problem

Some of my changes were not built correctly and I had a hard time debugging and checking whether the version of the swf I had deployed is the same with the latest version of my swf file.

Solution

I wanted to have an embedded code into the swf and display it into a label, at runtime. Browsing the internet initially, I’ve found some solutions using Python and Ruby.  Having my build in maven, I decided to see what is provided by Flex, FlexMojos and Maven in order to facilitate that.

Analyzing the mxmlc compiler arguments I have found that I can define custom parameters into the compiler and read them from actionscript code. So I have defined a BUILD::buildNumber compiler variable:

-define+=BUILD::buildNumber,"'1.0.0'"

In the Main.mxml application I’ve added:

<fx:Script>
	<![CDATA[
		[Bindable]
		/**
		* specified through the flex compiler arguments like: 
		* -define+=BUILD::buildNumber,"'1.0.0'"
		*/
		private static var buildNumber:String = BUILD::buildNumber;
	]]>
</fx:Script>		
<s:Label 
	right="0" top="0"
	color="red"
	text="{buildNumber}" />

Next, I had to change the compiler parameter for each build, automatically. Using Hudson as my Continuous Integration server, things got very simple. When a Hudson job executes, it sets some environment variables that can be used in build scripts. Among those variables there is BUILD_NUMBER.

<plugin>
       <groupId>org.sonatype.flexmojos</groupId>
       <artifactId>flexmojos-maven-plugin</artifactId>
       <version>${flex-mojos.version}</version>
       <extensions>true</extensions>
       <configuration>
                <definesDeclaration>
		 	<property>
		 	       <name>BUILD::buildNumber</name>
		 	       <value>"${BUILD_NUMBER}-${project.version}"</value>
		 	</property>
          	</definesDeclaration>
       </configuration>
</plugin>

The output of this, for a buildNumber 173 and a project version 1.0-SNAPSHOT is:

173-1.0-SNAPSHOT

The next thing I need to achieve, is to trick the browser’s cache, using this buildNumber, appending it at the end of the request for Main.swf, to end up automatically with a request like:
Main.swf?173-1.0-SNAPSHOT.

My next post covers this.

5 Comments

Sorry, the comment form is closed at this time.

Comments RSS Feed