Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Maven Multi-module parent-son Project in SpringBoot Project

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to realize the Maven multi-module parent-son project in the SpringBoot project". In the operation of the actual case, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

First, why use Maven multi-module

Suppose there is such a project, a very common Java Web application. In this application, we have several layers:

Dao

Service

Web

Accordingly, in a project, we will see some package names:

Org.xx.app.dao

Org.xx.app.service

Org.xx.app.web

Org.xx.app.util

But as the project progresses, you may encounter the following problems:

This application may need to have a foreground and a background manager, and you will find that most of the dao, some service, and most util are available in both applications.

The dependency list in pom.xml is getting longer and longer for reuse, but since there is only one project currently, you have to create a new project that relies on this WAR.

Build the whole project is getting longer and longer, and although you are only working at the web layer all the time, you have to build the whole project.

For a module, such as util, you only want some experienced people to maintain it, but in this case, every developer can modify it, which makes the code quality of the key module not up to your requirements.

We will find that there is actually not a design pattern principle that is "high cohesion, low coupling". Although we divide the hierarchy by package name, and you would also say that the dependencies of these packages are one-way, there is no ring dependency of the package. This is good, but not enough, because at the build level, everything is coupled together. So we need to use Maven to partition the module.

A simple Maven module structure looks like this:

-app-xxx |-- pom.xml (pom) | |-- app-util | |-- pom.xml (jar) | |-- app-dao | |-- pom.xml (jar) | |-- app-service | |-- pom.xml (jar) | |-- app-web |-- pom.xml (war) 2. Maven create multi-module aggregation project 1.1 create parent project

1. Take IDEA as an example (Eclipse is more or less the same) Select New--Project

two。 Select Maven in the pop-up interface and select JDK,-- > next

3. Enter GroupId and ArtifactId,-- > next

4. Enter the full project name and the path where the project is saved locally-> finish

5. When we are done, we get a new maven project as our parent project, and then delete the entire src from the directory to get the following directory

1.2 Spring Boot parent project

Spring boot has a default dependency when creating a single application project. When we customize the parent project, if we directly use the parent of Spring boot as the parent dependency management of the project, the child project will report an error due to the existence of two parent and cannot import the dependency.

Solutions are as follows:

Org.springframework.boot spring-boot-dependencies ${spring.boot.version} pom import

Use spring-boot-dependencies dependencies to manage springboot dependency packages in a unified manner.

For the Spring Boot project, the maven parent project pom.xml what we need to do

4.0.0 com.tan mtz-manager pom 1.0-SNAPSHOT mtz-manager mtz-manager Management system UTF-8 UTF-8 1.8 2.1.1.RELEASE org.springframework.boot spring-boot-dependencies ${spring.boot.version} pom import org.apache.maven.plugins maven-compiler-plugin ${java.version} ${java.version} ${project.build.sourceEncoding} public aliyun nexus http://maven.aliyun.com/nexus/content/groups/public/ true Public aliyun nexus http://maven.aliyun.com/nexus/content/groups/public/ true false

At this point, the parent project is created.

2.1 create a subproject

1. To create a child project, first select the parent project directory, then New-- > Model, and then directly-- > Next in the pop-up interface.

3. Next, on this page, we fill in the name of the subproject, and then-- > Next

4. Modify the following property values as appropriate, and then click-- > finish

5. After the completion of our sub-project, we can see the following directory structure.

And the pom of the parent project automatically generates Modules tags and content, thus creating a project with an aggregation relationship.

After that, we open the Maven view and click on the parent project test to see if there are any errors. If you see the following print, the child project is created successfully.

2.2 create a Spring Boot sub-project

Step 1: complete the Bizi project according to 2.1.

Part II: the sub-project we created with 2.1 will be modified as follows.

Create a new mtz-admin module. Take mtz-admin as an example. This module is a web module.

After the 1.mtz-admin is created, modify the pom.xml of the mtz-admin to add the following

Org.springframework.boot spring-boot-starter-web

two. Add application.yml and Application.java respectively

3. After that, we get a Spring Boot application, launch Application, and get the following result, which means that the creation is completed successfully.

This is the end of the content of "how to implement the Maven multi-module parent-son project in the SpringBoot project". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report