Maven子模块如何复用父模块的test scope依赖?
自定义Spring Boot Starter中Test依赖传递的解决方案
问题描述
我正在自定义Spring Boot Starter,其POM配置如下:
<dependencyManagement> <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${springBoot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- Load basic framework. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </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>compile</scope> --> <scope>test</scope> </dependency> <dependency> <groupId>io.projectreactor</groupId> <artifactId>reactor-test</artifactId> <!-- <scope>compile</scope> --> <scope>test</scope> </dependency> <!-- load database framework. --> <!-- Slf4j Logger --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <scope>compile</scope> </dependency> </dependencies>
但子模块引入该Starter依赖后,无法使用其中scope=test的依赖:
<dependency> <groupId>com.chhtest.clc</groupId> <artifactId>boot-starter</artifactId> </dependency>
我不想将test依赖改为compile(会增大包体积),也不想强制子模块用该Starter作为父模块,请问还有其他解决方法?
方案1:拆分出独立的Test Starter(推荐)
参考Spring官方的设计模式,将生产依赖与测试依赖拆分到两个独立模块中:
boot-starter:仅保留生产环境核心依赖,移除所有testscope的依赖boot-starter-test:专门存放测试相关依赖,保持scope=test
示例:boot-starter-test的POM配置
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${springBoot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.projectreactor</groupId> <artifactId>reactor-test</artifactId> <scope>test</scope> </dependency> <!-- 可按需添加其他测试工具依赖 --> </dependencies>
子模块使用方式
子模块按需引入两个Starter:
<!-- 生产核心依赖 --> <dependency> <groupId>com.chhtest.clc</groupId> <artifactId>boot-starter</artifactId> </dependency> <!-- 测试依赖,仅在测试阶段生效 --> <dependency> <groupId>com.chhtest.clc</groupId> <artifactId>boot-starter-test</artifactId> <scope>test</scope> </dependency>
优势:完全贴合Spring生态规范,子模块可自由选择是否引入测试依赖,不影响生产包体积。
方案2:通过Dependency Management统一管理测试依赖
在自定义boot-starter的<dependencyManagement>中声明测试依赖的版本,子模块在自身POM中显式引入这些测试依赖(无需重复指定版本)。
修改boot-starter的POM
移除<dependencies>中的测试依赖,将其移至<dependencyManagement>:
<dependencyManagement> <dependencies> <!-- Spring Boot 依赖管理 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${springBoot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- 声明测试依赖版本 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>${springBoot.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>io.projectreactor</groupId> <artifactId>reactor-test</artifactId> <version>${reactor.version}</version> <scope>test</scope> </dependency> </dependencies> </dependencyManagement> <!-- 保留原生产依赖,移除测试依赖 --> <dependencies> <!-- Load basic framework. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <!-- Slf4j Logger --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <scope>compile</scope> </dependency> </dependencies>
子模块使用方式
子模块在自身<dependencies>中引入测试依赖:
<!-- 引入核心starter --> <dependency> <groupId>com.chhtest.clc</groupId> <artifactId>boot-starter</artifactId> </dependency> <!-- 引入测试依赖,版本由starter的dependencyManagement控制 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.projectreactor</groupId> <artifactId>reactor-test</artifactId> <scope>test</scope> </dependency>
优势:无需拆分模块,子模块可灵活选择是否引入测试依赖,版本统一管控。
内容的提问来源于stack exchange,提问作者Haceral
相关产品推荐
相关产品推荐

