如何发布 `test-jar` 并让消费者模块在 `integration-test` 阶段使用?

解读

在国内企业级微服务或 Spring Boot 多模块仓库中,测试固件(test-jar) 常被用来共享 Mock 类、测试基类、契约测试桩 等“测试时”资产。
面试官真正想确认的是:

  1. 你是否理解 Maven 坐标三元组 + classifier 机制;
  2. 你是否能把 test-jar 的“产”与“销” 串进完整的生命周期(package → deploy → 消费 → failsafe);
  3. 你是否知道 国内私服(Nexus 私服、阿里云效 Maven、JFrog 中国加速) 的权限与缓存策略,避免“本地能跑、CI 找不到包”。
    一句话:既要 正确打出 test-jar,又要 让下游在 integration-test 阶段无痛依赖,还要 兼顾公司私服规范。

知识点

  • maven-jar-plugin 的 test-jar goal:唯一官方方式生成 classifier = test-jar 的附属构件
  • classifier 与 type 的区别:坐标 = groupId:artifactId:version[:classifier][:type]
  • maven-failsafe-plugin 的 integration-test 阶段:默认绑定在 verify 之前,与 test 阶段隔离
  • 依赖范围 <scope>import</scope> 与 systemPath 的禁区:国内面试常踩坑,必须拒绝
  • 私服 2 级缓存(group 聚合仓 + 代理仓):deploy 后若消费者仍解析不到,90% 是 私服路由规则 或 snapshot/release 策略 写死
  • CI 镜像加速:GitHub Action / Jenkins 中国节点需把 settings.xml 里的 mirrorOf=* 指向自建私服,否则中央仓拉取超时
  • 多模块 reactor 顺序:mvn -pl :producer -am deploy 先保证 test-jar 进私服,再 mvn -pl :consumer -am verify

答案

一、生产者模块:打出并发布 test-jar

在 producer/pom.xml 中加入:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>3.3.0</version>
      <executions>
        <execution>
          <goals>
            <goal>test-jar</goal>
          </goals>
          <!-- 可选:绑定到 package 阶段,确保 reactor 顺序正确 -->
          <phase>package</phase>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

执行:

mvn clean deploy

此时私服里会出现 两个构件:

  1. producer-1.0.0.jar
  2. producer-1.0.0-test-jar.jar(classifier = test-jar)

二、消费者模块:在 integration-test 阶段依赖 test-jar

在 consumer/pom.xml 中:

<dependencies>
  <!-- 普通依赖 -->
  <dependency>
    <groupId>com.xxx</groupId>
    <artifactId>producer</artifactId>
    <version>1.0.0</version>
  </dependency>

  <!-- 测试固件依赖:classifier=test-jar, scope=test -->
  <dependency>
    <groupId>com.xxx</groupId>
    <artifactId>producer</artifactId>
    <version>1.0.0</version>
    <classifier>test-jar</classifier>
    <type>test-jar</type>
    <scope>test</scope>
  </dependency>
</dependencies>

<build>
  <plugins>
    <!-- 运行集成测试 -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-failsafe-plugin</artifactId>
      <version>3.0.0</version>
      <executions>
        <execution>
          <goals>
            <goal>integration-test</goal>
            <goal>verify</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

执行:

mvn clean verify

关键点:

  • classifier 与 type 必须同时写,否则 Maven 3.8+ 会解析为默认 jar 而找不到构件
  • scope=test 保证 test-jar 只在测试类路径出现,不会污染运行时
  • failsafe 插件 默认读取 src/test/java 下的 *IT.java,与单元测试隔离

三、国内私服踩坑 checklist

  1. deploy 后立刻被消费者解析不到
    → 确认私服 release 仓 允许 redeploy=false,若第一次 deploy 失败,私服会留下 .lastUpdated,需手动删除或改版本号
  2. snapshot 版本
    → 必须使用 1.0.0-SNAPSHOT,否则 snapshot 仓不会代理
  3. mirrorOf= 覆盖 central*
    → 在 settings.xml 里把公司私服放在第一个 mirror,避免中央仓 443 超时
  4. CI 并发
    → GitLab CI 中国节点建议加 -Daether.connector.basic.threads=1,防止 deploy 上传 400

拓展思考

  1. test-jar 过大(>10MB)导致私服磁盘暴涨,能否用 maven-test-fixture-plugin 或 单独 test-support 模块 替代?
  2. 契约测试 场景下,消费者需要 producer 的 stub jar,但版本节奏不同步,能否用 maven-replacer-plugin 动态改写 classifier 为 stub 并走 SNAPSHOT 最新策略?
  3. Gradle 迁移混合仓库:若下游用 Gradle,Gradle Module Metadata 会识别 Maven classifier 吗?需要加 artifact { classifier = 'test-jar', type = 'jar' } 显式声明
  4. 安全合规:test-jar 里可能含 Mock 密钥、内网 IP,国内银行项目要求 deploy 前跑 maven-shade-plugin 过滤敏感文件,如何配置 excludeResources?