Configure Multi Module Maven Project with Multiple Sub Modules

Multi Module Maven Project

In this tutorial we will show you how to configure a multi module Maven project with multiple sub modules. By using Maven you can leverage all the goodies it provides like: standardisation, dependency management, common build structure, versioning and many more.

Project structure

First lets set up our project structure. We define two sub modules: one for our services, for example rest-services, and one for our front end.


+--multi-maven-project-services
|    +--src
|        +--main
|            +--java
|            +--resources
|            +--webapp
|                +--WEB-INF
|                    |--web.xml
|    |--pom.xml
+--multi-maven-project-web
|    +--src
|        +--main
|            +--java
|            +--resources
|            +--webapp
|                +--WEB-INF
|                    |--web.xml
|    |--pom.xml
pom.xml

Parent pom.xml

A multi module Maven project is defined by a parent POM referencing one or more sub modules. In the parent pom you could define dependencies that will be inherited by all of the sub modules. The parent project does not create a JAR or WAR but instead it is simply a POM that refers to other Maven projects. Next, the parent pom defines the modules. These modules inherit properties from the parent pom like properties, dependencies builds and plugins.

To configure your project encoding just add a property with key project.build.sourceEncoding and add your encoding. Look at the parent pom for an example.
<?xml version="1.0" encoding="UTF-8"?>
<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.memorynotfound.maven</groupId>
    <artifactId>multi-maven-project</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>PARENT - ${project.artifactId}</name>
    <packaging>pom</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <modules>
        <module>multi-maven-project-services</module>
        <module>multi-maven-project-web</module>
    </modules>

</project>

Sub module – services

This is a sub module of our multi module Maven project. As such it inherits all the properties, dependencies, plugins and build configurations specified in the parent pom, including dependencies inside this sub module will tell Maven to include this dependencies only in this sub module. We use packaging war so we are obligated to add webapp/WEB-INF/web.xml into our project. Otherwise Maven will complain that this is missing.

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <parent>
        <groupId>com.memorynotfound.maven</groupId>
        <artifactId>multi-maven-project</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>multi-maven-project-services</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>SERVICE - ${project.artifactId}</name>
    <packaging>war</packaging>



</project>

Sub module – web

This is the second sub module and the explanation is the same as the other sub module.

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <parent>
        <groupId>com.memorynotfound.maven</groupId>
        <artifactId>multi-maven-project</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>multi-maven-project-web</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>UI - ${project.artifactId}</name>
    <packaging>war</packaging>



</project>

Building our project

Now we have our project structure setup, it is time to test if Maven will compile. We can package our project using mvn clean package.

mvn clean package

If you have your project setup right you will see the following output by Maven.

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] PARENT - multi-maven-project
[INFO] SERVICE - multi-maven-project-services
[INFO] UI - multi-maven-project-web
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building PARENT - multi-maven-project 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[....]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building SERVICE - multi-maven-project-services 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[....]                                                                      
[INFO] ------------------------------------------------------------------------
[INFO] Building UI - multi-maven-project-web 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[....] 
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] PARENT - multi-maven-project ....................... SUCCESS [  0.002 s]
[INFO] SERVICE - multi-maven-project-services ............. SUCCESS [  0.784 s]
[INFO] UI - multi-maven-project-web ....................... SUCCESS [  0.021 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.891 s
[INFO] Finished at: 2014-12-28T13:43:35+01:00
[INFO] Final Memory: 11M/245M
[INFO] ------------------------------------------------------------------------

That’s it. Our multi module Maven project structure is created and we can start programming!

References

Download

Download it ! – multi-module-maven-project

You may also like...