1. 多模块项目
在实际开发过程中,会使用多模块的项目搭建,因此就会存在一个顶级项目,以及模块项目。
通常顶级项目也就是父类项目,是会删除其src文件夹,保留pom文件,通常作为各个模块的依赖的jar的管理。
2. 继承父级依赖dependencies
如果在父级项目中的pom文件中
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>xxx</version>
</dependency>
</dependencies>
其他的模块都是会自动导入该模块。
3. 管理依赖版本dependencyManagement
在顶级父类pom文件中
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>xxx</version>
</dependency>
</dependencies>
</dependencyManagement>
这里的依赖仅仅是被声明了,也就是定义了一个依赖配置类,如果子模块需要使用到该依赖的时候,只需要编写groupid和artifactid即可,无需定义version,因为父类中定义好了。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
4. 其他
如果使用idea创建一个单独无模块的项目的时候,会自动在pom文件指定父级项目。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
因此在生成的pom文件中,有些依赖是没有加version的,因为是可以在其父类中的dependencyManagement找到。