本规范定义了项目中依赖管理的原则和实践,确保依赖项的选择、版本控制和更新符合项目需求,并维护代码的稳定性和安全性。
pom.xml 的 <properties> 标签中定义依赖版本,避免在各依赖声明中硬编码版本号。示例:
<properties>
<spring-boot.version>3.0.13</spring-boot.version>
</properties>
<dependencyManagement> 引入 spring-boot-dependencies BOM 来管理 Spring 生态相关依赖的版本。示例:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
spring-boot-starter:基础启动器spring-boot-starter-web:Web 开发spring-boot-starter-validation:参数校验<exclusions> 标签排除不需要的传递依赖。示例:
<dependency>
<groupId>com.example</groupId>
<artifactId>example</artifactId>
<exclusions>
<exclusion>
<groupId>org.unwanted</groupId>
<artifactId>unwanted-artifact</artifactId>
</exclusion>
</exclusions>
</dependency>
mvn dependency:tree 分析依赖树,识别冲突。mvn dependency:analyze 检查未使用的依赖。<scope> 限制依赖作用域,如 provided 用于编译时依赖。