Background

I am using Maven for build and release management for last 4years or so. But recently when I started using ‘Flex Mojos’ plug-in for building my Flex project I realized that I do not have some of the basic knowledge about the Maven. I have to admit that I had only 30-40% of knowledge. For last few days I started reading “Maven – The Definitive Guide” from Sonatype and thought I will share some of the core concepts. I am planning to split this in to 2 parts. In first part I will cover core concepts about the Maven and in 2nd part I will cover different useful plug-ins and important commands.

Audience

This article assumes that you have some working knowledge of Maven2 and this should be used as a reference only.

Core Concepts

The core of Maven is pretty dumb, it doesn’t know how to do much beyond parsing few XML documents and keeping track of few lifecycle and few plugins. Most of the stuff is delegated to plugins.

Convention over Configuration    
Systems, libraries, and frameworks should assume reasonable defaults. Without requiring unnecessary configuration, systems should “just work”. Popular frameworks “Ruby On Rails” and EJB3 are based on this concept.

I have seen people breaking this rule and saying Maven is “stupid” it assumes my source files in “src\main\java” directory and I do not want to adhere to this folder structure. That is fine you can use whatever the structure you want and Maven doesn’t put any restriction on this but in your POM, make sure you tell that to Maven. But think again when you break any of these rules. I would do this only if somebody else owns this project and is a legacy code.

Plugins and Goals

A Maven plugin is a collection of one or more goals.

A goal is a “unit of work”. It is a specific task that may be executed as standalone goal or along with other goals as part of larger build.

     mvn archetype:create        (pluginId:goalId)
Maven Lifecycle

The build lifecycle is an ordered sequence of phases involved in building a project. Most often used is the default Maven lifecycle, which begins with a phase to validate the basic integrity of the project and ends with a phase that involves deploying a project to production.

 Plugin goals can be attached to a lifecycle phase. As Maven moves through the phases in a lifecycle, it will execute the goals attached to each particular phase. Each phase may have zero or more goals bound to it.

 mvn install        (install – It is one of lifecycle phase)
Maven Co-ordinates

Maven Coordinates define a set of identifiers which can be used to uniquely identify a project, a dependency, or a plugin in a Maven POM

Maven Repositories

