Inserting build version numbers into Flash and Flex Applications

For an ActionScript project I am currently working on (and for future projects), we wanted to be able to include the ANT build number into our applications when we do development and production quality releases to clients. The added value in this is that it is extremely helpful if you are constently pushing releases. It saves time so that we can easily tell what version our clients are running on their development and production servers. This helps avoid unneeded debugging time making sure that the versions are correct. We also wanted this to happen automatically, or as much as possible, without the need for us to modify text files or add compiler variables.

Ok, lets get started! First, all of these instructions assumes you have a good knowledge in how ANT works. It also assumes you will be building an ActionScript or Flex project. In this case, we are going to be creating a custom ContextMenu but you can really do this for anything. This will allow anyone us to see the version numbers when they right click on our application.

Ok, so how is this done? Open up your build.properties file for the project and add the following lines:

project.major	  = 1
project.minor     = 0
project.revision  = 0
project.version   = ${project.major}.${project.minor}.${project.revision}

The projet will act as our properties for figuring out the project major, minor and revision. These could probably be automated in some way but for now I am ok with manually setting these values since I will know when these versions change. We will also need to include a build number into our build so just add the following before anything else into your build file. Also note that if this file does not exist, ANT will create it for you so no need to concern yourself with what the contents are. Let ANT do the dirty work!


<buildnumber file="${basedir}/build/build.number" />

Next, we are going to want to copy our classes to a temporary directory so that we can modify them without harming the source. As you can see I have some temp.outdir and classes.dir properties


<target name="copyClasses" description="Creates a temporary directory where the code will be duplicated so we can modify and build without harming the original files">
	<mkdir dir="${temp.outdir}" />
	<copy todir="${temp.outdir}">
		<fileset dir="${classes.dir}"/>
	</copy>
</target>

Now that our classes are copied, we need to do a search on them for particular strings that we will end up replacing with the project version. In my case, I have a ContextMenu class which has a string value defined as “[PROJECT_VERSION_NUMBER]” so I do a regular expression replace in ANT to find that value and replace it.


<target name="projectversion" description="">
	<replaceregexp match="[PROJECT_VERSION_NUMBER]" replace="${project.fullname} : ${project.version}.${build.number}" flags="gs">
		<fileset dir="${temp.outdir}" includes="**/*.as"/>
	</replaceregexp>
</target>

And that is it. Now we have our build number plus project version automatically showing up in our application.

This entry was posted in development and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>