Building Flex 4.1 projects with Maven and Flexmojos

Adobe relies on ant for its main flex build tool, but Sonatype now has a very functional Maven 3 plugin. It’s still in beta but seems stable enough, and it has the usual advantages over ant.

To try it out, make sure you have the Sonatype repository, http://repository.sonatype.org/content/groups/forge/, and add the flexmojos plugin to your pom: org.sonatype.flexmojos:flexmojos-maven-plugin:4.0-beta-7.

Since I was retrofitting old projects to use maven, I didn’t try to try out the flexmojos archetypes.

The default flex sdk of flexmojos seems to be 4.5. For flex 4.1, I found I had to add a dependency on the flex compiler with the version I wanted (see pom below). The sdks are in Sonatype’s repo, as are other basic libraries like flexunit. Most third-party libraries, like Swiz, for example, will need to be installed manually into your repository.

You can move over most compile settings from your ant script to corresponding maven options for compiling a swc or swf.  If your swc has namespaces used by another component, you will need to declare and include them in your pom (see below).

To run unit tests, you need to add a flex.flashPlayer.command property pointing to your debug flashplayer. To add code coverage, you supposedly just have to include the coverage tag. I’ve been unable to get it to work successfully, however. It always hangs on instrumenting the TestRunner.swf.

Here is a pom for a swc:

For the swf, you may also want to generate html wrapper files. Flexmojos does not generate them for you directly. Instead, you supply a set of files you want (swfobject.js, etc.) in a directory, use some replacement parameters, and then define the path to the files with templateURI. A line in the html like <title>${title}</title> will get the value of the title parameter defined in your pom substituted in. The docs say that the htmlName parameter is deprecated, but I found I had to add it to get the name the output html file named correctly.

To store all these files in a repository, it’s convenient to zip them up with the maven assembly plugin. It can then be added as a dependency to another project and unzipped if necessary, into a war, for example. Here’s a pom for a swf:

The documentation mentions a flexbuilder goal to generate Eclipse/Flashbuilder files, but it doesn’t seem to be in the current latest version. Better IDE support and working code coverage (assuming it’s not the fault of my own misconfiguration) would be great to have in future versions of flexmojos. What is in place already, though, works fine, and if you like working with maven for java, it’s worth looking at converting your flex projects over.

This entry was posted in flex, software and tagged , . Bookmark the permalink.

7 Responses to Building Flex 4.1 projects with Maven and Flexmojos

  1. Nice article,

    About flexbuilder and coverage, no-one care about flexbuild support, at least not enough to help and code coverage is there for quite while, take a look on wiki.

    Funny enough the biggest project adobe has (flex related and besides flex SDK) are built using flexmojos… flexPMD and cairngorm…

  2. @Tom,

    Marvin bumped apparat to v1.0-RC9. If you modify your flexmojos-parent pom to use the updated apparat version you’ll notice coverage works in beta 7. Obviously, this is just a hack PoC, you’d need to then build your own version of 4.0-SNAPSHOT for internal use. I’m going to ask Marvin to do a release of 4.0-beta-8 with the upgraded apparat to work with Flex 4.1+ – as everyone has been clamouring for it.

    – Justin

  3. dongyang says:

    hi,all
    How to configure Flex4 i18n in pom file?

  4. Santosh Radhakrishna says:

    To run unit tests, you need to add a flex.flashPlayer.command property pointing to your debug flashplayer. To add code coverage, you supposedly just have to include the coverage tag. I’ve been unable to get it to work successfully, however. It always hangs on instrumenting the TestRunner.swf.

    Has anybody found a solution for this problem yet ?

  5. Unsan says:

    Does anyone have a sample settings.xml that includes that tag that is working for them?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>