What

SpringBoot projects with one or more Maven modules are called SpringBoot multi-module projects

Why

  1. Easy maintenance A large single project is divided into multiple sub-modules to facilitate maintenance between modules. Deleting or moving a single module has a relatively small impact on other modules. A single module can be reused

  2. Sharing dependencies only requires defining common dependencies in the parent class. Subclass modules all share dependencies in the parent class, making it easier to keep code concise in large projects.

How

1. The parent module

Create parent project (SpringBoot)

Delete unnecessary directories

Remove src. MVN MVNW mvnw.cmd

pom

  1. The packaging label is changed to “POM” to indicate that this module is an aggregation module. The subclass modules are “aggregated” and packaged using Maven
<packaging>pom</packaging>
Copy the code
  1. Dependencies and dependencyManagement tags

Submodules automatically introduce dependencies of their parent classes

A child module does not automatically import a dependency from the parent class. The version tag is not required in GAV. Version is the same as that in the parent class

Parent poM file demo

<! - the parent class: > <parent> <groupId>org.springframework.boot</groupId> The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.2.4. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <! --> <groupId>com.ybj</groupId> <artifactId>my-app</artifactId> <version>0.0.1-SNAPSHOT</version> <name>my-app</name> <description> Springboot multi-project </description> <! Pom --> <packaging> POm </packaging> <! <dependencies> <! --> <dependencyManagement>Copy the code
  1. Modules tag This tag manages modules that are aggregated. Modules automatically adds a single module tag when creating a submodule

Start the class

The parent module startup class is the startup class of the entire module. All requests go through the port of the parent module. In order to enable the services of the child module to be started, annotations need to be added to the parent module startup class

@ComponentScan(basePackages = {"com.ybj.*",})
Copy the code

2. The module

1. Create a submodule

Select the Module

Select the Maven project

2.pom

  1. Packaging label You can select jar or WAR to specify packaging
  2. Parentpom < parent > parentPoM < GAV > parentPoM < GAV > parentPoM < GAV
<! - the parent class: --> <parent> <artifactId>my-app</artifactId> <groupId>com.ybj</groupId> <version>0.0.1-SNAPSHOT</version> </parent>Copy the code
  1. Inter-module dependencies need to depend on a module, and you can declare in the POM that the dependencies here are downloaded from a local repository, not a remote repository

  2. Install the Maven Helper plugin in IDEA. Click on the POM file and select Dependency Analyzer

You can view and search for dependencies, which is helpful for resolving dependency conflicts

4. Packaging

Method 1: Package the aggregation module directly

In aggregation module packaging, the dependency order is automatically calculated and packaged in turn

Approach 2: Use plug-ins

The problem with approach 1 is that it takes a long time to package all modules, so you need to implement a single module package with the help of the spring-boot-Maven-plugin

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
			<version>2.3.0. RELEASE</version>
			<executions>
				<execution>
					<goals>
						<goal>repackage</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>
Copy the code

5. Faqs

Rely on the conflict

Because subclasses inherit dependencies from their parent class, they can cause dependency conflicts that prevent the project from starting. You can use the Maven Helper plugin to check for conflicts