如何在 `pre-integration-test` 启动数据库并在 `post-integration-test` 导出日志?

解读

国内大多数公司把集成测试跑在 Jenkins/GitLab CI 里,数据库生命周期必须与 Maven 生命周期精准对齐:pre-integration-test 阶段拉起数据库,post-integration-test 阶段把容器日志、慢 SQL、错误堆栈导出成文件,供 SonarQube/测试报告插件二次分析。面试官想确认两点:

  1. 你能否不依赖运维脚本,仅用 Maven 插件完成“起-停-导出”闭环;
  2. 你能否保证无论测试成功或失败,日志都能落盘,否则 CI 无法排障。

知识点

  1. Maven 生命周期:pre-integration-test → integration-test → post-integration-test 三阶段顺序由 Failsafe 插件触发,任何阶段异常都会跳过后续阶段,需用 maven.test.failure.ignore 或 failIfNoTests=false 保证 post 必跑。
  2. 外部进程守护:exec-maven-plugin 的 async=true 只能起进程,不能自动杀;必须用 start-stop-daemon 或 docker-maven-plugin 的 <keepContainer>false</keepContainer> 保证退出即销毁。
  3. Docker 国内镜像加速:registry-mirrors 需配置阿里云/腾讯云地址,否则 CI 拉取 MySQL 官方镜像超时。
  4. 日志导出时机:post-integration-test 在 integration-test 之后执行,此时容器已停,需用 docker logs containerId > target/logs/db.log 或 kubectl logs 把历史日志落盘;若用 Testcontainers,则通过 GenericContainer#getLogs() 写文件。
  5. 多模块 reactor 安全:父 POM 里用 maven-failsafe-plugin 的 <dependenciesToScan> 避免子模块重复启停数据库,确保只在一个 module 里起容器。

答案

  1. 父 POM 统一版本与镜像加速
<properties>
    <mysql.image>mysql:8.0.36-debian</mysql.image>
    <docker.registry>registry.cn-hangzhou.aliyuncs.com</docker.registry>
</properties>
  1. 在 pre-integration-test 阶段用 docker-maven-plugin 启动容器并写容器 ID
<plugin>
    <groupId>io.fabric8</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>0.43.4</version>
    <configuration>
        <images>
            <image>
                <alias>it-mysql</alias>
                <name>${docker.registry}/mysql:8.0.36-debian</name>
                <run>
                    <ports>
                        <port>3306:3306</port>
                    </ports>
                    <env>
                        <MYSQL_ROOT_PASSWORD>root</MYSQL_ROOT_PASSWORD>
                        <MYSQL_DATABASE>testdb</MYSQL_DATABASE>
                    </env>
                    <wait>
                        <log>ready for connections</log>
                        <time>30000</time>
                    </wait>
                    <log>
                        <file>${project.build.directory}/logs/mysql-start.log</file>
                    </log>
                </run>
            </image>
        </images>
    </configuration>
    <executions>
        <execution>
            <id>start-db</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>start</goal>
            </goals>
        </execution>
    </executions>
</plugin>
  1. 在 post-integration-test 阶段导出完整日志并停止容器
<execution>
    <id>export-logs</id>
    <phase>post-integration-test</phase>
    <goals>
        <goal>logs</goal>
        <goal>stop</goal>
    </goals>
    <configuration>
        <images>
            <image>
                <alias>it-mysql</alias>
                <run>
                    <log>
                        <file>${project.build.directory}/logs/mysql-full.log</file>
                        <color>false</color>
                    </log>
                </run>
            </image>
        </images>
    </configuration>
</execution>
  1. 保证 post 阶段必跑
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>3.1.2</version>
    <configuration>
        <testFailureIgnore>false</testFailureIgnore> <!-- 测试失败即构建失败 -->
        <skipAfterFailureCount>0</skipAfterFailureCount>
    </configuration>
</plugin>
  1. CI 调用
mvn clean verify -Ddocker.registry=registry.cn-hangzhou.aliyuncs.com

构建结束后,target/logs/mysql-full.log 包含完整容器日志,无论测试是否通过都会被归档到 Jenkins 制品库。

拓展思考

  1. Testcontainers 方案:若公司 K8s 集群禁止 Docker socket 挂载,可改用 testcontainers-maven-plugin,在 JUnit 5 基类里 @Container 启动数据库,配合 maven-surefire-plugin 的 systemPropertyVariables 把日志路径传出来,实现“代码即基础设施”。
  2. 并行测试隔离:使用 maven-failsafe-plugin 的 <forkCount>2C</forkCount> 时,每个 fork 需要独立数据库端口,可通过 dynamic.port 占位符 + tc.jdbc.url 系统属性实现。
  3. 日志分级上传:在 post-integration-test 增加 maven-antrun-plugin 调用 grep ERROR mysql-full.log > mysql-error.log,只把 ERROR 级日志上传到 SonarQube 的 generic issue 格式,减少网络传输。