Compiling a Flex Application using Maven

Skip to end of metadata
Go to start of metadata

Requirements

Flexmojos does need Maven 2.0.9 and Java 5 or newer to run.

First you need to install Maven. The recommended version is 2.0.9. Make sure to follow the installation instructions at the bottom of the download page. You will also need Java 1.5 or newer.

Confirm that Maven is working properly by typing this at a command line:

mvn -v


You should see something similar to this:

$ mvn -v
Maven version: 2.0.9
Java version: 1.6.0_10
OS name: "windows xp" version: "5.1" arch: "x86" Family: "windows"

The Java version needs to be 1.5 or greater, and the OS might be different for you.

Create a new project using archetype

An Archetype is a template for a maven project. Use the following command to create a Flex application:

$ mvn archetype:generate -DarchetypeGroupId=org.sonatype.flexmojos -DarchetypeArtifactId=flexmojos-archetypes-application -DarchetypeVersion=3.1.0

Maven will ask about your project groupId, artifactId and version. Just input the values corresponding to your company, like this:

Define value for property 'groupId': : my.company
Define value for property 'artifactId': : my-project
Define value for property 'version':  1.0-SNAPSHOT: :
Define value for property 'package':  my.company: : my.company.myproject
Confirm properties configuration:
groupId: my.company
artifactId: my-project
version: 1.0-SNAPSHOT
package: my.company.myproject
 Y: : Y

There are other archetypes available here.

There are more information about maven archetypes here.

Default file structure

Once that is done, maven will create a project like this:

my-project
¦   pom.xml
¦
+---src
    +---main
    ¦   +---flex
    ¦   ¦       Main.mxml
    ¦   ¦
    ¦   +---resources
    +---test
        +---flex
            +---my
                +---company
                    +---myproject
                            TestApp.as

Running maven

Run maven at my-project folder:

$ mvn clean install

That will produce a new SWF file at target sub-folder.

my-project
¦   pom.xml
¦
+---src
¦   +---main
¦   ¦   +---flex
¦   ¦   ¦       Main.mxml
¦   ¦   ¦
¦   ¦   +---resources
¦   +---test
¦       +---flex
¦           +---my
¦               +---company
¦                   +---myproject
¦                           TestApp.as
¦
+---target
    +---classes
    ¦
    +---surefire-reports
    ¦
    +---test-classes
    ¦
    +   my-project-1.0-SNAPSHOT.swf


