现象:当一个应用有多个模块时,导致项目无法启动。
原因:同一个依赖在不同的模块有不同的版本,出现冲突,导致报NoClassFound。
解决方案:通过在根模块时dependencyManagement标签来管理版本,子模块依赖相同的版本时不需要制定vesion.
dependencyManagement的原理:
只声明依赖,不实现引入。如果dependencies里面的dependency里面没有制定对应的vesion,Maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement 元素的项目,然后它就会使用这个dependencyManagement元素中指定的版本号。如果本模块已经有了version,不会用dependencyManagement 里面的vesion。这个类似java里面的继承关系。
按照在开发的场景中,可能会遇到下面的问题
1、在maven中,一个模块只能有一个parent,某个模块可能需要有多parent怎么处理呢,公司可能有自己的parent怎么办?
maven提出的解决方案:使用type标签和scope标签来解决。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.5.6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>