Building Flex Applications with Maven: Basic project setup

The Flex 3 Live Docs explain the usage of Ant to build your Flex project.
But what about using Maven instead? Velo has created a set of Maven goals (mojos) that provide the same features, and go even further than the Flex Ant tasks (e.g. including FlexCover support). This series of short posts lines out the usage of Flex-Mojos.

Setting up your development environment

Assuming that Java is already installed on your machine, download Maven from maven.apache.org and unzip it to a directory. I used /Java/apache-maven/apache-maven-2.0.9.
Then add the properties listed below to your environment variables. The listing actually is my .profile file. You can place this file in your user directory on your Mac, and the next time you open a terminal these properties are read.

# Java
export JAVA_HOME=/Library/Java/Home
 
# Maven
export M2_HOME=/Java/apache-maven/apache-maven-2.0.9
export M2=$M2_HOME/bin
export MAVEN_OPTS="-Xms512m -Xmx1024m"
export PATH=$M2:$PATH
 
# Ant
export ANT_HOME=/Java/apache-ant/apache-ant-1.7.0
export PATH=$ANT_HOME/bin:$PATH
 
# Flex
export FLEX_HOME=/Applications/Adobe\ Flex\ Builder\ 3/sdks/3.1.0
export PATH=$FLEX_HOME/bin:$PATH

(In addition, I also set up Ant, and added FLEX_HOME to my .profile, but this is not necessary for running maven.)

Adding a pom.xml with Flex-Mojos support to your Flex project

In order to be able to build your Flex project with Maven you need to add a pom.xml file to the root directory of your Flex project. In this example, the pom.xml specifies how your Flex application shall be build, in this case using Flex-Mojos.

 
	4.0.0
 
	de.cophase
	Maven-Tutorial-1
	1.0-SNAPSHOT
swf
 
		src/main/flex
 
				info.flex-mojos
				flex-compiler-mojo
				${flex-mojos.version}
				true
 
						en_US
 
			flex-mojos-repository
			http://svn.sonatype.org/flexmojos/repository/
 
			flex-mojos-repository
			http://svn.sonatype.org/flexmojos/repository/
 
			com.adobe.flex.framework
			flex-framework
			${flex.sdk.version}
			pom
 
		3.1.0.2710
		2.0M8

Build the application

To build your Flex application go to the root directory of your project and run

mvn install

Source folder structure

Note that the typical source directory structure for Maven is different to the one used by Flex Builder.

src/main/flex

You could arrange the source files of your project in Flex Builder in order to follow Maven conventions, or change the sourceDirectory property. When rearranging your source folder, go to Project Properties > Flex Build Path and change the main source folder accordingly, so that Flex Builder is able to build your project as well.

Comments

Comments are closed.