I’ve fallen in love with Maven, which is a powerful tool for project (in sense of the environment in the computer) handling.
I’m doing lots of Flex stuff and Maven – though designed for Java environment – works for that too. Using Maven in a Flex project requires a Maven plugin capable of compiling the AS3/Flex sources to a SWF or SWC. While Adobe has rejected the idea of releasing official Maven support plugin they recommend using the most actively developed and probably the best one of the available alternatives, Flex-Mojos.
From this on I assume that you know the basics of the Maven POM. In this part I’ll describe how to use Maven and Flex-Mojos to create a Flex Builder Flex library project. Since Flex-Mojos is under heavy development so we’re using 3.1-SNAPSHOT version of it, so some of the mentioned things might be outdated or fixed soon (as of March 22th 2009 they aren’t). In addition we’re using Flex 3.3.0.
Since Maven projects are based on the POM, a Flex project requires some modifications there. First of all, we need to change the packaging to SWC:
<packaging>swc</packaging>
Apart from that we need the dependency to Flex framework. That is found in Sonatype public repository:
<repositories>
<repository>
<id>sonatype-flex</id>
<url>http://repository.sonatype.org/service/local/repositories/flex/content</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.adobe.flex.framework</groupId>
<artifactId>flex-framework</artifactId>
<version>3.3.0.4852</version>
<type>pom</type>
</dependency>
</dependencies>
The last thing we need is the Flex-Mojos plugin which does all the hard work. It’s also found in the Sonatype repository:
<pluginRepositories>
<pluginRepository>
<id>sonatype-flexmojos-snapshots</id>
<url>http://repository.sonatype.org/service/local/repositories/flexmojos-snapshots/content</url>
</pluginRepository>
</pluginRepositories>
<build>
<sourceDirectory>src/main/flex</sourceDirectory>
<outputDirectory>target</outputDirectory>
<plugins>
<plugin>
<groupId>org.sonatype.flexmojos</groupId>
<artifactId>flexmojos-maven-plugin</artifactId>
<version>3.1-SNAPSHOT</version>
<extensions>true</extensions>
<configuration>
<includeClasses>
<class>com.googlecode.phlecks.example.Foo</class>
</includeClasses>
<includeFiles>
<param>foo.png</param>
</includeFiles>
<enableM2e>false</enableM2e>
</configuration>
</plugin>
</plugins>
</build>
Above we first specify the source folder which is src/main/flex instead of src/main/java. In includeClasses we specify the classes to be included in the library. Similarly we specify the other assets to be included in includeFiles. Also we specify that we’re not using the m2eclipse Eclipse plugin, in case you use that, feel free to set this to true.
Now we’ve got a POM for our Flex library project, the next thing is to generate the Flex Builder project files with the plugin. So we type:
mvn flexmojos:flexbuilder
And as the result our project becomes a Flex library project also in Flex Builder after refreshing. If take a look at the project’s properties and Flex library build path we see that in the classes tab src/main/flex is specified as the main source folder. In library path we notice that most of the Flex framework libraries are load from the local repository.
Also we notice that the current Flex-Mojos snapshot doesn’t yet implement adding the includeClasses or includeFiles to the library, the output folder is still bin-debug and in the source path tab src/main/flex is also added as an additional source folder outside the main source folder. These are smallish issues in the snapshot version, I’ve supplied the Flex-Mojos team a patch which fixes these.
So, now we’ve got a mavenized Flex library project that compiles in Flex Builder in the same way it would do if it was a Flex Builder created project. We can also compile it using Maven command:
mvn package
Which creates the SWC file, which is almost the same as the one created by Flex Builder. I think we’re done by now, here‘s the link for a sample project configured this way.
How are you using the swc files that you create in maven inside of FlexBuilder as a library? I am used to running mvn eclipse:eclipse to setup all my referenced libraries.
Basically, I have a swf that I want to use a swc that I created with maven in and would rather it be more like the mvn eclipse:eclipse way of doing it.
Thanks,
Karl
The mvn flexmojos:flexbuilder is the Flex-Mojos equivalent to mvn eclipse:eclipse.
To be able to use the SWC library on a SWF project it has to be on a repository (local repository will be fine) which you can achieve by running mvn install (to local repository) or mvn deploy (to remote repository on the net). When your SWC library is in the repository you can then setup the dependency for it to the SWF project and use mvn flexmojos:flexbuilder to configure the FlexBuilder project to use it.
Hey,
can’t wait to read part 2 of your post. ;)
Cheers,
Jakob
Thanks for reminding! The part 2 will be out soon…
Part 2 still pending, I’d like to see FlashBuilder 4 plugin on Flex-Mojos. In fact I’ve been developing it and I’m going to send a patch to the developer. I hope that gets in and I can continue the series.
Recently I have created an example project which uses Flash 4 and Air 2.5. You can see the code at https://bitbucket.org/mszalbach/flexsampleproject/overview