Creating a Multi Module Spring Boot Project

Posted Erato Rabbit

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Creating a Multi Module Spring Boot Project相关的知识,希望对你有一定的参考价值。

1 Document

https://docs.spring.io/spring-boot/docs/2.5.14/maven-plugin/reference/htmlsingle/#using.parent-pom

1.1 Inheriting the Starter Parent POM

Then, If you import additional starters, you can safely omit the version number.

1.2 Using Spring Boot without the Parent POM

If you do not want to use the spring-boot-starter-parent, you can still keep the benefit of the dependency management (but not the plugin management) by using an import scoped dependency, as follows:

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

If you want to override individual dependencies, you need to add entries in the dependencyManagement section of your project before the spring-boot-dependencies entry. For instance, to use a different version of the SLF4J library and the Spring Data release train,

<dependencyManagement>
    <dependencies>
        <!-- Override SLF4J provided by Spring Boot -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.30</version>
        </dependency>
        <!-- Override Spring Data release train provided by Spring Boot -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-releasetrain</artifactId>
            <version>2020.0.0-SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.5.14</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

/ADD: doesn’t import actual things. You should Import the core dependency yourself./

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

👿

/link to <maven-note.md>, When you want to have multiple fathers, you use -:import/

/After all, the core of maven ‘inheritance’ is just pom inheritance/

<dependencyManagement>
    <dependencies>
        <dependency>
            <>
            <>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

2 Tutorials

2.1 Creating a Multi Module Project

Learn how to build a library and package it for consumption in a Spring Boot Application

This guide shows you how to create a multi-module project with Spring Boot. The project will have a library jar and a main application that uses the library. You could also use it to see how to build a library (that is, a jar file that is not an application) on its own.

2.1.1 Create the Library Project

One of the two projects serves as a library that the other project (the application) will use.

Now you need to configure a build tool (Maven or Gradle). In both cases, note that the Spring Boot plugin is not used in the library project at all.

Well, still, you do want to take advantage of Spring Boot dependency management, so that is configured by using the spring-boot-starter-parent from Spring Boot as a parent project. An alternative would be to import the dependency management as a Bill of Materials (BOM) in the <dependencyManagement/> section of the pom.xml file.

Adjusting the Library Project

The Library project has no class with a main method (because it is not an application). Consequently, you have to tell the build system to not try to build an executable jar for the Library project. (By default, the Spring Initializr builds executable projects.)

To tell Maven to not build an executable jar for the Library project, you must remove the following block from the pom.xml created by the Spring Initializr:

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

2.1.2 Create a Service Component

The library will provide a MyService class that can be used by applications.

We do not recommend putting application.properties in a library, because there might be a clash at runtime with the application that uses the library (only one application.properties is ever loaded from the classpath). You could put application.properties in the test classpath but not include it in the jar (for instance, by placing it in src/test/resources).

2.1.3 Create the Application Project

The Application project uses the Library project, which offers a service that other projects can use.

2.1.4 Setting up the Application Project

For the Application project, you need the Spring Web and Spring Boot Actuator dependencies.

2.2 Testing the Web Layer

This guide walks you through the process of creating a Spring application and then testing it with JUnit.


3 Practice Tips

When scene gets complicated, not just one single project, but super and sub projects…

Super projet is

<project>
  <packaging>pom</packaging>
  
  <modules>
    <module>rights-portal</module>
  </modules>
</project>

sub project is jar

<project>
  <packaging>jar</packaging>
</project>

maven——multi-module

1. is defined by a parent POM referencing one or more submodules

2. The appropriate packaging for a project likesimple-parent that simply provides a Project Object Model is pom.

3. Maven knows to look in these directories for pom.xml files,maven知道如何去找子模块的pom.xml

4. Using POM inheritance allows you to add common dependencies for universal dependencies

那为什么要多模块呢? 要明白多个模块仍然在一个项目中,启动这个项目后,多个模块之间就可以相互调用。多个模块方面分工开发和功能聚合。

以上是关于Creating a Multi Module Spring Boot Project的主要内容,如果未能解决你的问题,请参考以下文章

2017-09-26 发布 SpringBoot多模块项目实践(Multi-Module)

maven——multi-module

Spring Boot Multi-Module maven 项目重新打包失败

MindManager后打开老是弹出DynaZip UnZIP Error: creating output file (Problem extracying file(s))

SonarQube Jacoco离线仪器在Maven Multi Module Project中显示0%的覆盖率

自定义函数multi()实现多个数求积