第1步:新建一個SpringBoot 項目 作為 父工程
[Ref] 新建一個SpringBoot項目
刪除無用的 .mvn
目錄、 src
目錄、 mvnw
及 mvnw.cmd
文件,最終只留 .gitignore
和 pom.xml
第2步:創(chuàng)建 子maven模塊
第3步:整理 父 pom 文件
① 刪除 dependencies
標(biāo)簽及其中的 spring-boot-starter
和 spring-boot-starter-test
依賴,因為 Spring Boot 提供的父工程已包含,并且父 pom 原則上都是通過 dependencyManagement
標(biāo)簽管理依賴包。
② 刪除 build
標(biāo)簽及其中的所有內(nèi)容,spring-boot-maven-plugin
插件作用是打一個可運行的包,多模塊項目僅僅需要在 入口類所在的模塊 添加打包插件,這里父模塊不需要打包運行。而且該插件已被包含在 Spring Boot 提供的父工程中,這里刪掉即可。
③ 最后整理父 pom 文件中的其余內(nèi)容,按其代表含義歸類,整理結(jié)果如下:
<?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步:配置模塊間的依賴關(guān)系
<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
文章來源:http://www.zghlxwxcb.cn/news/detail-806720.html
參考
IDEA 中搭建 Spring Boot Maven 多模塊項目文章來源地址http://www.zghlxwxcb.cn/news/detail-806720.html
到了這里,關(guān)于IDEA 中搭建 Spring Boot Maven 多模塊項目 (父SpringBoot+子Maven)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!