说明如何在 Maven 构建中合并多模块 SBOM
解读
在国内金融、运营商、政务云等 Spring Boot 项目交付流程中,甲方普遍要求随包提供 CycloneDX 或 SPDX 格式的 SBOM(软件物料清单),以便做开源合规审计与漏洞追溯。Maven 多模块工程(parent → child1/child2/…)默认每个模块独立生成各自的 bom.xml,导致甲方安全团队收到一堆碎片文件,无法一眼看清总依赖图谱。面试官问“如何合并”,本质考察两点:
- 是否熟悉 CycloneDX Maven 插件 的聚合(aggregate)模式;
- 是否能在 CI 阶段(如 GitLab-Runner、Jenkins、阿里云云效) 零人工干预地输出“单文件、无重复、带哈希、带许可证”的正式 SBOM。
知识点
- CycloneDX Maven 插件 2.7.x+ 的 aggregate 目标:只在 reactor 根模块执行一次,递归收集所有子模块依赖并去重,输出
target/bom.xml。 - SPDX Maven 插件 0.7.x 的 createSPDX 目标:同样支持聚合,但国内甲方以 CycloneDX 为主,SPDX 仅作补充。
- Maven 3.8.x 的 reactor 机制:聚合阶段子模块尚未打包完成,需把 SBOM 生成绑定到
verify或package之后,避免并发写冲突。 - 国内私服(Nexus、Artifactory、阿里云 ACR) 对 SBOM 的存储规范:文件名必须带
artifactId-version-classifier-bom.cdx.xml,并通过maven-deploy-plugin的classifier=bom上传,方便甲方自动化拉取。 - Git 提交钩子与 CI 质量门禁:合并后的 SBOM 若出现
critical > 0的漏洞,直接阻断合并请求,符合信通院《开源治理白皮书》要求。
答案
- 在父 pom 中 仅声明一次 CycloneDX 插件,避免子模块重复执行:
<pluginManagement>
<plugins>
<plugin>
<groupId>org.cyclonedx</groupId>
<artifactId>cyclonedx-maven-plugin</artifactId>
<version>2.7.9</version>
<configuration>
<projectType>library</projectType>
<schemaVersion>1.5</schemaVersion>
<includeBomSerialNumber>true</includeBomSerialNumber>
<includeCompileScope>true</includeCompileScope>
<includeProvidedScope>false</includeProvidedScope>
<includeRuntimeScope>true</includeRuntimeScope>
<includeTestScope>false</includeTestScope>
<includeLicenseText>true</includeLicenseText>
<outputReactorProjects>true</outputReactorProjects>
</configuration>
</plugin>
</plugins>
</pluginManagement>
- 在根模块(打包类型 pom)显式绑定 aggregate 目标:
<build>
<plugins>
<plugin>
<groupId>org.cyclonedx</groupId>
<artifactId>cyclonedx-maven-plugin</artifactId>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>makeAggregateBom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
- CI 流水线(以 GitLab 为例)统一命令:
mvn -B -s settings-aliyun.xml clean verify -Dmaven.test.failure.ignore=false
产物 target/bom.xml 即为 合并后、去重后、带完整依赖树和许可证 的单一 SBOM,可直接交付甲方或上传至 Nexus 的 raw-sbom 仓库。
- 若甲方要求 JSON 格式,只需在 configuration 中加
<outputFormat>json</outputFormat>,插件会同步生成bom.json,两者哈希值保持一致,满足双格式存档场景。
拓展思考
- 多分支并行开发时如何保证 SBOM 版本一致性:在 GitFlow 的 release 分支触发
makeAggregateBom,并把产出物通过maven-deploy-plugin的classifier=bom部署到私服,开发分支只生成但不部署,避免覆盖正式版本。 - 与 GraalVM Native 镜像结合:native 构建会引入额外静态链接库,需在
package阶段后再次执行makeAggregateBom并追加classifier=native,形成第二份 SBOM,供甲方区分 JVM 与 Native 的依赖差异。 - 国内监管趋严下的“二次合并”:若项目还包含前端 Node 模块,可用
cyclonedx-cli的merge命令把 Java SBOM 与package-lock.json生成的 BOM 再合并成 “全栈单一 SBOM”,实现端到端开源治理,这也是面试官常追问的“全链路物料清单”方案。