Opening my-project-1.0-SNAPSHOT.swf will result in a screen like this:

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Mar 02, 2009

    Justin Edelson says:

    There should be something on this page about the need to add r.s.o. as a reposit...

    There should be something on this page about the need to add r.s.o. as a repository or use Nexus/mirrors/etc.

    1. Mar 02, 2009

      Marvin Herman Froeder says:

      Flexmojos is going to central....  that is one of the reasons we are delayi...

      Flexmojos is going to central....  that is one of the reasons we are delaying 3.0

      VELO

  2. Apr 07, 2009

    pnico says:

    We have our artists compile FLAs in CS3 as SWCs so the Flex build can include th...

    We have our artists compile FLAs in CS3 as SWCs so the Flex build can include them as libraries, this gets us code hinting / some error checking of the assets in Flex Builder, etc. I want to use flex-mojos  but I don't want to deploy every art SWC to the maven repository for several reasons.  The code libraries yes; the assets no. They are stored in src/main/assets.  Any chance of adding the capability to link SWCs from the workspace for cases like this?

    1. Apr 07, 2009

      Marvin Herman Froeder says:

      I don't think so. Best way is to create a simple script for your artists that a...

      I don't think so.

      Best way is to create a simple script for your artists that automatically install this artifacts into a maven repository maven, like Nexus.

      VELO

      1. Apr 07, 2009

        pnico says:

        hmm - this is akin to to deploying all the mp3s, pngs etc. that we embed into th...

        hmm - this is akin to to deploying all the mp3s, pngs etc. that we embed into the swf using [Embed] to individual locations in the Maven repository and declaring dependencies on them as well - after all, they are compiled in. These are *assets* - the reason we store them as SWCs is simply that the peculiarity of Flash treating them as if they were code has some convenient side effects for us. To deploy them all to our maven repository seems like an awkward workaround, and would make our build process more complicated and dependent on human factors (such as requiring non-technical artists to run scripts as part of their process) than it already is.

        We could switch to storing our assets as SWFs instead of SWCs and using the [Embed (source="myassets.swf", symbol="myasset")] method, but then we give up some convenient features that add reliability and ease to our process. The alternative is cluttering our maven repository with countless small asset files many of which will only ever be used in one project and have no real purpose on their own. Even with asset libraries that are shared across projects, it would be much nicer to bundle them together in one package in the maven repository, while allowing the individual assets within that artifact to be split into separate files for art-production purposes, whose products would be consolidated only as part of the build process. I don't think Maven was intended or designed well for management of art assets at that level of granularity. I guess it doesn't help that the packaging paradigm is very peculiar to Flash. I agree that for compiling in external code dependencies, the Maven way is best. It is not best for packaging external (Flash-created or not) assets into a SWF.

        1. May 25, 2009

          Peter Jacobsen says:

          Exactly my thoughts - I could not express it better. I had a similar discussion ...

          Exactly my thoughts - I could not express it better.
          I had a similar discussion and supplied a patch, but it has been rejected. It offers the mxmlc compiler options include-libraries and library-path. It solves all our workflow requirements since I use it exclusively for graphic assets supplied by designers (tons of them).

          Regards,
          Peter

          1. Jun 04, 2009

            pnico says:

            OK, I wasn't going to post again but I found a way to work with this that's easi...

            OK, I wasn't going to post again but I found a way to work with this that's easier than what I described on the Jira page and probably more people look at the wiki: For each dependency on a SWC asset library published out of Flash CS3 by an artist, I add an execution of deploy:deploy-file which runs in the process-resources phase. Maven doesn't complain if a dependency doesn't exist until the compile step; if the deploy works, the dependency does exist by then and everything's fine. I've tried this with a fresh repository that has never seen these dependencies, and it seems to work.

             I am a maven newbie so please let me know if this is not best practices or whatever, but i guess sometimes with flash you're stuck with "best practices under the circumstances".. However, it works, it's easy, and I don't have to make any more POMs, or set up any new jobs on the build server, or patch/recompile/self-host the plugin. This is good enough for me unless the build engineer starts complaining about disk space or something.. so for these dependencies in the POM:

            <dependency>
            	<groupId>com.example</groupId>
            	<artifactId>asset-lib-one</artifactId>
            	<version>trunk-SNAPSHOT</version>
            	<type>swc</type>
            </dependency>
            <dependency>
            	<groupId>com.example</groupId>
            	<artifactId>asset-lib-two</artifactId>
            	<version>trunk-SNAPSHOT</version>
            	<type>swc</type>
            </dependency>
            

            I add this to the build section's <plugins/>:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.4</version>
                <executions>
                <execution>
                    <id>deploy-assets-one</id>
                    <phase>process-resources</phase>
                    <goals><goal>deploy-file</goal></goals>
                    <configuration>
                        <repositoryId>snapshots</repositoryId>
                        <url>http://myserver/archiva/repository/snapshots</url>
                        <file>src/main/assets/flash/asset-lib-one.swc</file>
                        <groupId>com.example</groupId>
                        <artifactId>asset-lib-one</artifactId>
                        <description>A SWC Asset Library</description>
                        <version>trunk-SNAPSHOT</version>
                        <uniqueVersion>false</uniqueVersion>
                        <packaging>swc</packaging>
                    </configuration>
                </execution>
                <execution>
                    <id>deploy-assets-two</id>
                    <phase>process-resources</phase>
                    <goals><goal>deploy-file</goal></goals>
                    <configuration>
                        <repositoryId>snapshots</repositoryId>
                        <url>http://myserver/archiva/repository/snapshots</url>
                        <file>src/main/assets/flash/asset-lib-two.swc</file>
                        <groupId>com.example</groupId>
                        <artifactId>asset-lib-two</artifactId>
                        <description>Another SWC Asset Library</description>
                        <version>trunk-SNAPSHOT</version>
                        <uniqueVersion>false</uniqueVersion>
                        <packaging>swc</packaging>
                    </configuration>
                </execution>
                </executions>
            </plugin>
            
            1. Jun 04, 2009

              Marvin Herman Froeder says:

              That is a very nicer idea!!! BTW, you could use Builder Helper Plugin: http://m...

              That is a very nicer idea!!!

              BTW, you could use Builder Helper Plugin: http://mojo.codehaus.org/build-helper-maven-plugin/attach-artifact-mojo.html

              So you don't need to specify where the artifacts should be deployed.

              May be you could create a new page for that.

              VELO

              1. Jun 04, 2009

                pnico says:

                I can't find clear documentation on what attach-artifact actually does. What do ...

                I can't find clear documentation on what attach-artifact actually does. What do I put for the artifactId of the dependencies if I use that?

                1. Jun 04, 2009

                  Marvin Herman Froeder says:

                  http://mojo.codehaus.org/build-helper-maven-plugin/usage.html Attach additiona...

                  http://mojo.codehaus.org/build-helper-maven-plugin/usage.html

                  Attach additional artifacts to your project

                  Typically run after antrun:run to attach antrun's build outputs to the project for install and deploy.

                  <project>
                    ...
                    <build>
                      <plugins>
                        <plugin>
                          <!-- add antrun configuration here -->
                        </plugin>
                        ...
                        <plugin>
                          <groupId>org.codehaus.mojo</groupId>
                          <artifactId>build-helper-maven-plugin</artifactId>
                          <version>1.1</version>
                          <executions>
                            <execution>
                              <id>attach-artifacts</id>
                              <phase>package</phase>
                              <goals>
                                <goal>attach-artifact</goal>
                              </goals>
                              <configuration>
                                <artifacts>
                                  <artifact>
                                    <file>some file</file>
                                    <type>extension of your file </type>
                                    <classifier>optional</classifier>
                                  </artifact>
                                  ...
                                </artifacts>
                              </configuration>
                            </execution>
                          </executions>
                        </plugin>
                      </plugins>
                    </build>
                  </project>
                  
                  1. Jun 04, 2009

                    pnico says:

                    Yeah well of course I looked at that first before asking here, and I tried it ou...

                    Yeah well of course I looked at that first before asking here, and I tried it out a couple of ways but I couldn't figure out how to get it to make the attached artifacts act as dependencies too. the build fails because maven can't find them. Maybe i wasn't clear enough about what I was doing - these are SWCs with assets with linkage IDs in them that are referenced in my code, so they have to be dependencies.

                    For now I'll just do it with the deploy plugin, since it works and it's fine to specify where to deploy them, maybe later when I understand all this stuff better I'll try with the build-helper plugin, or maybe someone who knows how to do it already will contribute.. I can still make a new page for the deploy way though if you want.