Maven: The Complete Reference - 13.3. Creating a Flex Mojos Project from an Archetype |
|
Flexmojos has a set of archetypes which can be used to quickly create
a new Flex project. The following archetypes are all in the
To create a Flex Library Project, execute the following command at the command-line: $ mvn archetype:generate \ -DarchetypeRepository=http://repository.sonatype.org/content/groups/public\ -DarchetypeGroupId=org.sonatype.flexmojos \ -DarchetypeArtifactId=flexmojos-archetypes-library \ -DarchetypeVersion=${flexmojos.version} [INFO] Scanning for projects... [INFO] Searching repository for plugin with prefix: 'archetype'. [INFO] com.sonatype.maven.plugins: checking for updates from central ... [INFO] [archetype:generate] [INFO] Generating project in Interactive mode [INFO] Archetype defined by properties ... Define value for groupId: : +org.sonatype.test+ Define value for artifactId: : +sample-library+ Define value for version: 1.0-SNAPSHOT: : +1.0-SNAPSHOT+ Define value for package: org.sonatype.test: : +org.sonatype.test+ Confirm properties configuration: groupId: org.sonatype.test artifactId: sample-library version: 1.0-SNAPSHOT package: org.sonatype.test Y: : +Y+[INFO] Parameter: groupId, Value: org.sonatype.test [INFO] Parameter: packageName, Value: org.sonatype.test [INFO] Parameter: basedir, Value: /Users/Tim [INFO] Parameter: package, Value: org.sonatype.test [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] Parameter: artifactId, Value: sample-library [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL If you look in the directory sample-library/ you will see that the project consists of the directory structure shown in Figure 13.4, “Flexmojo Library Archetype File Structure”. The product of the simple Flex library archetype only contains three files: a POM, one source, and a unit test. Let’s examine each of these files. First, the Project Object Model (POM). Project Object Model for Flex Library Archetype. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.sonatype.test</groupId> <artifactId>sample-library</artifactId> <version>1.0-SNAPSHOT</version> <packaging>swc</packaging> <name>sample-library Flex</name> <build> <sourceDirectory>src/main/flex</sourceDirectory> <testSourceDirectory>src/test/flex</testSourceDirectory> <plugins> <plugin> <groupId>org.sonatype.flexmojos</groupId> <artifactId>flexmojos-maven-plugin</artifactId> <version>3.5.0</version> <extensions>true</extensions> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>com.adobe.flex.framework</groupId> <artifactId>flex-framework</artifactId> <version>3.2.0.3958</version> <type>pom</type> </dependency> <dependency> <groupId>com.adobe.flexunit</groupId> <artifactId>flexunit</artifactId> <version>0.85</version> <type>swc</type> <scope>test</scope> </dependency> </dependencies> <profiles> <profile> <id>m2e</id> <activation> <property> <name>m2e.version</name> </property> </activation> <build> <plugins> <plugin> <groupId>org.maven.ide.eclipse</groupId> <artifactId>lifecycle-mapping</artifactId> <version>0.9.9-SNAPSHOT</version> <configuration> <mappingId>customizable</mappingId> <configurators> <configurator id='org.maven.ide.eclipse.configuration.flex.configurator' /> </configurators> <mojoExecutions> <mojoExecution> org.apache.maven.plugins:maven-resources-plugin:: </mojoExecution> </mojoExecutions> </configuration> </plugin> </plugins> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.4</version> </plugin> </plugins> </pluginManagement> </build> </profile> </profiles> </project>
Project Object Model for Flex Library Archetype is very simple, the key to this POM is
the In Project Object Model for Flex Library Archetype, the packaging is very critical. A
POMs packaging type controls the lifecycle it uses to produce build
output. The value Flex Library Archetype’s Sample App Class. package org.sonatype.test { public class App { public static function greeting(name:String):String { return "Hello, " + name; } } }
While this code is underwhelming, it does provide you with a quick model and a quick pointer: "Place More Code Here". While it might seem silly to test code this simple, a sample test named TestApp.as is provides in the src/test/flex directory. This test is shown in Unit Test for Library Archetype’s App Class. Unit Test for Library Archetype’s App Class. package org.sonatype.test { import flexunit.framework.TestCase; public class TestApp extends TestCase { /** * Tests our greeting() method */ public function testGreeting():void { var name:String = "Buck Rogers"; var expectedGreeting:String = "Hello, Buck Rogers"; var result:String = App.greeting(name); assertEquals("Greeting is incorrect", expectedGreeting, result); } } }
To run this build, go to the sample-library project directory and run mvn install. $ mvn install [INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Building sample-library Flex [INFO]task-segment: [install] [INFO] ------------------------------------------------------------------------ [INFO] [resources:resources] [INFO] [flexmojos:compile-swc] [INFO] flexmojos ${flexmojos.version} - GNU GPL License (NO WARRANTY) - \ See COPYRIGHT file [WARNING] Nothing expecified to include. Assuming source and resources folders. [INFO] Flex compiler configurations: -compiler.headless-server=false -compiler.keep-all-type-selectors=false -compiler.keep-generated-actionscript=false -compiler.library-path ~/.m2/repository/com/adobe/flex/framework/flex/\ 3.2.0.3958... -compiler.namespaces.namespace http://www.adobe.com/2006/mxml target/classes/configs/mxml-manifest.xml -compiler.optimize=true -compiler.source-path src/main/flex ... [INFO] [resources:testResources] [WARNING] Using platform encoding (MacRoman actually) to copy filtered \ resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory src/test/resources [INFO] [flexmojos:test-compile] [INFO] flexmojos ${flexmojos.version} - GNU GPL License (NO WARRANTY) - \ See COPYRIGHT file [INFO] Flex compiler configurations: -compiler.include-libraries ~/.m2/repository/org/sonatype/flexmojos/\ flexmojos-unittest-support... -compiler.keep-generated-actionscript=false -compiler.library-path ~/.m2/repository/com/adobe/flex/framework/flex 3.2.0.3958/flex-3.2.0.... -compiler.optimize=true -compiler.source-path src/main/flex target/test-classes src/test/flex -compiler.strict=true -target-player 9.0.0 -use-network=true -verify-digests=true -load-config= [INFO] Already trust on target/test-classes/TestRunner.swf [INFO] [flexmojos:test-run] [INFO] flexmojos ${flexmojos.version} - GNU GPL License (NO WARRANTY) - \ See COPYRIGHT file [INFO] flexunit setup args: null [INFO] ------------------------------------------------------------------------ [INFO] Tests run: 1, Failures: 0, Errors: 0, Time Elpased: 0 sec [INFO] [install:install] NoteTo execute Flex unit tests you will need to configure your PATH environment variable to include the Flash Player. For more information about configuring FlexMojos for unit tests, see Section 13.2.3, “Configuring Environment to Support Flex Unit Tests”. When you ran To create a Flex application from a Maven archetype, execute the following command: $ mvn archetype:generate \ -DarchetypeRepository=http://repository.sonatype.org/content/groups/public\ -DarchetypeGroupId=org.sonatype.flexmojos \ -DarchetypeArtifactId=flexmojos-archetypes-application \ -DarchetypeVersion=${flexmojos.version} [INFO] Scanning for projects... [INFO] Searching repository for plugin with prefix: 'archetype'. [INFO] com.sonatype.maven.plugins: checking for updates from central ... [INFO] [archetype:generate] [INFO] Generating project in Interactive mode [INFO] Archetype defined by properties ... Define value for groupId: : +org.sonatype.test+ Define value for artifactId: : +sample-application+ Define value for version: 1.0-SNAPSHOT: : +1.0-SNAPSHOT+ Define value for package: org.sonatype.test: : +org.sonatype.test+ Confirm properties configuration: groupId: org.sonatype.test artifactId: sample-library version: 1.0-SNAPSHOT package: org.sonatype.test Y: : +Y+ [INFO] Parameter: groupId, Value: org.sonatype.test [INFO] Parameter: packageName, Value: org.sonatype.test [INFO] Parameter: basedir, Value: /Users/Tim/flex-sample [INFO] Parameter: package, Value: org.sonatype.test [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] Parameter: artifactId, Value: sample-application [INFO] BUILD SUCCESSFUL If you look in the directory sample-application/ you will see the filesystem shown in Figure 13.5, “Directory Structure for Flex Application Archetype”. Building an application from the Application archetype produces the following POM. POM for Flex Application Archetype. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.sonatype.test</groupId> <artifactId>sample-application</artifactId> <version>1.0-SNAPSHOT</version> <packaging>swf</packaging> <name>sample-application Flex</name> <build> <sourceDirectory>src/main/flex</sourceDirectory> <testSourceDirectory>src/test/flex</testSourceDirectory> <plugins> <plugin> <groupId>org.sonatype.flexmojos</groupId> <artifactId>flexmojos-maven-plugin</artifactId> <version>3.5.0</version> <extensions>true</extensions> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>com.adobe.flex.framework</groupId> <artifactId>flex-framework</artifactId> <version>3.2.0.3958</version> <type>pom</type> </dependency> <dependency> <groupId>com.adobe.flexunit</groupId> <artifactId>flexunit</artifactId> <version>0.85</version> <type>swc</type> <scope>test</scope> </dependency> </dependencies> <profiles> <profile> <id>m2e</id> <activation> <property> <name>m2e.version</name> </property> </activation> <build> <plugins> <plugin> <groupId>org.maven.ide.eclipse</groupId> <artifactId>lifecycle-mapping</artifactId> <version>0.9.9-SNAPSHOT</version> <configuration> <mappingId>customizable</mappingId> <configurators> <configurator id='org.maven.ide.eclipse.configuration.flex.configurator' /> </configurators> <mojoExecutions> <mojoExecution> org.apache.maven.plugins:maven-resources-plugin:: </mojoExecution> </mojoExecutions> </configuration> </plugin> </plugins> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.4</version> </plugin> </plugins> </pluginManagement> </build> </profile> </profiles> </project>
The difference between POM for Flex Application Archetype and
Project Object Model for Flex Library Archetype is that the Sample Application Main.mxml. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> <mx:Text text="Hello World!"/> </mx:Application>
This application also creates a simple FlexUnit test that does nothing more than print out a trace message. The sample unit test is in src/test/flex/org/sonatype/test. Unit Test for Main.mxml. package org.sonatype.test { import flexunit.framework.TestCase; import Main; public class TestApp extends TestCase { public function testNothing():void { //TODO un implemented trace("Hello test"); } } }
To create a multi-module project consisting of a Flex Library project referenced by a Flex Application, referenced by a Web Application. $ mvn archetype:generate \ -DarchetypeRepository=http://repository.sonatype.org/content/groups/public\ -DarchetypeGroupId=org.sonatype.flexmojos \ -DarchetypeArtifactId=flexmojos-archetypes-modular-webapp \ -DarchetypeVersion=${flexmojos.version} [INFO] Scanning for projects... [INFO] Searching repository for plugin with prefix: 'archetype'. [INFO] com.sonatype.maven.plugins: checking for updates from central ... [INFO] [archetype:generate] [INFO] Generating project in Interactive mode [INFO] Archetype defined by properties ... Define value for groupId: : +org.sonatype.test+ Define value for artifactId: : +sample-multimodule+ Define value for version: 1.0-SNAPSHOT: : +1.0-SNAPSHOT+ Define value for package: org.sonatype.test: : +org.sonatype.test+ Confirm properties configuration: groupId: org.sonatype.test artifactId: sample-library version: 1.0-SNAPSHOT package: org.sonatype.test Y: : +Y+ [INFO] Parameter: groupId, Value: org.sonatype.test [INFO] Parameter: packageName, Value: org.sonatype.test [INFO] Parameter: basedir, Value: /Users/Tim [INFO] Parameter: package, Value: org.sonatype.test [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] Parameter: artifactId, Value: sample-multimodule [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL If you look in the sample-multimodule/ directory, you will see a directory structure which contains three projects swc, swf, and war. The simple top-level POM in this multimodule project is shown in . It consists of module references to the swc, swf, and war modules. Top-level POM Created by Modular Web Application Archetype. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.sonatype.test</groupId> <artifactId>sample-multimodule</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>swc</module> <module>swf</module> <module>war</module> </modules> </project>
The swc project has a simple POM that resembles the POM shown in
Project Object Model for Flex Library Archetype. Note that the swc Module POM. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.sonatype.test</groupId> <artifactId>sample-multimodule</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>org.sonatype.test</groupId> <artifactId>swc</artifactId> <version>1.0-SNAPSHOT</version> <packaging>swc</packaging> <name>swc Library</name> <build> <sourceDirectory>src/main/flex</sourceDirectory> <testSourceDirectory>src/test/flex</testSourceDirectory> <plugins> <plugin> <groupId>org.sonatype.flexmojos</groupId> <artifactId>flexmojos-maven-plugin</artifactId> <version>3.5.0</version> <extensions>true</extensions> <configuration> <locales> <locale>en_US</locale> </locales> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>com.adobe.flex.framework</groupId> <artifactId>flex-framework</artifactId> <version>3.2.0.3958</version> <type>pom</type> </dependency> <dependency> <groupId>com.adobe.flexunit</groupId> <artifactId>flexunit</artifactId> <version>0.85</version> <type>swc</type> <scope>test</scope> </dependency> </dependencies> <profiles> <profile> <id>m2e</id> <activation> <property> <name>m2e.version</name> </property> </activation> <build> <plugins> <plugin> <groupId>org.maven.ide.eclipse</groupId> <artifactId>lifecycle-mapping</artifactId> <version>0.9.9-SNAPSHOT</version> <configuration> <mappingId>customizable</mappingId> <configurators> <configurator id="org.maven.ide.eclipse.configuration.flex.configurator" /> </configurators> <mojoExecutions> <mojoExecution> org.apache.maven.plugins:maven-resources-plugin:: </mojoExecution> </mojoExecutions> </configuration> </plugin> </plugins> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.4</version> </plugin> </plugins> </pluginManagement> </build> </profile> </profiles> </project>
The swf module’s POM resembles the POM in POM for Flex Application Archetype
adding a dependency on the <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.sonatype.test</groupId> <artifactId>sample-multimodule</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>org.sonatype.test</groupId> <artifactId>swf</artifactId> <version>1.0-SNAPSHOT</version> <packaging>swf</packaging> <name>swf Application</name> <build> <sourceDirectory>src/main/flex</sourceDirectory> <testSourceDirectory>src/test/flex</testSourceDirectory> <plugins> <plugin> <groupId>org.sonatype.flexmojos</groupId> <artifactId>flexmojos-maven-plugin</artifactId> <version>3.5.0</version> <extensions>true</extensions> <configuration> <locales> <locale>en_US</locale> </locales> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>com.adobe.flex.framework</groupId> <artifactId>flex-framework</artifactId> <version>3.2.0.3958</version> <type>pom</type> </dependency> <dependency> <groupId>com.adobe.flexunit</groupId> <artifactId>flexunit</artifactId> <version>0.85</version> <type>swc</type> <scope>test</scope> </dependency> <dependency> <groupId>org.sonatype.test</groupId> <artifactId>swc</artifactId> <version>1.0-SNAPSHOT</version> <type>swc</type> </dependency> </dependencies> <profiles> <profile> <id>m2e</id> <activation> <property> <name>m2e.version</name> </property> </activation> <build> <plugins> <plugin> <groupId>org.maven.ide.eclipse</groupId> <artifactId>lifecycle-mapping</artifactId> <version>0.9.9-SNAPSHOT</version> <configuration> <mappingId>customizable</mappingId> <configurators> <configurator id="org.maven.ide.eclipse.configuration.flex.configurator" /> </configurators> <mojoExecutions> <mojoExecution> org.apache.maven.plugins:maven-resources-plugin:: </mojoExecution> </mojoExecutions> </configuration> </plugin> </plugins> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.4</version> </plugin> </plugins> </pluginManagement> </build> </profile> </profiles> </project>
When you declare a dependency on a SWC, you’ll need to specify the
type of the dependency so that Maven can locate the appropriate
artifact in the remote or local repository. In this case, the
Next, take a look at the simple POM in the war module. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>sample-multimodule</artifactId> <groupId>org.sonatype.test</groupId> <version>1.0-SNAPSHOT</version> </parent> <groupId>org.sonatype.test</groupId> <artifactId>war</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <build> <plugins> <plugin> <groupId>org.sonatype.flexmojos</groupId> <artifactId>flexmojos-maven-plugin</artifactId> <version>3.5.0</version> <executions> <execution> <goals> <goal>copy-flex-resources</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.17</version> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.sonatype.test</groupId> <artifactId>swf</artifactId> <version>1.0-SNAPSHOT</version> <type>swf</type> </dependency> </dependencies> </project>
The POM shown in war module POM configures the FlexMojos
plugin to execute the To build the multimodule web application project, run NoteTo execute Flex unit tests you will need to configure your PATH environment variable to include the Flash Player. For more information about configuring FlexMojos for unit tests, see Section 13.2.3, “Configuring Environment to Support Flex Unit Tests”. |