When you run Maven for the first time, you will notice that Maven downloads a number of files from a remote Maven repository. Maven ships with the bare minimum and fetches from a remote repository when it needs to. Maven ships with a default remote repository location (http://repo1.maven.org/maven2)

Maven Dependency (Transitive Dependency)

A dependency in Maven isn’t just a JAR file; it’s a POM file that, in turn, may declare dependencies on other artifacts. These dependencies of dependencies are called transitive dependencies

Optimizing Dependencies

In a multi module project use “dependencyManagement” section to categorize all the common dependency jar’s between projects. This avoids dependency duplication and sibling dependency mis-match.

<project>
…..
 <dependencyManagement>
 <dependencies>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring</artifactId>
 <version>2.0.7</version>
 </dependency>
 <dependencies>
 <dependencyManagement>
</project>
Optimizing Plugins

Use “pluginManagement” section in top level POM just like the way we use for Dependency management.

<project>
…..
 <build>
<pluginManagement>
<plugins>
<plugin>
 <groupId> org.apache.maven.plugins </groupId>
 <artifactId>maven-complier-plugin</artifactId>
 <configuration>
 <source>1.5</source>
 <target>1.5</target>
 </configuration>
 </plugin>
 </plugins>
 < pluginManagement >
 </build>
</project>

Share and Enjoy:
  • del.icio.us
  • TwitThis
  • LinkedIn
  • StumbleUpon
  • Facebook
  • Google
 | Posted by Srinivas Kusunam | Categories: Maven |

Recently I have started working on my first Flex project. It is been 2 weeks or so and first question came to my mind was how do I structure my project so that I can build it using Maven? I have decided to use ‘info.flex-mojos‘ project and also seriously considered his suggestion about one single Flex project should not generate multiple SWF files read it at his blog “Multiple SWF from same Sources“.

Requirement

  • Need to build 2 WAR files, one WAR file need to have 2 main application MXML files which are accessed through a direct URL (one for each) and 2nd one with a single MXML application file

Setup

Here is the structure I have setup for this project:

MyProjectName

– model (Hibernate model classes common for both exceptionsweb and formsweb)

– exceptions_web (web project which needs to have 2 main SWF files from modules screen1_module and screen2_module)

– forms_web (web project which contains 1 main SWF file from forms_module)

– ria

– shared_module_lib (This is the shared library or RSL used in all the 3 Flex main modules)

– screen1_module

– screen2_module

– forms_module


Build process
With this project structure Flex modules always generate new version with every build and we need to make sure that HTML or JSP file which wraps corresponding SWF file needs to be updated dynamically during the maven build process. Luckily we have ‘html-wrapper-mojo’ plugin available from the same project ‘info.flex-mojos’. As the requirement is to have 2 SWF files in ‘exceptionsweb’ WAR file, this plugin has a limitation of allowing only 1 SWF. This made me to spend more 8-20hrs and build it through a workaround i.e. using ‘maven-assembly-plugin’ build 2nd flex project (screen1_module) as ZIP and unpack it in to ‘exceptions_web’ war file.

Here is the order of the build:

  • Build flex projects first
    • Build ’shared_module_lib’ project as ‘SWC’ i.e. <packaging>swc</packaging> which will be referred as RSL in other projects
    • Build ’screen1_module’ project as regular SWF
      • This project also needs to use ‘html-wrapper-mojo‘ plugin to dynamically creating ${project.build.finalName}.html (eg: screen1_module-1.0.0-SNAPSHOT.swf) with a link to current version of SWF file it is building
      • Next use ‘maven-antrun-plugin‘ to move above generated file (eg: screen1_module-1.0.0-SNAPSHOT.html) to the name by which users access i.e. Screen1.html
      • Use ‘maven-assembly-plugin‘ to generate a zip file which will have Screen1.html, Screen1-1.0.0.swf and any history and player files which will be extracted in to WAR project
    • Build ’screen2_module’ project as regular SWF
    • Build ‘forms_module’ project as regular SWF
  • Build ‘model’ project as ‘jar’ project
  • Build ‘exceptions_web’ project as ‘war’
    • Add screen2_module files – We need to generate Screen2-1.0.0-SNAPSHOT.html which will be used to access the corresponding SWF file. For this we need to use ‘html-wrapper-mojo’ plugin to dynamically creating ${project.build.finalName}.html with a link to current version of SWF file it is building
    • Next use ‘maven-antrun-plugin’ to move above generated file (eg: Screen2-1.0.0-SNAPSHOT.html) to the name by which users access i.e. Screen2.html
    • Use ‘maven-dependency-plugin’ to unpack the ’screen1_module’ zip file
    • Use ‘maven-dependency-plugin’ to copy player Adobe framework SWF and SWZ files to the WAR build
  • Build ‘forms_web’ project as ‘war’
    • Add forms_module files – We need to generate Forms-1.0.0-SNAPSHOT.html which is a wrapper to corresponding SWF file. For this we need to use ‘html-wrapper-mojo’ plugin
    • Next use ‘maven-antrun-plugin’ to move above generated file (eg: Forms-1.0.0-SNAPSHOT.html) to the name by which users access i.e. Forms.html
    • Use ‘maven-dependency-plugin’ to copy player Adobe framework SWF and SWZ files to the WAR build

** This is my first attempt of building Flex with Maven. Any suggestions or questions are welcome.
Top Level POM which builds all the sub projects.

<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>com.company.project</groupId>
	<artifactId>multimodule</artifactId>
	<name>Multi module Parent project</name>
	<packaging>pom</packaging>
	<version>1.0.0-SNAPSHOT</version>
	<modules>
		<module>ria</module>
		<module>model</module>
		<module>exceptions_web</module>
		<module>forms_web</module>
	</modules>
</project>

Download complete project structure with Maven scripts here

For the latest release of flex-mojos please access this wiki and also there is a very good example there in building Flex / Java and BlazeDS  i.e.  Reference application.
If you have any questions use this friendly group @ http://code.google.com/p/flex-mojos/

Share and Enjoy:
  • del.icio.us
  • TwitThis
  • LinkedIn
  • StumbleUpon
  • Facebook
  • Google
 | Posted by Srinivas Kusunam | Categories: Flex, Maven |