Skip to content

platform-parent: spring-boot repackage 替换主制品 → failsafe 集成切片找不到 @SpringBootConfiguration(service 仓 CI 红) #27

@JIAQIA

Description

@JIAQIA

源起:hashmatrix-security#4 实现期发现,且为 hashmatrix-security 当前 main CI 红的根因(PR #2 合入即红)。本仓已落临时 workaround(repackage 加 <classifier>exec</classifier> + Dockerfile 改 COPY *-exec.jar),现回升根因到 platform-parent 统一治理。

问题

spring-boot-maven-plugin:repackage(默认绑 package 阶段)替换主制品为可执行 fat-jar——类被搬到 BOOT-INF/classes/ 下。随后 maven-failsafe-pluginverify 阶段跑集成切片(*IT),其 @SpringBootTest 的类路径扫描读不到 @SpringBootConfiguration,抛:

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration,
you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

证据(已隔离验证)

  • mvn clean verify(含 package/repackage)→ IT bootstrap 失败(上述异常)。
  • mvn clean test-compile failsafe:integration-test failsafe:verify跳过 package/repackage)→ 同一 IT 正常 Found @SpringBootConfiguration ... SecurityApplication、6 测试全过。
  • 差异仅在 package 阶段是否执行 → 锁定 repackage 替换主制品为触发点。
  • 单测(surefire,test 阶段、早于 package)不受影响——只有 verify 阶段的 failsafe 切片中招。

影响

凡「spring-boot:repackage 默认配置 + failsafe 集成切片」的 service 仓都会复现。当前至少 hashmatrix-securitymain CI 因此长期红。属平台级模式问题,不应各仓各自打补丁。

建议修法(择一,主仓拍板)

  1. platform-parent 的 spring-boot-maven-plugin pluginManagement 默认加 <classifier>exec</classifier>:保留瘦 jar 为主制品(failsafe 扫 target/classes 正常)、fat-jar 走 -exec 分类附件。各 service 的 Dockerfile 统一 COPY *-exec.jar。(Spring Boot 官方推荐「保留瘦 jar 主制品」做法。)
  2. 或在 platform-parent 给 failsafe 默认配 <classesDirectory>${project.build.outputDirectory}</classesDirectory>,强制扫 target/classes 而非 repackage 后的 jar。

方案 1 副作用更小、最贴近官方;需在主仓文档同步「Dockerfile 取 *-exec.jar」约定,避免子仓踩坑。

临时 workaround(本仓现状)

  • pom.xml:repackage execution 加 <configuration><classifier>exec</classifier></configuration>
  • DockerfileCOPY --from=build /workspace/target/hashmatrix-security-*-exec.jar app.jar

根因上提 platform-parent 后,子仓可回退到默认(保留 Dockerfile 的 *-exec.jar 约定即可)。

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions