Flex, Spring and BlazeDS: the full stack! (Part 2)

[UPDATE 2] If you are still having version issues with the sample application featured in this article, you can get a fully upgraded version here

[UPDATE] This article series has been reedited on the Adobe Developer Connection. For more information, see this post.

In the previous article of this series, I laid the ground for what will be a step-by-step tutorial about creating a Flex/BlazeDS/Spring/Hibernate project. In this article, I will go into more detail about the creation of the Flex UI for our Todo List application.

Creating a root Maven project

The first issue we have to tackle when creating a Flex/BlazeDS project with Maven is the location of configuration files. Both the Flex compiler and BlazeDS server-side libraries need XML configuration files that describe AMF channels, destinations, adapters and so on. They’re usually called remoting-config.xml and services-config.xml. So if we want to prevent duplicating those configuration files, it could be interesting to put everything into one single war-packaged Maven module. But then there is another issue: this monolithic project setup is not really compliant with Maven standards and mixing Flex and Java compilers in the same module is quite ugly. That’s why I’ve chosen to split the client-side and the server-side into two modules with a root pom-packaged module, at the cost of duplicating configuration files.

So first things first, let’s create the root todolist module. This article series assumes that you have already installed Maven, Java, MySQL and JBoss or Tomcat. If you have not already done so, see their respective website. Now the first thing to do is to create an empty Maven project using the default archetype. To do this, change your working directory to where you want to create the project and run the following command.

mvn -DgroupId=org.epseelon.samples -DartifactId=todolist archetype:create

This command creates a basic JAR project that we will modify to serve as a root module.

  1. Delete the “src” directory in this module
  2. Edit pom.xml and change packaging to “pom” instead of “jar”
  3. Add the following repositories to the POM:
<project>
  …
  <repositories>
    <repository>
      <id>flex-mojos-repository</id>
      <url>http://flex-mojos.googlecode.com/svn/trunk/repository/</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
    <repository>
      <id>epseelon-repository</id>
      <url>http://m2repo.epseelon.org/</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>  

  <pluginRepositories>
    <pluginRepository>
      <id>flex-mojos-repository</id>
      <url>http://flex-mojos.googlecode.com/svn/trunk/repository/</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </pluginRepository>
    <pluginRepository>
      <id>epseelon-repository</id>
      <url>http://m2repo.epseelon.org/</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </pluginRepository>
  </pluginRepositories>
</project>

The first repository is the one for the flex-compiler mojo, the second one is my own repository, on which you’ll be able to find blazeds libraries until Adobe releases them in a public repository.

[EDIT]
Finally, since we’re gonna use maven-assembly-plugin and maven-dependency-plugin in submodules, it’s good practice to specify an explicit version for those plugins here. So add the following build section to pom.xml:

<build>
  <pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.0</version>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.2-beta-2</version>
      </plugin>
    </plugins>
  </pluginManagement>
</build>

[/EDIT]

Creating a Flex module

We’re going to start with the “easy” part: the Flex module. There are several maven plugins to handle Flex-based projects, namely ServeBox and Israfil. But the last time I tried them, none handled BlazeDS projects, which is why I reported this issue on Adobe issue tracking system. And then one day, a brazilian guy named Velo decided that it was time to act, and he started working on his own Maven plugin. And I have to say that he’s working very fast, because it’s working great, as you will see.

You have to change your working directory to the todolist directory you’ve created above and run the following command:

mvn archetype:create 
-DarchetypeArtifactId=maven-archetype-flex 
-DarchetypeVersion=1.0 
-DarchetypeGroupId=dk.jacobve.maven.archetypes 
-DgroupId=org.epseelon.samples 
-DartifactId=todolist-ria 
-DpackageName=

This archetype create a Flex project under the root module created above, but it uses israfil mojo by default. So once you’ve created your subproject, you’ll have to replace the israfil plugin configuration with the following one:

<plugin>
  <groupId>info.rvin.mojo</groupId>
  <artifactId>flex-compiler-mojo</artifactId>
  <version>1.0-alpha7</version>
  <extensions>true</extensions>
  <configuration>
    <locales>
      <param>en_US</param>
    </locales>
  </configuration>
</plugin>

You can also delete the properties section, as flex-compiler-mojo uses its own version of the Flex compiler. Last but not least, you have to add the following dependencies section to the POM:

