Getting started with multiproject maven2

I'm helping a group of students to split up their codebase into a couple of manageable project using maven2 as the build tool. Their existing project is a layered Spring MVC web application. The following layers are present:

  • data model
  • dao / integration
  • services
  • web application 1

Since dependencies where not managed yet the codebase managed to drift into containing spring 2 and spring 1.2.6 libs without anybody overseeing the classpath... something maven should really help to fix.

Once you've got maven2 installed it is quite easy to setup a project structure which packages and versions of the different layers in different libraries (jars) so they can be reused for different purposes.

First we'll define the parent project, which will contain dependencies and configuration shared between all layers:

mvn archetype:create -DgroupId=wosi -DartifactId=wosi

This will create a directory with the artifactId as name containing pom file (project object model) and a source directory. We won't need the source directory so it can be wiped.

Since the parent project will aggregate a couple of 'modules' the packaging should be changed from the default 'jar' to 'pom':

XML:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3.   <modelVersion>4.0.0</modelVersion>
  4.   <groupId>wosi</groupId>
  5.   <artifactId>wosi</artifactId>
  6.   <packaging>pom</packaging>
  7.   <version>1.0-SNAPSHOT</version>
  8.   <name>wosi</name>
  9.   <url>http://maven.apache.org</url>
  10.   <dependencies>
  11.     <dependency>
  12.       <groupId>junit</groupId>
  13.       <artifactId>junit</artifactId>
  14.       <version>3.8.1</version>
  15.       <scope>test</scope>
  16.     </dependency>
  17.   </dependencies>
  18. </project>

We can leave the dependency on junit, since we'll use it in all projects.

Now, if we create projects from the previously generated directory they'll be registered as 'modules' automatically. Let's create the different projects. From the command line run:

CODE:
  1. mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=wosi -DartifactId=wosi-domain -DpackageName=org.wosi.domain -Dversion=1.0
  2.  
  3. mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=wosi -DartifactId=wosi-integration -DpackageName=org.wosi.integration -Dversion=1.0
  4.  
  5. mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=wosi -DartifactId=wosi-services -DpackageName=org.wosi.services -Dversion=1.0
  6.  
  7. mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=wosi -DartifactId=wosi-utils -DpackageName=org.wosi.utils -Dversion=1.0
  8.  
  9. # mind the fact that we use the maven-archetype-webapp to create the webapp!!!
  10. mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=wosi -DartifactId=wosi-web -DpackageName=org.wosi.web -Dversion=1.0

Now if we look at the parent pom we'll see the different module entries:

XML:
  1. ...
  2.   <modules>
  3.     <module>wosi-domain</module>
  4.     <module>wosi-integration</module>
  5.     <module>wosi-services</module>
  6.     <module>wosi-utils</module>
  7.     <module>wosi-web</module>
  8.   </modules>
  9. ...

And all module poms will contain a reference to the parent:

XML:
  1. ...
  2.   <parent>
  3.     <artifactId>wosi</artifactId>
  4.     <groupId>wosi</groupId>
  5.     <version>1.0-SNAPSHOT</version>
  6.   </parent>
  7. ...

Now it's possible to compile or install all projects from the root directory of the project (mvn install) or you can compile any of the subprojects separately.

Useful commands to know are:

  • validate : validate the project is correct and all necessary information is available
  • compile : compile the source code of the project
  • test : test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package : take the compiled code and package it in its distributable format, such as a JAR.
  • integration-test : process and deploy the package if necessary into an environment where integration tests can be run
  • verify : run any checks to verify the package is valid and meets quality criteria
  • install : install the package into the local repository, for use as a dependency in other projects locally
  • deploy : done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
  • clean : cleans up artifacts created by prior builds
  • site : generates site documentation for this project

By default subprojects are not dependent on any of the other projects, it is however quite easy to create dependencies. For instance, say I want to have a dependency from my service layer on my integration layer. Just at it to the pom of the service project:

XML:
  1. <dependencies>
  2. ...
  3.   <dependency>
  4.     <artifactId>wosi-integration</artifactId> 
  5.     <groupId>wosi</groupId> 
  6.     <version>1.0</version>
  7.   </dependency>
  8. ...
  9. </dependencies>

Using the projects in Eclipse
It is possible to use the projects in Eclipse without any modifications (make sure to configure eclipse correctly though). Just run 'mvn eclipse:eclipse' from the root and import the generated projects using eclipse. If you would however like to use Eclipse's web tools to deploy webapplications a small modification to the parent pom is needed, ad a wtp configuration to the build/plugins section:

XML:
  1. <build>
  2.   <plugins>
  3.     <plugin>
  4.       <groupId>org.apache.maven.plugins</groupId>
  5.       <artifactId>maven-eclipse-plugin</artifactId>
  6.       <version>2.2</version>
  7.       <configuration>
  8.         <wtpversion>1.0</wtpversion>
  9.       </configuration>
  10.     </plugin>
  11.   </plugins>
  12. </build>

Now, after generating the eclipse projects again all web projects in the project should get configured for WST.


1 Response to “Getting started with multiproject maven2”

  1. 1 Integrate Maven with Version Control System | twit88.com

Leave a Reply





About

Welcome to the weblog of Peter Maas. Here you'll find various posts related to stuff I like (like my kids and espresso) and stuff I do (like developing software).

JavaOne 2008 Pictures

pub crab IMG_4595 IMG_4706.JPG the building IMG_4572 IMG_4597 kleine libelle IMG_4556.JPG breakfast IMG_4609.JPG IMG_4611.JPG robin_met_zijn_hyundai_horloge_foon spidercrab IMG_6147 IMG_4684.JPG olijfboom IMG_4707.JPG Greenland IMG_4682.JPG
View more photos >

Categories



Meld u aan voor PayPal en begin direct met het accepteren van creditcardbetalingen.