Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dependency spring-boot-starter-test not inherited in multiple module maven project

In my multi-module maven project, all modules have a dependency of my own "common" module, eg, module "A" have this dependency "common". And the "common" module has a spring-boot-starter-test dependency. However, when I write unit test in this "A" module, it shows that the test dependency not imported. And then I check the dependencies and found that the spring-boot-starter-test is not imported. I want to ask why? In my sense, module "A" ref "common", "common" ref spring-boot-starter-test , so the module "A" should ref spring-boot-starter-test, but the fact is not that. By the way, in spite of this spring-boot-starter-test other dependencies works well via the hirachy. Does anyone know why it is? Thank you in advance.

like image 980
Yun Avatar asked Oct 29 '25 17:10

Yun


1 Answers

Most likely in the module "A" dependecy spring-boot-starter-test has scope test. Dependecies with such scope is not transitive. See Dependency Scope section https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html. The best solution is dependency management. See Dependency Management

Briefly, you need to create parrent module and declare dependency managment sectoin:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>A</artifactId>
            <version>1.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Then inherit your modules from parent and just declare dependency without version and scope

    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>A</artifactId>
        </dependency>
    </dependencies>
like image 99
vbychkovskyi Avatar answered Oct 31 '25 11:10

vbychkovskyi