<dependencies>
  <dependency>
    <groupId>com.adobe.flex.sdk</groupId>
    <artifactId>playerglobal</artifactId>
    <version>3.0.0.3.0.0.477</version>
    <type>swc</type>
    <scope>external</scope>
  </dependency>
  <dependency>
    <groupId>com.adobe.flex.sdk</groupId>
    <artifactId>flex</artifactId>
    <version>3.0.0.3.0.0.477</version>
    <type>swc</type>
  </dependency>
  <dependency>
    <groupId>com.adobe.flex.sdk</groupId>
    <artifactId>framework</artifactId>
    <version>3.0.0.3.0.0.477</version>
    <type>swc</type>
  </dependency>
  <dependency>
    <groupId>com.adobe.flex.sdk</groupId>
    <artifactId>framework</artifactId>
    <version>3.0.0.3.0.0.477</version>
    <type>resource-bundle</type>
    <classifier>en_US</classifier>
  </dependency>
  <dependency>
    <groupId>com.adobe.flex.sdk</groupId>
    <artifactId>rpc</artifactId>
    <version>3.0.0.3.0.0.477</version>
    <type>swc</type>
  </dependency>
  <dependency>
    <groupId>com.adobe.flex.sdk</groupId>
    <artifactId>rpc</artifactId>
    <version>3.0.0.3.0.0.477</version>
    <type>resource-bundle</type>
    <classifier>en_US</classifier>
  </dependency>
  <dependency>
    <groupId>com.adobe.flex.sdk</groupId>
    <artifactId>utilities</artifactId>
    <version>3.0.0.3.0.0.477</version>
    <type>swc</type>
  </dependency>
</dependencies>

With those modifications, again from the todolist directory, try to run “mvn install”. Normally, you should get a BUILD SUCCESSFUL message, and you should be able to view todolist/todolist-ria/target/todolist-ria-1.0-SNAPSHOT.swf in Flash Player.

Tadaaaa! That’s it for this second episode: now we have the Flex compiler plugin working on a simple client-only application. In the next episode (now that Luke and Lorelei are back together… woohooo!), we’ll create the server module with Spring and Hibernate. Stay tuned!

PS: Here is Todolist project with just the Flex module

PS II: If someone has a good code beautifier WordPress plugin to suggest, I’ll take it. I’ve tried WP-Syntax but it sucks with XML code.

