Maven - Build a Multiple Module Project with Maven - maven tutorial



Introduction

  • This document is based on:
  • Eclipse 4.6 (NEON)
  • You are viewing the advanced Maven. If you are a beginner Maven. You should look at the documentation for beginners Maven (Maven Hello world) at:
maven tutorial tags : apache maven , maven repository , maven central

Model example

  • This is the model example in this document.
 maven multiple project

learn maven tutorial - maven multiple project - maven example

  • The objective of the guidelines is:
  • How a module using other module in Maven
  • Packing multiple Module using Maven (output: jar, war).
  • MathWebApp: is a WebApp project
  • MathLibrary: is a library Project, contains utility classes used by MathWebApp.
  • MavenParent: will pack two projects above, it is a parent module. MavenParent will:
    • Packing MathLibary to a jar file
    • Packing MathWebApp to a file war.

Create project MathLibrary

 maven multiple project select a wizard

learn maven tutorial - maven multiple project select a wizard - maven example

  • This is a simple Project, so we don't need to select a Maven archetype.
  • Select:
  • Create a simple project (skip archetype selection)
 maven multiple project new

learn maven tutorial - maven example maven multiple project new -

  • Group Id: org.o7planning
  • Artifact Id: MathLibrary
  • Packaging: jar
  • Skip parent module information.
 maven multiple configure project

learn maven tutorial - maven multiple configure project - maven example

  • Project is created.
 maven multiple project project explorer

learn maven tutorial - maven multiple project project explorer - maven example

  • Create class MathUtils:

MathUtils.java code

package org.o7planning.mathutils;
 
public class MathUtils {
 
  public static int sum(int a, int b) {
      return a + b;
  }
 
}

Create project MathWebApp

 maven multiple select projectname location

learn maven tutorial -maven multiple select projectname location - maven example

 maven multiple select new project

learn maven tutorial - maven multiple select new project - maven example

  • Select maven-archetype-webapp archetype. Eclipse will create a Project Maven that has the structure of a web application.
 maven multiple project select archetype

learn maven tutorial -maven multiple project select archetype - maven example

  • Group Id: org.o7planning
  • Artifact Id: MathWebApp
  • Version: 0.0.2-SNAPSHOT
  • Package: org.o7planning.mathwebapp
 maven specify archytype parameter

learn maven tutorial -maven specify archytype parameter- maven example

  • Here is a screenshot MathWebApp project has been created. You may see an error message somewhere on the Project, do not worry about it, cause you have not declared Servlet library.
 maven multiple project source

learn maven tutorial - maven multiple project source - maven example

  • Eclipse create this project structure may be wrong. You need to check out.
 maven multiple project create eclipse

learn maven tutorial - maven multiple project create eclipse - maven example

  • Open file pom.xml
 maven multple project mathwebapp

learn maven tutorial - maven multple project mathwebapp - maven example

Add code

<dependency>
 <groupId>javax.servlet</groupId>
 <artifactId>javax.servlet-api</artifactId>
 <version>3.1.0</version>
 <scope>provided</scope>
</dependency>
 maven multiple pom xml

learn maven tutorial - maven multiple pom xml - maven example

  • Now error has no longer exist:
 maven project explorer

learn maven tutorial - maven project explorer - maven example

Continue configure Maven, MathWebApp use MathLibrary:

<dependency>
   <groupId>org.o7planning</groupId>
   <artifactId>MathLibrary</artifactId>
   <version>0.0.1-SNAPSHOT</version>
</dependency>
maven tutorial tags : apache maven , maven repository , maven central

MathWebApp/pom.xml

<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.o7planning</groupId>
  <artifactId>MathWebApp</artifactId>
  <packaging>war</packaging>
  <version>0.0.2-SNAPSHOT</version>
  <name>MathWebApp Maven Webapp</name>
  <url><http://maven.apache.org/></url>
  <dependencies>
      <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
      </dependency>
 
 
      <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.1.0</version>
          <scope>provided</scope>
      </dependency>
      
      <dependency>
          <groupId>org.o7planning</groupId>
          <artifactId>MathLibrary</artifactId>
          <version>0.0.1-SNAPSHOT</version>
      </dependency>        
      
  </dependencies>
  <build>
<finalName>MathWebApp</finalName>
  </build>
