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.
1 Answer
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>
testonly compile time and non optional dependencies are included.