Maven

 

 

Information available at  http://maven.apache.org/guides/getting-started/index.html

Preliminary remarks on a maven project from the maven site above.   This site includes documentation on adding resources, filters and building more than one project at a time.

 

Generate directory structure using maven archtype.  On command line type:

 

mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.mycompany.app -DartifactId=my-app

 

This generates a maven project with names as set above.

my-app
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- com
    |           `-- mycompany
    |               `-- app
    |                   `-- App.java
    `-- test
        `-- java
            `-- com
                `-- mycompany
                    `-- app
                        `-- AppTest.java

 

Generated pom file (Project Object Model (POM) for this project):

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Maven Quick Start Archetype</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

 

This is a very simple POM but still displays the key elements every POM contains, so let's walk through each of them to familiarize you with the POM essentials:

  • project This is the top-level element in all Maven pom.xml files.
  • modelVersion This element indicates what version of the object model this POM is using. The version of the model itself changes very infrequently but it is mandatory in order to ensure stability of use if and when the Maven developers deem it necessary to change the model.
  • groupId This element indicates the unique identifier of the organization or group that created the project. The groupId is one of the key identifiers of a project and is typically based on the fully qualified domain name of your organization. For example org.apache.maven.plugins is the designated groupId for all Maven plug-ins.
  • artifactId This element indicates the unique base name of the primary artifact being generated by this project. The primary artifact for a project is typically a JAR file. Secondary artifacts like source bundles also use the artifactId as part of their final name. A typical artifact produced by Maven would have the form <artifactId>-<version>.<extension> (for example, myapp-1.0.jar).
  • packaging This element indicates the package type to be used by this artifact (e.g. JAR, WAR, EAR, etc.). This not only means if the artifact produced is JAR, WAR, or EAR but can also indicate a specific lifecycle to use as part of the build process. (The lifecycle is a topic we will deal with further on in the guide. For now, just keep in mind that the indicated packaging of a project can play a part in customizing the build lifecycle.) The default value for the packaging element is JAR so you do not have to specify this for most projects.
  • version This element indicates the version of the artifact generated by the project. Maven goes a long way to help you with version management and you will often see the SNAPSHOT designator in a version, which indicates that a project is in a state of development. We will discuss the use of snapshots and how they work further on in this guide.
  • name This element indicates the display name used for the project. This is often used in Maven's generated documentation.
  • url This element indicates where the project's site can be found. This is often used in Maven's generated documentation.
  • description This element provides a basic description of your project. This is often used in Maven's generated documentation.

Compiling the app:

C:\eclipse workspace\my-app>mvn compile

[INFO] Scanning for projects...

[INFO] ------------------------------------------------------------------------

[INFO] Building my-app

[INFO]    task-segment: [compile]

[INFO] ------------------------------------------------------------------------

[INFO] [resources:resources]

[INFO] Using default encoding to copy filtered resources.

[INFO] [compiler:compile]

[INFO] Compiling 1 source file to C:\eclipse workspace\my-app\target\classes

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESSFUL

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 1 second

[INFO] Finished at: Sat Apr 11 11:12:35 EDT 2009

[INFO] Final Memory: 3M/10M

[INFO] ------------------------------------------------------------------------

 

C:\eclipse workspace\my-app>

 

Running tests

 

C:\eclipse workspace\my-app>mvn test

[INFO] Scanning for projects...

[INFO] ------------------------------------------------------------------------

[INFO] Building my-app

[INFO]    task-segment: [test]

[INFO] ------------------------------------------------------------------------

[INFO] [resources:resources]

[INFO] Using default encoding to copy filtered resources.

[INFO] [compiler:compile]

[INFO] Nothing to compile - all classes are up to date

[INFO] [resources:testResources]

[INFO] Using default encoding to copy filtered resources.

[INFO] [compiler:testCompile]

[INFO] Compiling 1 source file to C:\eclipse workspace\my-app\target\test-classe

s

[INFO] [surefire:test]

[INFO] Surefire report directory: C:\eclipse workspace\my-app\target\surefire-re

ports

 

-------------------------------------------------------

 T E S T S

-------------------------------------------------------

Running com.mycompany.app.AppTest

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.031 sec

 

Results :

 

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

 

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESSFUL

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 3 seconds

[INFO] Finished at: Sat Apr 11 11:13:41 EDT 2009

[INFO] Final Memory: 5M/11M

[INFO] ------------------------------------------------------------------------

 

C:\eclipse workspace\my-app>

Other maven goals:

mvn test-compile

 Would compile but not run tests.

mvn package would create a jar file

mvn install would install the jar into your local repository.

mvn site generates a website for your project

mvn clean will remove the target directory

Perhaps you'd like to generate an IntelliJ IDEA descriptor for the project?

mvn idea:idea

This can be run over the top of a previous IDEA project - it will update the settings rather than starting fresh.

If you are using Eclipse IDE, just call:

mvn eclipse:eclipse

 

Building a webapp with maven

First use archetype to generate file structure: (all on one line)

 

C:\eclipse workspace>mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=com.mycompany.

app -DartifactId=my-webapp

[INFO] Scanning for projects...

[INFO] Archetype [org.apache.maven.archetypes:maven-archetype-webapp:RELEASE] fo

und in catalog internal

Define value for version:  1.0-SNAPSHOT: :

Confirm properties configuration:

groupId: com.mycompany.app

artifactId: my-webapp

version: 1.0-SNAPSHOT

package: com.mycompany.app

 Y: :

[INFO] -------------------------------------------------------------------------

---

[INFO] Using following parameters for creating OldArchetype: maven-archetype-web

app:RELEASE

[INFO] -------------------------------------------------------------------------

---

[INFO] Parameter: groupId, Value: com.mycompany.app

[INFO] OldArchetype created in dir: C:\eclipse workspace\my-webapp

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESSFUL

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 21 seconds

[INFO] Finished at: Sat Apr 11 12:17:54 EDT 2009

[INFO] Final Memory: 8M/14M

[INFO] ------------------------------------------------------------------------

 

C:\eclipse workspace>

 

Run mvn clean package to create war file

An index.jsp is included in the empty project.

 

Creating a multimodule flex project using maven

 

This tutorial at

http://www.mindtheflex.com/?p=55

 

First use maven archetype to generate the parent directory structure:

 

C:\eclipse workspace>mvn archetype:generate -DarchetypeGroupId=org.apache.maven.

archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=multiModPr

oj -DartifactId=multiModProj -DpackageName=org.mypackage -Dversion=1.0

[INFO] Scanning for projects...

package: multiModProj

 Y: : y

[INFO] -------------------------------------------------------------------------

---

[INFO] Using following parameters for creating OldArchetype: maven-archetype-qui

ckstart:RELEASE

[INFO] -------------------------------------------------------------------------

---

[INFO] Parameter: groupId, Value: multiModProj

[INFO] Parameter: packageName, Value: multiModProj

[INFO] Parameter: package, Value: multiModProj

[INFO] Final Memory: 8M/15M

[INFO] ------------------------------------------------------------------------

 

C:\eclipse workspace>

 

In your main pom, change packaging type from jar to pom: <packaging>pom</packaging>

 

Go into this main project dir and run maven again to create a web app (on one line):

 

C:\eclipse workspace\multiModProj>mvn archetype:generate -DarchetypeGroupId=org.

apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=m

ultiModProj -DartifactId=multiModProjWeb -DpackageName=org.test -Dversion=1.0

[INFO] Scanning for projects...

[INFO] [archetype:generate]

[INFO] Generating project in Interactive mode

[INFO] BUILD SUCCESSFUL

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 5 seconds

[INFO] Finished at: Sat Apr 11 12:41:40 EDT 2009

[INFO] Final Memory: 8M/15M

[INFO] ------------------------------------------------------------------------

 

C:\eclipse workspace\multiModProj>

Now create flex part with maven

 

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-flex -DarchetypeVersion=1.0 -DarchetypeGroupId=dk.jacobve.maven.archetypes  -DgroupId=multiModProj -DartifactId=multiModProjFlex -DpackageName=org.test -Dversion=1.0

 

Here’s what we’ve got so far:

 

 

 

I did not find I needed to modify the parent pom to add the (child) flex and web modules – maven did that.

 

Tutorial shows now adding plugins etc to webapp pom build goal  ---see the tutorial for details!!!

<plugin>
  <groupId>net.israfil.mojo</groupId>
   <artifactId>maven-flex2-plugin</artifactId>
   <executions>
     <execution>
       <phase>compile</phase>
       <id>copy-flex</id>
       <goals>
         <goal>copy-flex-applications</goal>
       </goals>
       <configuration>
         <modules>
          <swfModule>
            <groupId>multiModuleProjectId</groupId>
            <artifactId>multiModuleProjectArtifactIdFlex</artifactId>
            <targetPath>target/multiModuleProjectArtifactIdWeb/</targetPath>
          </swfModule>
         </modules>
        </configuration>
      </execution>
   </executions>
</plugin>

 

 

<plugin>
  <artifactId>maven-antrun-plugin</artifactId>
  <executions>
    <execution>
        <phase>compile</phase>
        <configuration>
            <tasks>
               <taskdef
                   className="flex.ant.HtmlWrapperTask" name="html-wrapper"
                   classpath="${flex.home}\ant\lib\flexTasks.jar" />
               <property name="FLEX_HOME" value="${flex.home}" />
               <property name="APP_ROOT" value="target/multiModuleProjectArtifactIdWeb" />
               <html-wrapper
                      title="Welcome to My Flex App"
                      file="index.html"
                      height="300"
                      width="400"
                      bgcolor="red"
                      application="app"
                      swf="multiModuleProjectArtifactIdFlex-1.0-SNAPSHOT"
                      version-major="9"
                      version-minor="0"
                      version-revision="0"
                      history="true"
                      template="express-installation"
                      output="${APP_ROOT}" />
           </tasks>
         </configuration>
         <goals>
           <goal>run</goal>
         </goals>
      </execution>
   </executions>
</plugin>

 

You only need to run poms in child directories.  The war file is in the target directory of the web child.  I have to confess I still had to monkey with extensions to get it to deploy correctly.

Deployed in tomcat:

 

A fairly complicated multi-module flex maven project

 

http://www.servebox.org/2009/01/sample-multi-modules-flexj2ee-web-application/

This is a sample flex3/J2EE project configured as a multi-module maven project. You can use this structure as a basis to build your own project. The Flex project should be considered as a dependency of the Web app project (the WAR). Using the maven dependency plugin, the swf artifact is copied to the web application root folder.

The project contains three modules :

  • flex-gui : the SWF artifact,
  • java-service : a sample Java facade,
  • web-app : the Web application, depending on both the flex-gui and java-services artifacts.

flex-gui project

The “flex-gui” project is a swf flex nature project which contains .as and .mxml source files. This module produces a SWF artifact (packaging: swf).

java-service project

The “java-service” project is a standard java project which contains a java facade, providing data to the GUI. This module produces a JAR artifact (packaging: jar).

web-app project

The “web-app” project is a Web application project that contains  configuration files needed to deploy the application in a container, such as Tomcat. Other resources may also be included, such as JSP, servlet, images… This module produces a WAR artifact (packaging: war).

You can add other modules to create your own project, more often those projetcs could be  dedicated to the business layer, or persistence layer.

Requirements

To build this project you will need :

Dependencies

  • This sample is based on the Maven Flex Plugin 2.1.1. Check the homepage of the project to see more.
  • The Flex SDK version used is 3.2.0.3958.
  • As an IDE you may use FlexBuilder 3.0.2.xxx, or any text editor

 

Download the source, unzip, and in the root directory of extraction run maven:

 

mvn flex:eclipse eclipse:eclipse
This will download many files.
 
It is actually not nec to do this unless you will build the project and run it in eclipse. See notes below.  In any case, it won’t work because of dependencies so the indiv poms will have to be executed first. (see below.)
 
 
You’ll have to fix dir names in pom.xml
 
<properties>
        <tomcat.managerurl>http://localhost:8080/manager/html</tomcat.managerurl>
        <tomcat.serverprofile></tomcat.serverprofile>
        <webapp.name>sample-multi-project</webapp.name>
        <tomcat.server.root>C:\…tomcat root here</tomcat.server.root>
        <tomcat.server.user>tomcat</tomcat.server.user>
        <tomcat.server.password>tomcat</tomcat.server.password>

    </properties>

 

If you want to deploy automatically make sure tomcat is running and the tomcat user is defined as below in …/conf/tomcat-users.xml:

 

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
  <role rolename="tomcat"/>
  <role rolename="manager"/>
  <role rolename="admin"/>
  <user username="tomcat" password="tomcat" roles="tomcat,admin,manager"/>
  <user username="admin" password="" roles="admin,manager"/>
</tomcat-users>

 

The url in the mxml file is wrong. You’ll have to go change that to localhost:8080/yourappname/..

 

 

I tried running

 mvn flex:eclipse eclipse:eclipse

but because of the way the dependencies were defined maven thought it had to download the jar files it was trying to build. Instead, run mvn package and mvn install first in the flex-gui directory and then in the java-service directory to execute those pom files. Then it is possible to go to the root and run

mvn package

command to create the war file.  It will be in the target directory.   Servlet api jar is downloaded to the lib folder which is not allowed in tomcat or in the war file specification.  This has to be removed.  You can get maven to deploy, in theory. I moved the war file to tomcat webapps by hand.

 

You can generate voluminous documentation by running

mvn site

at the top level pom. This will show conflicts in jars.

There’s not a whole lot there but…

 

 

 

Now… building a more significant project

 

Proper steps: use maven archtype to generate file structure.

 

I did not do this. I copied the file structure from this sample.  I left most everything alone except: changed the name to sample-muti-project2 in the pom and in the index.html.  I needed to rename the swf to sample-muti-project2.swf by hand – don’t know why.  

 

I replaced the mostly empty java façade class with my own class that defines a TreeMap for state abbreviation lookup:

package org.servebox.sample.javaservice;

import java.util.*;

import java.io.*;

 

public class JavaServiceFacade{

 

TreeMap tm;

 

public  JavaServiceFacade (){

tm=new TreeMap();

 tm.put("ALABAMA","AL");

 tm.put("ALASKA ","AK");

//…all the rest

tm.put("WISCONSIN", "WI");

tm.put("WYOMING",  "WY");   }

 

public String lookUp(String key){

  if(tm.containsKey(key))

  return (String)tm.get(key);

  else return(key+" has no value in this map");

}//fn

 

}

 

 

I added a textinput to main.mxml and changed the button click action – here’s just the changed part of the mxml:

 

  <mx:Label text="Sample Flex Application"/>

    <mx:TextInput id="state" width="100"/>

    <mx:Button label="state lookup" click="ro.lookUp(state.text)"/>

 

I ran mvn compile, package and install from both the flex-gui and java-service directories, then ran mvn package from the root.

Deployed webapp: