在我的多模块 Maven 项目中,所有模块都依赖于我自己的“公共(public)”模块,例如,模块“A”具有这种“公共(public)”依赖性。并且“通用”模块具有 spring-boot-starter-test
依赖项。但是,当我在这个“A”模块中编写单元测试时,它显示未导入测试依赖项。然后我检查依赖项,发现 spring-boot-starter-test
没有导入。我想问为什么?在我看来,模块“A”引用“common”,“common”引用 spring-boot-starter-test
,所以模块“A”应该引用 spring-boot-starter-test
,但事实并非如此。顺便说一下,尽管有这个 spring-boot-starter-test
其他依赖项通过 hirachy 运行良好。有谁知道这是为什么?提前谢谢你。
最佳答案
很可能在模块“A”中依赖 spring-boot-starter-test
具有范围 test
。具有这种范围的依赖关系是不可传递的。参见依赖范围部分https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html .
最好的解决方案是依赖管理。请参阅依赖性管理
简而言之,您需要创建父模块并声明依赖管理部分:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>A</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
然后从父级继承你的模块并声明没有version
和scope
的依赖
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>A</artifactId>
</dependency>
</dependencies>
关于spring - 依赖spring-boot-starter-test在多模块maven项目中没有继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44257250/