A sample Flex/Webapp project using Maven

This is a sample flex3/J2EE project configured as a multi-module maven project. You can use this structure as a basis to build your own project. The Flex project should be considered as a dependency of the Web app project (the WAR). Using the maven dependency plugin, the swf artifact is copied to the web application root folder.

The project contains three modules :

  • flex-gui : the SWF artifact,
  • java-service : a sample Java facade,
  • web-app : the Web application, depending on both the flex-gui and java-services artifacts.

flex-gui project

The “flex-gui” project is a swf flex nature project which contains .as and .mxml source files. This module produces a SWF artifact (packaging: swf).

java-service project

The “java-service” project is a standard java project which contains a java facade, providing data to the GUI. This module produces a JAR artifact (packaging: jar).

web-app project

The “web-app” project is a Web application project that contains  configuration files needed to deploy the application in a container, such as Tomcat. Other resources may also be included, such as JSP, servlet, images… This module produces a WAR artifact (packaging: war).

You can add other modules to create your own project, more often those projetcs could be  dedicated to the business layer, or persistence layer.

Requirements

To build this project you will need :

Dependencies

  • This sample is based on the Maven Flex Plugin 2.1.1. Check the homepage of the project to see more.
  • The Flex SDK version used is 3.2.0.3958.
  • As an IDE you may use FlexBuilder 3.0.2.xxx, or any text editor.

Multi-module project layout

    ...
    +sampleProject
      -pom.xml ( the parent pom )
      +flex-gui
        -pom.xml
        +src
          +main
            +flex
              + org.servebox.sample ( All as/mxml sources here )
          +resources
          +test
            +flex
              + org.servebox.test.sample ( All classes for unit test here )
      +java-service
        -pom.xml
        +src
          +main
            +java
              + org.servebox.sample ( All java sources here )
          +test
            +java
              + org.servebox.test.sample ( All classes for unit tests here )
      +web-app
        -pom.xml
       +src
          +main
            +resources
              - additional resources for webapp ( such as images, video etc... )
            +webapp
              +WEB-INF
                +flex
                  -services-config.xml
                -web.xml

POMs overview

The parent POM

Main section

Parent pom uses the pom packaging (this is an “aggregator project”) :

    <modelVersion>4.0.0</modelVersion>
    <groupId>org.servebox.sample.multi-module-app</groupId>
    <artifactId>multi-module-sample</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>Flex3-J2EE-MultiModules-Sample-Application</name>

Modules section

The modules section defines the three maven sub-modules :

    <modules>
        <module>flex-gui</module>
        <module>java-service</module>
        <module>web-app</module>
    </modules>

Plugin Repositories and Repositories

The following pluginRepository directive defines the location of the Maven Flex Plugin:

    <pluginRepositories>
        <pluginRepository>
            <id>servebox</id>
            <name>ServeBox Plugin Repository</name>
            <url>http://maven.servebox.org/repository</url>
        </pluginRepository>
    </pluginRepositories>

The following repository defines the location of the Flex SDK and ActionScript Foundry dependencies:

    <repositories>
        <repository>
            <id>servebox</id>
            <name>ServeBox Plugin Repository</name>
            <url>http://maven.servebox.org/repository</url>
        </repository>
    </repositories>

The Build Section

Importing projects into the Eclipse / FlexBuilder workspace

Maven can generate the eclipse project properties files for flex and java nature trought the maven eclipse plugin and maven flex plugins. Download the sample project, extract it into your workspaces directory, create a new workspace for the multi project and run the following command line from the root folder of the multi-module project (the root of the Maven reactor).

mvn flex:eclipse eclipse:eclipse

After importing “existing project” into a workspace, you have to define M2_REPO variables available to flex and java, see the next two screenshots :

Building the project

First, you have to define properties in the pom.xml of the root project :

    <properties>
        <tomcat.managerurl>http://localhost:8400/manager</tomcat.managerurl>
        <tomcat.serverprofile></tomcat.serverprofile>
        <webapp.name>sample-multi-project</webapp.name>
        <tomcat.server.root>D:\dev\blazeds-turnkey-3.2.0.3978\tomcat\webapps\${webapp.name}</tomcat.server.root>
        <tomcat.server.user>tomcat</tomcat.server.user>
        <tomcat.server.password>tomcat</tomcat.server.password>
    </properties>

You should replace the values with those corresponding to your own environment.

