The following example is based on the order service I’m using. The principle is the same no matter what project you’re using. Don’t get stuck on what service you’re using.

1. Modify the outermost POM file


The outermost POM file is used to manage the parent POM of other sub-modules, and the < Packaging > node is changed to POM. Before modification, < Packaging > may be jar or WAR

This is changed to POM because the parent POM relies on the child POM for packaging, and the parent POM introduces and manages all the child POM modules through the < Modules > node

<packaging>pom</packaging>Copy the code

2. Create multiple modules


Assuming that you have worked out the rules for splitting submodules, start creating submodules based on your own design

For example, I split the Order project into three separate sub-modules, common, Client and Server. Each module is responsible for different things

Step 1: Right-click the project name and select New->Module

Step 2: Select Maven in the New Module window that pops up

Step 3: Click Next, enter the ArtifactId for the submodule, such as Common, Client, Server, etc., and click Next

Step 4: Check and verify that the submodule name and path are correct. If so, click Finish

At this point, the common module has been created, and you’ll find a common submodule in the order root directory

3. Migrate code


After creating the common module, if you need to move code under the common submodule, you can create the same directory structure under common-src-main-java based on the original code directory structure

Such as: ProductInfoOutput (com.imooc.order.mon) : com.imooc.order.mon So I need to create com.imooc.order.mon under common-src-main-java and drag ProductInfoOutput into it

Supplementary notes: Com.imooc.order.mon () : com.imooc.order.mon () : com.imooc.order.mon () : com.imooc.order.mon () : com.imooc.order.mon () : com.imooc.order.mon () : com.imooc.order.mon () : com.imooc.order.mon () : com.imooc.order.mon () That is, IDEA simply created a folder for me called com.imooc.order.mon

After study, it is found that the problem is the view. Change the Project in the upper left corner into Project Files, and then create it in the way above

When transferring the code in the test directory, you also need to ensure that the package structure in the test directory is the same as before transferring the code. This section uses the Server module as an example, because the test directory in the common directory has no code

After we have created several child modules, we look at the contents of the outermost parent POM file and see that the parent POM has already introduced the child module we created as modules

4. Modify the POM file of the submodule


When we create multiple submodules, there may be dependencies between submodules, for example, my Server module will rely on the ProductInfoOutput class in the Common module

Then I need to introduce the common module in the SERVER module’s POM file

The same applies to dependency references between other modules

After modifying a child POM dependency, refresh the POM file; otherwise, the dependency will not take effect

5. Configure the plug-in package


SpringBoot projects are usually deployed as JAR packages, so you need to introduce the Maven plug-in of spring-boot-Maven-plugin in the POM

For example, if I want to package the Order project and my SpringBoot main class runs under the Server module, THEN I need the Server module as the main package object

Because the Maven plugin will look for SpringBoot’s boot class when packaging, packaging will fail if there is no boot class

I need to add the spring-boot-Maven-plugin to the POM of the server module

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

Note: If spring-boot-Maven-plugin exists in the outermost parent POM, please delete it

6. Package and run the service


Clean and install for Lifecycle in a single run in the Maven window (if you know the maven command, you can also use the command)

As you can see, these modules are all SUCCESS, indicating a successful build

The jar file we want to run is in the target directory of the server module, and we find it

Finally, run the JAR package on the server

Run the nohuo java-jar order-server-0.0.1- snapshot. jar > order.log 2>&1 & command

As you can see, the project has successfully started with the default port number 8080