How to create a multiple module project using Maven

11 December 2012
By Gonçalo Marques
In this tutorial you will learn how to create a multiple module project using Maven, more precisely a web application project (WAR) referencing another project as a library (JAR).

Used software

This tutorial considers the following software and environment:

  1. Ubuntu 12.04
  2. Maven 3.0.4
  3. Eclipse Juno (for additional tutorial steps)

Creating the main project

In this tutorial we will assume that we want to create a Maven project made by two distinct modules: One will be a web application (WAR) and the other will be a library (JAR). The library project will be referenced by the web application project so it will be included by Maven in the WEB-INF/lib directory.

We start by positioning ourselves in the workspace directory and creating the main (or parent project) by issuing the following Maven command:

mvn archetype:create -DgroupId=com.byteslounge.main -DartifactId=com-byteslounge-main

After this step you will end up with a directory called com-byteslounge-main that will contain your main project.

The directory containing your main project
Project folder structure

Now you may delete the src folder since it will not be used. After this edit your pom.xml file and change jar to pom.

Note that you just changed the packaging type of the main project from jar to pom. After this your pom.xml file should look like the following:

Main project pom.xml file packaging edited to pom
<?xml version="1.0"?>
<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.byteslounge.main</groupId>
  <artifactId>com-byteslounge-main</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>com-byteslounge-main</name>
  <url>http://maven.apache.org</url>

  <!-- ommiting other sections for clarity -->
  
</project>

Creating the project modules

After creating the main project we will create the library module (or project). Place yourself in the main project directory and issue the following Maven command:

mvn archetype:create -DgroupId=com.byteslounge.lib -DartifactId=com-byteslounge-lib

The library project will be created in the directory com-byteslounge-lib.

Now we create the WAR module by issuing the following command:

mvn archetype:create -DgroupId=com.byteslounge.war -DartifactId=com-byteslounge-war

The WAR module will be created in the directory com-byteslounge-war. At this moment you have two directory projects inside the main project directory each one containing its own pom.xml file:

Your final multiple module Maven project directory structure
Multiple module project folder structure

Now we must edit the pom.xml file of the WAR project and do a couple of things: The first one is to define the module packaging type as war, ie adding war to the project definition. The second one is defining the library project as a dependency of the war project. The final pom.xml file of the WAR project should look like the following after editing:

War module pom.xml file after editing
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
  http://maven.apache.org/xsd/maven-4.0.0.xsd"
  xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.byteslounge.main</groupId>
    <artifactId>com-byteslounge-main</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <groupId>com.byteslounge.war</groupId>
  <artifactId>com-byteslounge-war</artifactId>

  <!-- packaging defined as war -->
  <packaging>war</packaging>

  <version>1.0-SNAPSHOT</version>
  <name>com-byteslounge-war</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>

    <!-- dependency to lib project -->
    <dependency>
      <groupId>com.byteslounge.lib</groupId>
      <artifactId>com-byteslounge-lib</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>

    <!-- other dependencies omitted for clarity -->

  </dependencies>
</project>

Now you must create a web.xml file. For the clarity of this example we will create an empty one. Create a file called web.xml in the war module:

com-byteslounge-war/src/main/webapp/WEB-INF/web.xml

As a final step we must move to the main project directory and issue the build command:

mvn clean install

At this point you have a multiple module Maven project. You can start defining classes in your library project and referencing them from your war project classes. You may add as many modules you want to your main Maven project using this approach.

Importing the multiple Maven project in Eclipse

To import the project you just created in Eclipse you just have to place yourself in the main project directory and issue the following command:

mvn eclipse:eclipse

After this step you should open Eclipse and set M2_REPO classpath variable: Window -> Preferences -> Java -> Classpath variables. Create a new variable named M2_REPO and point it to your local Maven repository folder under .m2, ie /home/user/.m2/repository/

Finally you just need to Import the project into the workspace by using the Import Existing Projects into Workspace feature from Eclipse, selecting your Maven main project directory as the Import root directory and finally select both projects from the Import window.

Multiple module Maven project in Eclipse
Multiple module Maven project in Eclipse

The source code from this example is available for download right at the bottom of the tutorial.

Download source code from this article

Related Articles

Comments

About the author
Gonçalo Marques is a Software Engineer with several years of experience in software development and architecture definition. During this period his main focus was delivering software solutions in banking, telecommunications and governmental areas. He created the Bytes Lounge website with one ultimate goal: share his knowledge with the software development community. His main area of expertise is Java and open source.

GitHub profile: https://github.com/gonmarques

He is also the author of the WiFi File Browser Android application: