Building a Simple BlazeDS Application Using Ant

tags: java · blazeds · ant

When I first starting working on a BlazeDS application, it wasn't too difficult to build the example flex application using FlexBuilder, then deploy on JBoss. However, once I started attempting to build using Ant, I started to run into some problems. Using a seemingly correct build script resulted in a non-functioning flex build (it is pretty easy to tell, since you can see in the lower status bar of your browser that there isn't any communication with the server).

Turns out that in order to use the mxmlc Ant tasks when building a BlazeDS application, one needs to include both the services and context-root attributes. The services attribute specifies the path to the services-config.xml file one would place in the WEB-INF/flex/ directory of a war file. The context-root is a string value that replace the {context.root} tokens in a url request to the MessageBroker servlet. So if you are running an application called blazetest (as I was), you would set the context-root attribute to /blazetest, so that the url request can properly map to the application when making calls back to the server.

A good starting point for building BlazeDS application is the Adobe sample chat application.

Here is an example ant build file for building the sample BlazeDS application:

Plain text

<?xml version="1.0" encoding="UTF-8"?>
<!-- 

	Example Ant build file for a sample blazeDS application.
	http://kushaura.com/blog/view/name:Building-a-Simple-BlazeDS-Application-Using-Ant
	
-->
<project name="blazetest" default="dist">

	<property name="FLEX_HOME" location="C:\flex_sdk_3"/>

	<property name="jboss.dir" location="C:\jboss\jboss-4.2.3.GA" />
	<property name="jboss.bin.dir" location="${jboss.dir}\bin" />
	<property name="jboss.deploy.dir" location="${jboss.dir}\server\default\deploy" />
	
	<property name="lib.dir" location="lib" />
	<property name="bin.dir" location="bin" />
	<property name="flex.bin.dir" location="flex-bin" />
	<property name="src.dir" location="src" />
	<property name="dist.dir" location="dist" />
	
	<property name="config.dir" location="conf" />

	<property name="flex.name" value="main" />

	<path id="libraries">
		<fileset id="blaze-lib" dir="${lib.dir}/blazeds">
			<include name="**.jar"/>
		</fileset>
	</path>

	<fileset id="flex-src" dir="${src.dir}">
		<exclude name="**.java" />
	</fileset>
	
	<fileset id="blaze-config" dir="${config.dir}">
		<include name="flex/**.xml" />
	</fileset>
	
	<echo>Loading extra ant tasks</echo>
	<taskdef resource="flexTasks.tasks" classpath="${lib.dir}/ant/flexTasks.jar" />

	<target name="init">
		<mkdir dir="${bin.dir}" />
		<mkdir dir="${flex.bin.dir}" />
		<mkdir dir="${dist.dir}" />
	</target>

	<target name="clean">
		<delete dir="${bin.dir}" />
		<delete dir="${flex.bin.dir}" />
		<delete dir="${dist.dir}" />
	</target>

	<target name="build" depends="init">

		<echo>Starting java compile</echo>
		<javac srcdir="${src.dir}" destdir="${bin.dir}" classpathref="libraries" />

		<echo>Starting flex compile</echo>
		<mxmlc 
			file="${src.dir}/${flex.name}.mxml" 
			output="${flex.bin.dir}/${flex.name}.swf" 
			keep-generated-actionscript="false"
			services="conf/flex/services-config.xml"	
			context-root="/${ant.project.name}"
			>

			<!-- Include default libraries & config file. -->
			<load-config filename="${FLEX_HOME}/frameworks/flex-config.xml"/>
			<source-path path-element="${FLEX_HOME}/frameworks"/>
			<source-path path-element="${src.dir}" />
			
			<!-- Include custom flex libraries. -->
            <compiler.include-libraries dir="${lib.dir}/flex" append="true">
             	<include name="**.swc" />
            </compiler.include-libraries>
			
        </mxmlc>

	</target>
	
    <target name="wrapper">
        <html-wrapper 
            title="${ant.project.name}"
            file="index.html"
            height="100%"
            width="100%"
            bgcolor="white"
            application="${flex.name}"
            swf="${flex.name}"
            version-major="9"
            version-minor="0"
            version-revision="0"
            history="true"
            template="express-installation"
            output="${flex.bin.dir}"/>
    </target>

	<!-- Package application for distribution -->
	<target name="dist" depends="clean,build,wrapper">

		<war destfile="${dist.dir}/${ant.project.name}.war" webxml="${config.dir}/web.xml">
			  <fileset dir="${flex.bin.dir}"/>

			  <lib dir="${lib.dir}/blazeds" />

			  <classes dir="${bin.dir}"/>

			  <webinf refid="blaze-config" />

		</war>


	</target>

	<!-- Deploy built war to local server for testing -->
	<target name="deploy-local" depends="dist">
		<copy file="${dist.dir}/${ant.project.name}.war" toFile="${jboss.deploy.dir}/${ant.project.name}.war" />
	</target>

</project>


Add A Comment

Some html formatting accepted. Please use Markdown syntax for entering formatting markup. All comments are moderated and we reserve the right to remove inappropriate material.