Compile and Deploy Flex and Grails with Maven

The project I am working on uses an Adobe Flex front-end and a Grails back-end. We needed a way to inject the flex SWF files into Grails and then deploy into an Apache Tomcat server. After much tampering and banging of head, I came up with a solution that uses GMaven, the Maven Plugin, and Cargo to compile and deploy our modified grails app.

Here is the resulting POM file. My commentary is in Bold and not red. I started by installing the Grails-Maven plugin and generating a plugin, which I then modified. All my Flex source files were located in the src/main/flex directory.

<!– POM FILE: WE START WITH STANDARD POM DEFINITIONS, NOTICE THAT THE PACKAGE TYPE IS GRAILS-APP –>

<?xml version=”1.0″ encoding=”UTF-8″?>
<project xmlns=”http://maven.apache.org/POM/4.0.0&#8243; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd”&gt;
<modelVersion>4.0.0</modelVersion>
<groupId>com.company package</groupId>
<artifactId>myapp</artifactId>
<packaging>grails-app</packaging>
<name>myapp</name>
<version>0.1</version>

<pluginRepositories>

<!– Cargo Repository –>

<pluginRepository><id>codehaus snapshot repository</id>
<url>http://snapshots.repository.codehaus.org/</url&gt;
<releases>
<enabled>true</enabled>
</releases>
</pluginRepository>

<!– Maven-Grails plugin –>

<pluginRepository>
<id>mtg.snapshots</id>
<name>Forge Snapshot Repository</name>
<url>http://forge.octo.com/archiva/repository/mtg-snapshots</url&gt;
<releases>
<enabled>true</enabled>
</releases>
</pluginRepository>
</pluginRepositories>

<!– Here is where we would add additional libraries for DEPLOYMENT, in here, I added the mysql library for database communication. –>

<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.8</version>
</dependency>
</dependencies>

<!– Start of Build script –>

<build>
<plugins>

<!– SWF COMPILATION

In here, we point to the mxml file that needs to be compiled. We use the maven-flex2-plugin, which actually works for Flex 3. All the mxml files needed live in the /src/flex/main directory. ( src is that same directory in a Grails app that would have the java and groovy directories )

Notice that the compilation phase here is generate sources, which allows me to do something like :

mvn generate-sources
grails run-app

which compiles and moves the swf file and then allows me to run the jetty container with this swf included.

–>

<plugin>
<groupId>net.israfil.mojo</groupId>
<artifactId>maven-flex2-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>compile-flex</id>
<phase>generate-sources</phase>
<goals>
<goal>compile-swf</goal>
</goals>
<configuration>
<finalName>flexapp</finalName>
<flexHome>${flex.home}</flexHome>
<mainMxmlFile>flexapp.mxml</mainMxmlFile>
</configuration>
</execution>
</executions>
</plugin>

<!– MOVING COMPILED SWF FILE
This is a simple GMaven ( groovy for Maven ) script that copies the compiled swf files into the maven web-app directory.
–>

<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
def dir = new File(project.basedir, ‘web-app’)
ant.mkdir(dir: dir)
ant.copy(todir: dir) {
fileset(dir: new File( project.basedir, ‘target’ ) ) {
include(name: ‘*.swf’)
}
}
</source>
</configuration>
</execution>
</executions>
</plugin>

<!– Maven-Grails Plugin definition

This plugin integrates packaging and deploying the grails app into maven. With the package type of this project of the form grails-app, it generates the appropiate WAR file when you call

mvn package

–>

<plugin>
<groupId>com.octo.mtg</groupId>
<artifactId>grails-maven-plugin</artifactId>
<version>0.2</version>
<extensions>true</extensions>
</plugin>

<!– Cargo Deployment of the Grails App

The Cargo Maven2 plugin allows for hot deployment into the Tomcat container, amongst other things ( such as downloading a complete web application server and deploying the files into it, try doing that with GANT ).

Incorporating this plugin allows me to deploy to tomcat via:

mvn cargo:deploy

–>

<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<configuration>
<container>
<containerId>tomcat5x</containerId>
<type>remote</type>
</container>
<configuration>
<type>runtime</type>
<properties>
<cargo.tomcat.manager.url>http://localhost:9090/manager</cargo.tomcat.manager.url&gt;
<cargo.remote.username>admin</cargo.remote.username>
<cargo.remote.password></cargo.remote.password>
</properties>
</configuration>
<deployer>
<type>remote</type>
<deployables>
<deployable>

<!– POINTING TO THE WAR FILE

Notice that here, I declare a new link to same war file generated by grails application. Cargo only handles war, ear or j2ee package types. The Maven Grails plugin’s package type of grails-app makes the Cargo maven plugin cry. The workaround is to define an external dependency to the same file, which lets me deploy the grails-app

–>
<groupId>com.company.package</groupId>
<artifactId>myappWebApp</artifactId>
<type>war</type>
<location>myapp-0.1.war</location>
</deployable>
</deployables>
</deployer>
</configuration>
</plugin>
</plugins>
</build>

<properties>
<grailsVersion>1.0.2</grailsVersion>
<grailsHome>${env.GRAILS_HOME}</grailsHome>
</properties>

</project>

The case for Maven: why use Maven when there are tools like Gant that provide tighter integration with Grails? While this is true, the problem comes when you try to do things in which your grails war file is only a small piece of the puzzle. While the Ivy plugin for Grails does allow for dependency management, Maven’s plugins allow me to compile SWFs and deploy into web servers pretty much out of the box and without having to worry about removing jar files that are compile time only.

Potential directions with GMaven: When I first heard about GMaven, I was very excited. Since Grails scripts are written in Gant, there might be some clever way to have GMaven build the different stages of the Grails application life cycle with Maven, without having to use the grails maven plugin. Grails 1.1 promises Maven integration, so this will definitively something I will keeping my eye out for.

4 thoughts on “Compile and Deploy Flex and Grails with Maven

  1. Pingback: Grails, SEO, SWFObject and Flex : Progressive Enhancement - Layering SWF on top of GSPs « Dump brain here : Flex, AIR, Grails, Groovy, Facebook and all that jazz

  2. Pingback: Nine Lessons from building a Grails / Flex / Flash website « Dump brain here : Flex, AIR, Grails, Groovy, Facebook and all that jazz

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s