12

I want to know how i can create a multi module project with maven using Spring Boot.

Can someone show me an example of the "parent-pom" and "child-pom"?

Thanks

Paul Verest
  • 60,022
  • 51
  • 208
  • 332
juanhl
  • 1,170
  • 2
  • 11
  • 16
  • cdheck this http://maven.apache.org/plugins/maven-eclipse-plugin/reactor.html it gives idea about multimodule maven project – jos Jul 05 '14 at 05:36
  • Maven is managing your application structure as java projects (a common practice could be divide your application in several layers, implemented throw a jar/war/ear for instance) while Spring (or spring boot in your case) is just a framework to implement your application logic. Using Spring Boot should not impact the way you'll divide your project into modules. I recommend you to first design your application architecture (layers, error management, ...), next decide how to divide your app into java proyects (is a layer replaceable o not, ...) and finally choose a way to implement each module – Cheloute Oct 15 '15 at 16:28

3 Answers3

11

This question is too simple to answer or too complex to be understood, otherwise I cannot explain why there are no answers.

I would go for a solution that uses a folder structure like the following

project folder
|---pom.xml
|---module1
    |---pom.xml
    |---src
|---module2
    |---pom.xml
    |---src
|---module3
    |---pom.xml
    |---src

I would define the pom.xml of the project with a pom packaging, e.g.:

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

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.7-RELEASE</version>
        <relativePath/>
    </parent>

    <groupId>com.myproject</groupId>
    <artifactId>project</artifactId>
    <packaging>pom</packaging>
    <version>0.0.1-SNAPSHOT</version>

    <modules>
        <module>module1</module>
        <module>module2</module>
        <module>module3</module>
    </modules>

    <name>MySupercoolProject</name>
    <description>What else?</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
      <!-- Put here dependencies shared by your project -->
    </dependencies>
</project>

As you see, the parent of your project will be a spring-boot-starter-parent.

Then, the pom.xml of one of your modules will be, for instance:

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

    <parent>
        <groupId>com.myproject</groupId>
        <artifactId>project</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <groupId>com.myproject</groupId>
    <artifactId>module1</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>

    <name>Application</name>
    <description>SudenGut application</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <start-class>com.myproject.MyApplication</start-class>
        <java.version>1.8</java.version>
        <tomcat.version>8.0.24</tomcat.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--- this will not be enough to provide a cool app :) -->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins
    </build>
</project>

where the main application is MyApplication.java in package com.myproject of module1.

I would use the jar packaging for the other modules without a main application, unless you have other submodules (i.e., pom packaging again).

JeanValjean
  • 17,172
  • 23
  • 113
  • 157
  • Is it possible to have this configuration with 2 web applications? Ex If module 1 is a web application used for rest services and module 2 is another web application used as an admin portal. They both have a dependency on a common module 3. I know it would be possible to run them as 2 separate applications on 2 separate tomcat instances, but it is possible to have them run on the same instance? – mad_fox Feb 15 '16 at 22:18
  • @mad_fox Sure you can. You can have a common parent, with pom packaging, common shared jar modules (e.g. the model that embeds the db-related part) and then two modules with two spring applications, but on different port (if you plan to run them on the same web server). I usually do something like that. At the end you deploy an application that was compiled with all the dependencies, not your whole IDE project. – JeanValjean Feb 16 '16 at 07:49
  • Sir, thank you soooo much! You saved my time and life)))) – Orkhan Hasanli Dec 07 '20 at 23:42
3

Modules as a spring boot project

Spring boot usually works with parent. But if you create multi module project and if you want to use spring boot on some of your module there is a important question, which one will be the parent? Of course your main project must be parent. So you should use spring boot as a dependency in your module.

<dependencyManagement>
     <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.5.9.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

For detailed information please review following documentation;

https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-build-systems.html#using-boot-maven-without-a-parent

0

@juanhl it is so simple. You can create Multi-module maven project with Spring Boot.

Steps : 1)Create Maven project.The Parent project's pom.xml is like following,

    <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.springmultumoduleweb</groupId>
      <artifactId>multimodule-web-app</artifactId>
      <packaging>pom</packaging>

      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
      </parent> 
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
      <modules>
        <module>multimodule-api</module>
        <module>multimodule-service</module>
        <module>EarModule</module>
      </modules>
      <version>0.0.1-SNAPSHOT</version>
    </project>

2) add maven module to the parent modules child modules may have one or more dependencies of other modules and packaging type may be jar or war.

3)create one more module to generate ear file.(packaging type : ear ) which contains dependencies of all modules as below,

<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>
  <parent>
    <groupId>com.springbootparent.demo</groupId>
    <artifactId>com.springbootparent.demo</artifactId>
    <version>0.0.1-SNAPSHOT</version
  </parent>
  <artifactId>com.springboot.ear</artifactId>
  <packaging>ear</packaging>
  <dependencies>
    <dependency>
        <groupId>com.springbootparent.demo</groupId>
        <artifactId>com.springboot.service1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.springbootparent.demo</groupId>
        <artifactId>com.springboot.service2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.springbootparent.demo</groupId>
        <artifactId>com.springboot.war1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>war</type>
    </dependency>
    <dependency>
        <groupId>com.springbootparent.demo</groupId>
        <artifactId>com.springboot.war2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>war</type>
    </dependency>
  </dependencies>
</project> 

4) clean and install maven parent project.

5) deploy created ear/war file on server. (for ear file you have to deploy on Enterprise Application server)

-Good luck

OnkarG
  • 267
  • 1
  • 3
  • 16