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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值