Maven multi module example

This example shows a multi-module maven working example. To show a working application, I have created a parent project containing 2 child (module) projects. One of the child project is a spring mvc rest based webapp and the other is a java project containing the service & model.

This series:
Maven multi module example
Gradle multi project (module) example – Type I (nested)
Gradle multi project (module) example – Type II (flat)

This also serves as a Hello World example of Spring MVC REST JSON Services using ResponseBody.

On intellij project view, the project structure looks as shown below:

On explorer, the directory structure looks as shown below:

 

 

 

Parent Project

The parent project is an empty project containing just the 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>com.idodevjobs.sample-multi-module-project</groupId>
    <artifactId>sample-multi-module-parent</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>pom</packaging>
    <name>sample-multi-module-parent</name>

    <properties>
        <junit.version>4.11</junit.version>
        <spring.version>4.0.0.RELEASE</spring.version>
        <jackson.version>2.3.2</jackson.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>${jackson.version}</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <modules>
        <module>../sample-multi-module-service</module>
        <module>../sample-multi-module-web</module>
    </modules>

</project>

The packaging is defined as ‘pom’ to tell maven not to package it and specify its a container of sub-modules. The pom also defines the dependencies to be used by the sub-modules under ‘dependency-management’ so that all the sub-module uses the same version of dependencies. The pom also defines the group-id, artifact-id and version which child projects’ pom will list as parent.

Child Project I – Service

The child project is a simple java project created using ‘maven-archetype-quickstart’ containing all the java classes.


<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>

    <artifactId>sample-multi-module-service</artifactId>
    <packaging>jar</packaging>

    <parent>
        <groupId>com.idodevjobs.sample-multi-module-project</groupId>
        <artifactId>sample-multi-module-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../sample-multi-module-parent</relativePath>
    </parent>

    <name>sample-multi-module-service</name>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
    </dependencies>

</project>

The pom has the parent pom’s details in parent section. The child pom will inherit the parent project’s group and version and hence we just only define the name. The packaging is defined as ‘jar’. The dependencies don’t have to contain version as its derived from parent pom. This pom also contains the dependency for spring-context.

SampleResponse.java


package com.idodevjobs.sample.model;

public class SampleResponse {
    private String message;
    private String status;

    public SampleResponse() {}

    public SampleResponse(String message, String status) {
        this.message = message;
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}

SampleService.java


package com.idodevjobs.sample.service;

import com.idodevjobs.sample.model.SampleResponse;
import org.springframework.stereotype.Service;

@Service
public class SampleService {
    public SampleResponse sayHello(String message) {
        return new SampleResponse("hello " + message, "SUCCESS");
    }
}

Child Project II – Webapp

The other child project is a web project created using ‘maven-archetype-webapp’ which contains the controller classes and can potentially contain all web resources as well.


<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>

    <artifactId>sample-multi-module-web</artifactId>
    <packaging>war</packaging>

    <name>sample-multi-module-web Maven Webapp</name>

    <parent>
        <groupId>com.idodevjobs.sample-multi-module-project</groupId>
        <artifactId>sample-multi-module-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../sample-multi-module-parent</relativePath>
    </parent>

    <dependencies>
        <dependency>
            <groupId>com.idodevjobs.sample-multi-module-project</groupId>
            <artifactId>sample-multi-module-service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
    </dependencies>

    <build>
        <finalName>sample-multi-module-web</finalName>
    </build>
</project>

This pom contains the service project as a dependency. This also contains the dependency for spring-webmvc and jackson-databind.

SampleController.java


package com.idodevjobs.sample.controller;

import com.idodevjobs.sample.model.SampleResponse;
import com.idodevjobs.sample.service.SampleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SampleController {

    @Autowired
    private SampleService sampleService;

    @RequestMapping(value = "/hello/{message}", method = RequestMethod.GET)
    public @ResponseBody SampleResponse sayHello(@PathVariable String message) {
        return sampleService.sayHello(message);
    }
}


spring-servlet.xml


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <context:component-scan base-package="com.idodevjobs.sample" />

    <mvc:annotation-driven />

</beans>

web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

Advantages

The advantages of using multi module maven project are a lot. Some are listed below:

  • Using dependency-management, the parent project can control all the sub-modules use the same version of a particular dependency.
  • By listing modules and building the parent pom, all the sub-modules will be built in the order they are mentioned and there is no need to build individual modules.
  • As all the child projects inherit parent project’s group-id and version, all the modules’ group-id and version will be consistent and can be maintained in a single place.

Testing

Do a maven clean install on parent project which will in turn build all the modules. The war file in the sample-multi-module-web target folder has to be copied to tomcat -> webapps folder and bin/startup has to be executed.

To test the rest services, use a rest client like POSTMAN (chrome plugin). As we only have a get service, this can be tested on browser as below.

Advertisements
This entry was posted in java and tagged , , , , . Bookmark the permalink.

21 Responses to Maven multi module example

  1. Anonymous says:

    hi thanks for the sample so helpful

    Like

  2. Anonymous says:

    can i know from where i can download working sample

    Like

  3. sam says:

    can i have a downloadable copy
    thanks in advance

    Like

  4. Emeric says:

    Hi. Thank you for your example.
    However, is there a way to include multiple REST modules (e.g. admin-rest-module, business-rest-module) and use a specific base URL for each one of them?

    Like

  5. Manjush says:

    Thanks for a wonderful example , Can you please tell me the disadvantages of this approach.

    What are the disadvantages if we dont go with MultiModule.

    If we need to add DAO layer then that has to added as third child project.

    Any examples based on including DAO layer too

    Like

  6. Anil Pandey says:

    Hi, I am doing exactly the same but getting below error after “mvn install”

    [ERROR] Failed to execute goal on project sample-multi-module-web: Could not res
    olve dependencies for project com.idodevjobs.sample-multi-module-project:sample-
    multi-module-web:war:2.0-SNAPSHOT: Could not find artifact com.idodevjobs.sample
    -multi-module-project:sample-multi-module-service:jar:1.0-SNAPSHOT -> [Help 1]

    Like

  7. Anil Pandey says:

    First of all, thanks for your support and reply. Please use below google drive url to check the complete log trace.
    https://drive.google.com/file/d/0B4x_B9CbW2LCTEdvS1ZrUkQtcjA/view?usp=sharing

    Like

  8. Georman calderon says:

    Your example show your configuration accurately. However what happens if you include an hibernate configuration. It must be in the last proyect (web) or can be in service but how web proyect can notice about that.

    Like

  9. Pingback: Setup a Multiple Module Project using Maven | Sohaib Nasir

  10. ganesh says:

    Hai ,first of all thanks a lot for this …My question is how do i include 2 modules of war package type ??

    Like

  11. Pingback: Gradle multi project (module) example – Type II (flat) | iDoDevJobs

  12. Pingback: Gradle multi project (module) example – Type I (nested) | iDoDevJobs

  13. pf_miles says:

    Great! That’s what I need.
    Thanks!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s