
一、原始结构
D:\SpringAI\SpringAI\
├── .idea\
├── .mvn\
├── src\ ← 待删除
│ └── main\java\Main.java
├── target\ ← 待删除
├── .gitignore
└── pom.xml ← 待重写
二、需要删除的东西
只删两个目录,其它都保留:
| 删除项 | 原因 |
|---|---|
src |
父工程不写代码,只管依赖 |
target |
编译产物,没用 |
.idea、.mvn、.gitignore、pom.xml保留。
三、改造后的最终结构
D:\SpringAI\SpringAI\
├── .idea\
├── .mvn\
├── .gitignore
├── pom.xml ← 父工程(packaging=pom)
├── demo1\ ← 子模块 1(以后新建)
│ ├── src\main\java\...
│ └── pom.xml
├── demo2\ ← 子模块 2
│ └── ...
└── ...
四、完整的父 pom.xml(直接复制即可)
文件位置:SpringAI/pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SpringAI</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<!-- 以后每新建一个子模块,就在这里加一行 -->
<!-- <module>demo1</module> -->
</modules>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-boot.version>3.5.4</spring-boot.version>
<spring-ai.version>1.1.4</spring-ai.version>
</properties>
<!--
============================================================
【dependencyManagement】只声明版本,不真的引入。
▸ 子模块要用,仍要写 <dependency>,但不用写 <version>,自动继承此处版本。
▸ 适合放 BOM、第三方库版本号,统一版本管理。
▸ 子模块不写就不引入,灵活。
============================================================
-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!--
============================================================
【dependencies】真的引入依赖,所有子模块强制继承(无法移除)。
▸ 适合放每个子模块都必用的基础库(如 web、test、lombok)。
▸ 子模块就算用不上也会被强制带上,慎放可选依赖。
▸ 与 dependencyManagement 区别:前者"强制带货",后者"按需点单"。
============================================================
如需 webflux,注释下面的 web,并把 webflux 取消注释(二者互斥)
============================================================
-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- MyBatis 整合 Spring Boot -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.4</version>
</dependency>
<!-- MySQL 8 驱动 -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Lombok(编译期生成 getter/setter/构造器等,运行期不参与) -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots><enabled>false</enabled></snapshots>
</repository>
</repositories>
</project>
五、新建子模块的步骤
-
IDEA 中右键父工程
SpringAI→ New → Module -
选 Spring Boot (或 Maven Archetype),Name 填
demo1 -
Location 自动是
D:\SpringAI\SpringAI\demo1,不要改 -
创建完后,回到父 SpringAI/pom.xml,在
<modules>中加一行:<module>demo1</module> -
子模块的 pom.xml 顶部要有:
<parent> <groupId>org.example</groupId> <artifactId>SpringAI</artifactId> <version>1.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> -
子模块加依赖时只写 groupId + artifactId,不写 version------版本由父工程统一管理。
七、以后添加依赖
在父工程添加依赖依赖统一版本,子模块引入即可