IDEA 中搭建 Spring Boot Maven 多模块项目 (父SpringBoot+子Maven)

发布时间:2024年01月08日

第1步:新建一个SpringBoot 项目 作为 父工程

[Ref] 新建一个SpringBoot项目

删除无用的 .mvn 目录、 src 目录、 mvnwmvnw.cmd 文件,最终只留 .gitignorepom.xml
在这里插入图片描述
在这里插入图片描述

第2步:创建 子maven模块

在这里插入图片描述
在这里插入图片描述

第3步:整理 父 pom 文件

① 删除 dependencies 标签及其中的 spring-boot-starterspring-boot-starter-test 依赖,因为 Spring Boot 提供的父工程已包含,并且父 pom 原则上都是通过 dependencyManagement 标签管理依赖包。
② 删除 build 标签及其中的所有内容,spring-boot-maven-plugin 插件作用是打一个可运行的包,多模块项目仅仅需要在 入口类所在的模块 添加打包插件,这里父模块不需要打包运行。而且该插件已被包含在 Spring Boot 提供的父工程中,这里删掉即可。
③ 最后整理父 pom 文件中的其余内容,按其代表含义归类,整理结果如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<!-- 基本信息 -->
	<modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>
	<name>ParentSpringBoot</name>
	<description>ParentSpringBoot</description>

	<!-- 项目说明:这里作为聚合工程的父工程 -->
	<groupId>com.example</groupId>
	<artifactId>ParentSpringBoot</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<!-- 继承说明:这里继承Spring Boot提供的父工程 -->
    <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.2.1</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<!-- 模块说明:这里声明多个子模块 -->
	<modules>
		<module>module1</module>
	</modules>

	<!-- 属性说明 -->
	<properties>
		<java.version>17</java.version>
	</properties>
</project>

第4步:添加入口类

选择某个module添加入口类
在这里插入图片描述

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

第5步:配置模块间的依赖关系

在这里插入图片描述

<properties>
    <java.version>17</java.version>
    <module1.version>0.0.1-SNAPSHOT</module1.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>module1</artifactId>
            <version>${module1.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

第6步:启动SonApplication

在这里插入图片描述

参考

IDEA 中搭建 Spring Boot Maven 多模块项目

文章来源:https://blog.csdn.net/weixin_37646636/article/details/135438118
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。