BOM
What Is Maven BOM❓
BOM stands for Bill Of Materials. A BOM is a special kind of POM that is used to control the versions of a project’s dependencies and provide a central place to define and update those versions.
BOM provides the flexibility to add a dependency to our module without worrying about the version that we should depend on.
在大型项目中,BOM 用于将一组相关的、可以良好协作的构建(Maven Artifact)组合在一起,提供版本管理,避免构件间潜在的版本不兼容风险。
BOM 配置如下:
12345678910111213
BOM 引用如下:
12345678910111213
常用的 BOM 如下:https://mvnrepository.com/tags/bom
Project
Maven URL
JUnit 5
https://mvnrepository.com/artifact/org.junit/junit-bom
Log4j 2
https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-bom
Jackson
https://mvnrepository.com/artifact/com.fasterxml.jackson/jackson-bom
OpenFeign
https://mvnrepository.com/artifact/io.github.openfeign/feign-bom
Reactor
https://mvnrepository.com/artifact/io.projectreactor/reactor-bom
Netty
https://mvnrepository.com/artifact/io.netty/netty-bom
Jetty
https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-bom
Jersey
https://mvnrepository.com/artifact/org.glassfish.jersey/jersey-bom
Micrometer
https://mvnrepository.com/artifact/io.micrometer/micrometer-bom
Spring Framework
https://mvnrepository.com/artifact/org.springframework/spring-framework-bom
Spring Session
https://mvnrepository.com/artifact/org.springframework.session/spring-session-bom
Spring Security
https://mvnrepository.com/artifact/org.springframework.security/spring-security-bom
Spring Integration
https://mvnrepository.com/artifact/org.springframework.integration/spring-integration-bom
Spring Boot
https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-dependencies
Spring Cloud
https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies
实践例子
这种方法的好处是显而易见的。依赖的细节(如版本号)可以在一个中心位置集中设置,并传播到所有继承的 POM。
但由于现实中有可能是 N 个项目都继承同一个父项目,如果把它们的依赖全部放到父项目的
解决办法是按项目组粒度对依赖进行分组管理,分而治之。将每组依赖抽取成像 spring-boot-dependencies 一样的 BOM,并在父项目中
12345678910111213141516 +------------+ | parent-pom | +------+-----+ | +-----------------|-----------------+ | | |+-------v-------+ +-------+-------+ +-------v-------+| Project-A-BOM | | Project-B-BOM | | Project-N-BOM |+---------------+ +-------+-------+ +---------------+ | +--------------|---------------+ | | | +-----v------+ +-----v------+ +------v-----+ | Artifact-A | | Artifact-B | | Artifact-N | +------------+ +------------+ +------------+
例如某个公司的标准父项目 POM 如下:
123456789101112131415161718192021222324
开发阶段为了测试 Spring Boot 2 的兼容性,该公司 child 项目新开 git 分支,修改 POM 如下:
123456789101112131415161718192021222324252627282930313233343536373839
上例 child 项目覆盖了父项目
参考https://en.wikipedia.org/wiki/Bill_of_materials
https://www.baeldung.com/spring-maven-bom
https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-build-systems.html
https://projectreactor.io/docs/core/release/reference/#getting-started-understanding-bom