如何在 `package` 阶段生成两份清单(蓝/绿)?
解读
面试官真正想考察的是:
- 你是否理解 Maven 生命周期与 phase→goal→execution 的绑定关系;
- 能否在不破坏“约定优于配置”的前提下,用 官方插件 而非手写脚本,在同一 phase 内产出 多份差异化制品;
- 对国内常见的“蓝绿发布”场景是否具备落地经验,即:同一次构建要同时给出 蓝版本清单(旧版本号、旧镜像 tag)与 绿版本清单(新版本号、新镜像 tag),供 CD 平台做流量切换。
若只回答“用两个 profile 各打一次包”会被追问“双倍构建时间如何优化”;若回答“自己写 Ant 脚本”会被判为“侵入式”。因此必须给出 单轮构建、单 POM、零冗余时间 的解法。
知识点
- maven-antrun-plugin 或 maven-resources-plugin 的
execution可绑定到同一 phase,靠id区分; - maven-jar-plugin / maven-assembly-plugin 的
classifier能力,可在一次构建里生成 主制品 + 多份附属制品; - Maven 属性过滤机制
${...}配合profiles或command-line -D注入“蓝/绿”变量; - 国内私有仓库(Nexus 私服) 对 classifier 的索引规则与中央仓库完全一致,无需额外配置;
- CI 场景下 蓝绿清单通常只是文本文件(JSON/YAML),需以 附件(attached artifact) 形式安装到本地仓库并上传到私服,供 Ansible、Spinnaker 等 CD 工具拉取。
答案
在 pom.xml 的 <build><plugins> 段追加两段 同一 phase(package)不同 id 的 execution,借助 maven-resources-plugin 的过滤能力即可一次性生成蓝/绿两份清单,示例片段如下:
<properties>
<!-- 由 CI 传入:-Dgreen.version=1.2.0 -Dblue.version=1.1.9 -->
<green.version>${green.version}</green.version>
<blue.version>${blue.version}</blue.version>
</properties>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
<executions>
<!-- 生成绿清单 -->
<execution>
<id>generate-green</id>
<phase>package</phase>
<goals><goal>copy-resources</goal></goals>
<configuration>
<outputDirectory>${project.build.directory}/manifest</outputDirectory>
<resources>
<resource>
<directory>src/manifest</directory>
<filtering>true</filtering>
<includes><include>green.yaml</include></includes>
</resource>
</resources>
</configuration>
</execution>
<!-- 生成蓝清单 -->
<execution>
<id>generate-blue</id>
<phase>package</phase>
<goals><goal>copy-resources</goal></goals>
<configuration>
<outputDirectory>${project.build.directory}/manifest</outputDirectory>
<resources>
<resource>
<directory>src/manifest</directory>
<filtering>true</filtering>
<includes><include>blue.yaml</include></includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<!-- 把两份清单作为附属制品安装并上传 -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.4.0</version>
<executions>
<execution>
<id>attach-green</id>
<phase>package</phase>
<goals><goal>attach-artifact</goal></goals>
<configuration>
<artifacts>
<artifact>
<file>${project.build.directory}/manifest/green.yaml</file>
<classifier>manifest-green</classifier>
<type>yaml</type>
</artifact>
<artifact>
<file>${project.build.directory}/manifest/blue.yaml</file>
<classifier>manifest-blue</classifier>
<type>yaml</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
执行 单条命令
mvn clean package -Dgreen.version=1.2.0 -Dblue.version=1.1.9
即可在 target/manifest 得到已过滤的 green.yaml 与 blue.yaml,且两者会被附加安装到本地仓库,私服推送时同样带 manifest-green / manifest-blue 后缀,CD 平台按 classifier 拉取即可,无需二次构建。
拓展思考
- 若清单格式需动态切换为 JSON,只需把模板文件改为
.json并调整type>json<,其余机制不变; - 当项目为 多模块 时,可在父 POM 用
pluginManagement统一定义上述插件,子模块按需引用,避免重复; - 国内部分企业要求 “不可修改原包”,此时可以把蓝绿清单做成 zip 包 再 attach,用
maven-assembly-plugin一次性打包,classifier 设为manifest-bundle; - 若构建频率极高,可将 模板文件 放到公司私服里的
archetype或build-resources坐标下,子项目通过 overlay 机制拉取,实现 统一版本治理; - 未来升级到 Maven 4 后,可利用全新的 “build consumer” 特性,在
package阶段直接注册 multiple main artifacts,彻底摆脱 classifier 限制,但当前国内主流版本仍为 3.8.x,上述方案在可预见三年内仍是 最稳最优解。