The whole project may be built using a single command. Using a command-line shell, run “mvn clean install” from the root folder : all projects included in the reactor will be built and installed to your local Maven repository (by default ~/.m2/repository).

If you want the application to be deployed automagically in tomcat, use the goal “tomcat:exploded”, and make sure that :

  • your Tomcat instance is running,
  • the configuration file ${tomcat.root]/conf/tomcat-users.xml contains the “tomcat” user (this profile is used by Maven to manage the Tomcat instance) :
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
  <role rolename="tomcat"/>
  <role rolename="manager"/>
  <role rolename="admin"/>
  <user username="tomcat" password="tomcat" roles="tomcat,admin,manager"/>
  <user username="admin" password="" roles="admin,manager"/>
</tomcat-users>

Download the source code

You may download the source code of this sample from the following location: multi-module-sample-10-snapshot-source

Tags: , ,

Print This Post Print This Post Monday, January 19th, 2009 Tutorials by Jeff Mathiot

6 Comments to A sample Flex/Webapp project using Maven

  1. Gave it a quick skim, very interesting. I’ll be doing something similar to this on my current project very soon.

  2. pwilder on January 20th, 2009
  3. I have added the parent pom that was missing.

  4. Alexis Desmarais on January 26th, 2009
  5. I cant able to find the jar file for the java services module
    can any one help me?

  6. kalyankrishna on July 10th, 2009
  7. Just use “mvn clean install” for building java-service java project.

  8. Alexis Desmarais on July 10th, 2009
  9. i find ur module interesting, but i have one problem

    when i clean install, i got this error :

    java.lang.NullPointerException
    at org.codehaus.plexus.util.xml.Xpp3Dom.(Xpp3Dom.java:73)
    at org.apache.maven.lifecycle.LifecycleUtils.mergeBindings(LifecycleUtils.java:304)
    at org.apache.maven.lifecycle.plan.BuildPlan.addLifecycleOverlay(BuildPlan.java:129)

    if u can help me;) thx

  10. crofteur on December 4th, 2009
  11. Hi,

    i have an error when executing mvn flex:eclipse eclipse:eclipse

    [INFO] ————————————————————————
    [INFO] Reactor Summary:
    [INFO]
    [INFO] Flex3-J2EE-MultiModules-Sample-Application …….. SUCCESS [26.555s]
    [INFO] flex-gui …………………………………… FAILURE [21.820s]
    [INFO] java-service ……………………………….. SKIPPED
    [INFO] web-app Maven Webapp ………………………… SKIPPED
    [INFO] ————————————————————————
    [INFO] BUILD FAILURE
    [INFO] ————————————————————————
    [INFO] Total time: 3:55.765s
    [INFO] Finished at: Thu Jun 09 16:50:04 CEST 2011
    [INFO] Final Memory: 9M/23M
    [INFO] ————————————————————————
    [ERROR] Failed to execute goal org.servebox.flex:flex-plugin:2.1.0:eclipse (defa
    ult-cli) on project flex-gui: The property serverRoot should be set. -> [Help 1]

    org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal o
    rg.servebox.flex:flex-plugin:2.1.0:eclipse (default-cli) on project flex-gui: Th
    e property serverRoot should be set.
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor
    .java:217)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor
    .java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor
    .java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProje
    ct(LifecycleModuleBuilder.java:84)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProje
    ct(LifecycleModuleBuilder.java:59)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBu
    ild(LifecycleStarter.java:183)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(Lifecycl
    eStarter.java:161)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:319)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
    java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
    sorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Laun
    cher.java:290)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.jav
    a:230)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(La
    uncher.java:409)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:
    352)
    Caused by: org.apache.maven.plugin.MojoExecutionException: The property serverRo
    ot should be set.
    at org.servebox.flex.mojo.EclipseMojo.checkServerConfiguration(EclipseMo
    jo.java:291)
    at org.servebox.flex.mojo.EclipseMojo.execute(EclipseMojo.java:261)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(Default
    BuildPluginManager.java:101)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor
    .java:209)
    … 19 more
    [ERROR]
    [ERROR]
    [ERROR] For more information about the errors and possible solutions, please rea
    d the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionE
    xception
    [ERROR]
    [ERROR] After correcting the problems, you can resume the build with the command

    [ERROR] mvn -rf :flex-gui

    Thanks

  12. nillak on June 9th, 2011

Leave a comment

You must be logged in to post a comment.