44 comments

  1. Rather than duplicate the config files, you can create another module to bundle them up with the assembly plugin and then use the dependency plugin to unpack them where you need them.

    –Brian (Apache Maven PMC member)

  2. It’s weird. I mean, it would probably work but since Maven is all about integrating best practices, this doesn’t seem like a best practice to me. I would find it more natural if Maven allowed me to define inheritable resources: things like icon images, configuration files that are to be used in several modules of a project, I should be able to put them is src/main/resources in the root module and submodules should have access to this resource. What do you think?

  3. Oooops! In fact it doesn’t work, neither pretty, nor well at all. Overflow is completely wrong, all xml elements are lowercased and in Java code, it replaces generics usages by a closing and opening tag (List<TodoItem> becomes List</todoitem><todoitem>). It’s all wrong, I had to deactivate it. And pre doesn’t work very well either in the WordPress editor: it has a big issue with line feeds when I copy/paste code, it adds blank lines and when I try to remove them, it has a very erratic behaviour. I would have hoped that WordPress support for code snippet was a little better.

  4. Nice. I would share, that I think you can do

    mvn -DgroupId=org.epseelon.samples -DartifactId=todolist archetype:create -Dtype=pom

    where I added -Dtype=pom, then you can get rid of the step to go change the pom type from jar to pom.

  5. Inheriting the resources from the parent is an interesting concept, I don’t know if we’ve had that suggestion before. It’s not entirely unprecedented since site stuff does get attached to a parent and is inherited. You should bring this idea to the dev list and we can show you how to write a proposal or maybe someone will run with it.

  6. Hi ,

    When i try to install the POM File i am getting the following Error.

    [ERROR]

    [ERROR] BUILD ERROR
    [INFO] ————————————————————————
    [INFO] Plugin could not be found – check that the goal name is correct: Unable to download the artifact from any repository

    Try downloading the file manually from the project website.

    Then, install it using the command:
    mvn install:install-file -DgroupId=info.rvin.mojo -DartifactId=flex-compiler-mojo
    -Dversion=1.0-alpha7 -Dpackaging=maven-plugin -Dfile=/path/to/file

    info.rvin.mojo:flex-compiler-mojo:maven-plugin:1.0-alpha7

    from the specified remote repositories:
    central (http://repo1.maven.org/maven2)

    info.rvin.mojo:flex-compiler-mojo:maven-plugin:1.0-alpha7

    from the specified remote repositories:
    central (http://repo1.maven.org/maven2)

    [/ERROR]

    When i tried to check whether alpha7 of flex-compiler-mojo i cannot find it on the web site..

    pls let me know what am i doing wrong..

    Sateesh

  7. It seems that you forgot to add both repositories as explained at the beginning of the article, right after the creation of the root module. Otherwise those repositories would appear in the error message after the central repository.

  8. I am getting following error after running mvn -install. Please help.

    [ERROR] BUILD ERROR
    [INFO] ————————————————————————
    [INFO] Internal error in the plugin manager executing goal ‘info.rvin.mojo:flex-compiler-mojo:1.0-alpha7:compile-swf’: U
    nable to find the mojo ‘info.rvin.mojo:flex-compiler-mojo:1.0-alpha7:compile-swf’ in the plugin ‘info.rvin.mojo:flex-com
    piler-mojo’
    info/rvin/mojo/flexmojo/compiler/ApplicationMojo (Unsupported major.minor version 49.0)

  9. I have jdk 1.4 but I have JRE 1.6. I did set the JRE_HOME to 1.6. Do you think I need JDK 1.5+?

  10. Hi,

    If i add the plugin repositories the i am getting the Error:

    Parsing error /home/dpa/kommines/MyWorkspaces/com.lmco.dpa.ui.maven/pom.xml; org.codehaus.plexus.util.xml.pull.XmlPullParserException: Unrecognised tag: ‘pluginrepositories’ (position: START_TAG seen …n … @32:23)

    That’s the reason why i have commented out these in the first place.

    JDK Version : 1.6.5
    Maven Version : 2.0.5
    OpenSUSE10.3

    Thanks
    Sateesh

  11. Hi,

    Never mind..I moved the pluginrepositories to settings.xml and i am able to build it with out any problems..

    Thanks
    Sateesh

  12. Sorry, that’s my fault: the case is actually very important: it’s not “pluginrepositories” but “pluginRepositories”. I’ve fixed the code in the article.

  13. Hi,

    any idea how to use flex 4 maven compiler plugin instead of 3?..

    got really confused with the blog.

    THX all

  14. Hey,

    I am getting following exception after all the steps.

    $mvn install
    [INFO] Scanning for projects…
    [INFO] Reactor build order:
    [INFO] todolist
    [INFO] todolist-ria Maven Flex
    [INFO] ————————————————————————
    [ERROR] FATAL ERROR
    [INFO] ————————————————————————
    [INFO] id can not be null
    [INFO] ————————————————————————
    [INFO] Trace
    java.lang.NullPointerException: id can not be null
    at org.apache.maven.wagon.repository.Repository.(Repository.java:81)
    at org.apache.maven.artifact.repository.DefaultArtifactRepository.(DefaultArtifactRepository.java:70)
    at org.apache.maven.artifact.repository.DefaultArtifactRepositoryFactory.createDeploymentArtifactRepository(DefaultArtifactRepositoryFactory.j
    ava:44)
    at org.apache.maven.project.ProjectUtils.buildDeploymentArtifactRepository(ProjectUtils.java:80)
    at org.apache.maven.project.DefaultMavenProjectBuilder.processProjectLogic(DefaultMavenProjectBuilder.java:1038)
    at org.apache.maven.project.DefaultMavenProjectBuilder.buildInternal(DefaultMavenProjectBuilder.java:880)
    at org.apache.maven.project.DefaultMavenProjectBuilder.buildFromRepository(DefaultMavenProjectBuilder.java:255)
    at org.apache.maven.plugin.DefaultPluginManager.checkRequiredMavenVersion(DefaultPluginManager.java:270)
    at org.apache.maven.plugin.DefaultPluginManager.verifyVersionedPlugin(DefaultPluginManager.java:198)
    at org.apache.maven.plugin.DefaultPluginManager.verifyPlugin(DefaultPluginManager.java:177)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.verifyPlugin(DefaultLifecycleExecutor.java:1517)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.findArtifactTypeHandlers(DefaultLifecycleExecutor.java:1434)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.findExtensions(DefaultLifecycleExecutor.java:218)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:179)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:356)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:137)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:356)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
    at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
    at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
    at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
    [INFO] ————————————————————————
    [INFO] Total time: < 1 second
    [INFO] Finished at: Fri Jun 12 01:27:57 VET 2009
    [INFO] Final Memory: 1M/254M
    [INFO] ————————————————————————

    Any idea?

    -Shahzad

  15. VELO :
    Hi Shahzad,
    What maven version are you using?
    VELO

    I am using 2.1.0. I also have tried the same with 2.0.9 and found same results.

  16. Hrmmm, not related…

    Well, my next thought is there is something wrong on your settings.xml or pom.xml…. a server w/o id, or a repository entry w/o id, honestly I never saw this error.

    But take a look on settings and pom… I guess some entry there is missing the ID.

    VELO

  17. howdy.. interesting article.. same error as Shazad tho..

    [ERROR] FATAL ERROR
    [INFO] ————————————————————————
    [INFO] id can not be null

    anyone else experiencing that? (Maven 2.21)

    cheers!

  18. I followed post from VELO and downloaded the application from the URL given above by him and it works for me. But as a surprise, it won’t work well after I follow all steps again. Although, I wasn’t able to find much differences in the project provided by VELO and created using steps in this blog.

  19. Very interesting article but I am getting the same error with Shahzad and mike 🙁

    I made this from todolist directory after I changed everything as mentioned:
    **********************************************************************
    C:hanzotodolist>mvn install
    [INFO] Scanning for projects…
    [INFO] Reactor build order:
    [INFO] todolist
    [INFO] todolist-ria Maven Flex
    [INFO] ————————————————————————
    [ERROR] FATAL ERROR
    [INFO] ————————————————————————
    [INFO] id can not be null
    [INFO] ————————————————————————
    [INFO] Trace
    java.lang.NullPointerException: id can not be null
    at org.apache.maven.wagon.repository.Repository.(Repository.java:81)
    at org.apache.maven.artifact.repository.DefaultArtifactRepository.(DefaultArtifactRepository.java:70)
    at org.apache.maven.artifact.repository.DefaultArtifactRepositoryFactory.createDeploymentArtifactRepository(DefaultArtifactRepositoryFactory.java:44)
    at org.apache.maven.project.ProjectUtils.buildDeploymentArtifactRepository(ProjectUtils.java:80)
    at org.apache.maven.project.DefaultMavenProjectBuilder.processProjectLogic(DefaultMavenProjectBuilder.java:1038)
    at org.apache.maven.project.DefaultMavenProjectBuilder.buildInternal(DefaultMavenProjectBuilder.java:880)
    at org.apache.maven.project.DefaultMavenProjectBuilder.buildFromRepository(DefaultMavenProjectBuilder.java:255)
    at org.apache.maven.plugin.DefaultPluginManager.checkRequiredMavenVersion(DefaultPluginManager.java:270)
    at org.apache.maven.plugin.DefaultPluginManager.verifyVersionedPlugin(DefaultPluginManager.java:198)
    at org.apache.maven.plugin.DefaultPluginManager.verifyPlugin(DefaultPluginManager.java:177)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.verifyPlugin(DefaultLifecycleExecutor.java:1517)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.findArtifactTypeHandlers(DefaultLifecycleExecutor.java:1434)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.findExtensions(DefaultLifecycleExecutor.java:218)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:179)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:356)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:137)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:356)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
    at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
    at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
    at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
    [INFO] ————————————————————————
    [INFO] Total time: mvn –version
    Apache Maven 2.1.0 (r755702; 2009-03-18 21:10:27+0200)
    Java version: 1.6.0_13
    Java home: C:java_cansanerj2eejdkjre
    Default locale: en_US, platform encoding: Cp1252
    OS name: “windows xp” version: “5.1” arch: “x86” Family: “windows”
    **********************************************************************

    Thanks advance if you can see what is the problem 🙂

    CS

  20. [INFO] ————————————————————————
    [INFO] Total time: mvn –version
    Apache Maven 2.1.0 (r755702; 2009-03-18 21:10:27+0200)
    Java version: 1.6.0_13
    Java home: C:java_cansanerj2eejdkjre
    Default locale: en_US, platform encoding: Cp1252
    OS name: “windows xp” version: “5.1” arch: “x86” Family: “windows”
    **********************************************************************

  21. I had same problem as many others –
    [INFO] id can not be null
    – and I tried reverting back to Maven 2.0.8 – everything then worked fine. Maybe later versions are stricter in pom syntax?
    It’s a workaround, not a solution.
    I’d be interested to know underlying fault though as I don’t want to get stuck on 2.0.8

    SY

  22. Change the flex-compiler-mojo plugin to have a version value of
    1.0-beta7 instead of
    1.0-beta3

    that solved the null id error for me.

  23. Thanks thanks Caius, your solution really pushed me to understand the rest of the article, else I was stuck hovering with the same problem for quite a while.
    Thanks again.

  24. The flex-mojos was moved to Sonatype forge.
    Here is its core codebase now:
    “http://svn.sonatype.org/flexmojos/”.

    Hope that this information will be of help.

Leave a Reply to Bouiaw Cancel reply