在开发中,我们经常要使用 maven 进行多模块依赖管理,这样可以进行更方便的依赖管理。那么接下来就演示如何将已有的多个项目整合为一个 maven 多模块项目。
构建项目
新建父模块
新建一个 maven 项目
将父模块的 src 源码包删除掉
将已有的项目导入作为子模块
先将已有的项目(2 个项目:son-01,son-02)拷贝到父模块的目录下,截图如下:idea 并没有把拷贝进来的 2 个项目识别为 module。
选择已有的项目文件作为模块导入【File - New - Module from Existing Sources…】
选择父模块目录下先前拷贝进来的项目 son-01
按下图选择后点击 Finish
可以看到 son-01 带上了蓝色标识,说明已成为 module;其他的项目重复刚才的操作即可
配置
父模块的 pom.xml 配置
- 改打包方式为 pom(非常重要)
- 指定父模块:可选的,指定的父模块会被项目的所有子模块所继承。如 在下面的 pom.xml 中,父模块继承了
spring-boot-starter-parent
,项目的子模块都会继承该模块(子模块中也就不需要再指定继承spring-boot-starter-parent
),这样便于统一配置 Spring Boot 的版本,避免依赖冲突。 - 声明属性:子模块的 pom.xml 中可以直接使用这些属性
- 指定模块列表:指定子模块有哪些
- 统一依赖管理:声明依赖的版本,当子模块中引入依赖时没有指定版本,默认使用的就是此处声明的版本。
特别注意:统一依赖管理只是声明了依赖的版本,子模块中使用依赖时仍需配置引入。 - 定义插件配置:子模块会继承这些配置
父模块的 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>
<!--2.指定父模块-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.15</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging> <!--1.改打包方式为 pom-->
<!--3.声明属性-->
<properties>
<java.version>1.8</java.version>
</properties>
<!--4.指定模块列表-->
<modules>
<module>son-01</module>
<module>son-02</module>
</modules>
<!-- 5.统一管理子模块的依赖版本 -->
<dependencyManagement>
<dependencies>
<!--自定义的一个依赖包-->
<dependency>
<groupId>com.example</groupId>
<artifactId>my-sdk</artifactId>
<version>0.0.1</version>
</dependency>
</dependencies>
</dependencyManagement>
<!--6.定义插件配置-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
子模块的 pom.xml 配置
- 指定父模块
- maven 坐标:groupId、version 和 父模块相同,可以省略。
- 引入依赖:如果想要使用父模块中声明的依赖版本,在引入依赖时省略版本即可,当然可以重新声明将其覆盖。
子模块的 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--1.指定父模块-->
<parent>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<!--2.maven 坐标:省略了 groupId、version-->
<artifactId>son-01</artifactId>
<!--3.引入依赖-->
<dependencies>
<!--自定义的一个依赖包-->
<dependency>
<groupId>com.example</groupId>
<artifactId>my-sdk</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>