spring - 依赖spring-boot-starter-test在多模块maven项目中没有继承

标签 spring maven testing junit dependencies

在我的多模块 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>

然后从父级继承你的模块并声明没有versionscope的依赖

    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>A</artifactId>
        </dependency>
    </dependencies>

关于spring - 依赖spring-boot-starter-test在多模块maven项目中没有继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44257250/

相关文章:

javascript - JQuery/AJAX 调用没有命中 Spring MVC Controller

java - 加快Java企业Web应用程序的启动速度

maven - 导入命名空间 - 无法将名称解析为 (n) 'type definition' 组件

java - 将复杂的项目从Ant迁移到Maven-如何处理异常的文件夹结构?

java - Spring Data Mongo MapReduce javascript 没有被转义

java - 无法找到 org.springframework.format.datetime.standard.DateTimeConverters

maven - 如何创建空目录作为 distribution.xml 的一部分?

django - 我应该为每个应用程序创建一个 factories.py 吗?

testing - 我如何模拟 ApolloMutation 组件?

testing - 以编程方式执行 Gatling 测试