</project>
  • Edit file index.jsp using MathUtils, class in project MathLibrary.

index.jsp

<html>
<body>
<h2>Hello World!</h2>
 
<%
 
int a = 100;
int b = 200;
 
int c = org.o7planning.mathutils.MathUtils.sum(a,b);
 
out.println("<h2>"+ c+"</h2>");
 
%>
 
</body>
</html>

Create project MavenParent

  • Create a Common Java Project.
  • File/New/Other..
 maven java project

learn maven tutorial -maven java project - maven example

 maven multiple javaparent

learn maven tutorial - maven multiple javaparent- maven example

 maven multiple project java settings

learn maven tutorial - maven multiple project java settings - maven example

 maven configure project

maven configure project

  • Group Id: org.o7planning
  • Artifact Id: MavenParent
  • Version: 1.0.0-SNAPSHOT
  • Packaging: pom
 maven snapsot project

learn maven tutorial - maven snapsot project- maven example

 maven multiple package explorer

learn maven tutorial - maven multiple package explorer - maven example

Configure Maven the relationship between the Projects

  • This image describes briefly the Maven relational configuration among Projects.
 maven parent class

learn maven tutorial - maven parent class - maven example

  • Open file pom.xml of 2 project MathLibrary & MathWebApp and add:
<parent>
   <groupId>org.o7planning</groupId>
   <artifactId>MavenParent</artifactId>
   <version>1.0.0-SNAPSHOT</version>
   <relativePath>../MavenParent/pom.xml</relativePath>
</parent>

Open pom.xml of MavenParent add:

<modules>
  <module>../MathLibrary</module>
  <module>../MathWebApp</module>
</modules>
maven tutorial tags : apache maven , maven repository , maven central

MathLibrary/pom.xml

<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>org.o7planning</groupId>
 <artifactId>MathLibrary</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 
 
 <parent>
     <groupId>org.o7planning</groupId>
     <artifactId>MavenParent</artifactId>
     <version>1.0.0-SNAPSHOT</version>
     <relativePath>../MavenParent/pom.xml</relativePath>
 </parent>
 
</project>

MathWebApp/pom.xml

<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.o7planning</groupId>
  <artifactId>MathWebApp</artifactId>
  <packaging>war</packaging>
  <version>0.0.2-SNAPSHOT</version>
  <name>MathWebApp Maven Webapp</name>
  <url><http://maven.apache.org/></url>
  <dependencies>
      <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
      </dependency>
 
 
      <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.1.0</version>
          <scope>provided</scope>
      </dependency>
 
      <dependency>
          <groupId>org.o7planning</groupId>
          <artifactId>MathLibrary</artifactId>
          <version>0.0.1-SNAPSHOT</version>
      </dependency>
 
  </dependencies>
 
  <parent>
      <groupId>org.o7planning</groupId>
      <artifactId>MavenParent</artifactId>
      <version>1.0.0-SNAPSHOT</version>
      <relativePath>../MavenParent/pom.xml</relativePath>
  </parent>
 
  <build>
      <finalName>MathWebApp</finalName>
  </build>
</project>

MavenParent/pom.xml

<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>org.o7planning</groupId>
  <artifactId>MavenParent</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>pom</packaging>
 
 
  <modules>
      <module>../MathLibrary</module>
      <module>../MathWebApp</module>
  </modules>
 
</project>

Install projects

  • Right-click project MavenParent select:
  • Run As/Maven install.
 maven installation

learn maven tutorial - maven installation - maven example

Result:

 maven multiple project result

learn maven tutorial - maven multiple project result - maven example

 maven package explorer

learn maven tutorial - maven package explorer - maven example


Wikitechy provides an indepth knowledge on the below maven tutorial items such as maven download , maven install , maven goals , maven build , maven commands , maven plugin , maven search , maven eclipse , maven deploy , eclipse maven , maven junit , maven java , hibernate maven , maven project , java maven , maven version , maven proxy , maven 2 , maven pom , maven eclipse plugin , maven java version , maven properties , maven install windows , how to create maven project in eclipse , eclipse maven plugin , maven dependencies , what is maven in java , maven apache , maven project structure , how to install maven in eclipse

Related Searches to Build a Multiple Module Project with Maven