在发布到 Maven Central 时,如何验证 staging 仓库的签名?
解读
国内公司把库推到 Maven Central 时,Sonatype OSSRH 会先在 staging 仓库 里暂存构件。只有通过 签名验证 与 规则校验 后,才能点 “Close”→“Release”。面试官问“如何验证”,既考察你对 GPG 签名链 的理解,也考察你是否真在 CI 里踩过坑:很多人本地 publishToMavenLocal 成功,一上 staging 就因签名被驳回。回答时要给出 可落地的命令与 Gradle 配置,并说明 国内网络下常见失败场景 的排查思路。
知识点
- GPG 双钥匙体系:私钥签名(
signing插件),公钥分发到 keyserver.ubuntu.com 或 keys.openpgp.org,Sonatype 用公钥验签。 - 签名产物:每个
.jar/.pom/.module必须伴随.ascASCII armor 文件;缺少即报 “signature missing”。 - staging 校验时机:执行
./gradlew publishCloseAndReleaseRepository或手动在 Nexus UI 点 “Close” 时触发;校验失败则仓库状态变为 “Failed” 并给出 XML 报告。 - Gradle 侧核心配置:
signing { useInMemoryPgpKeys(System.getenv("SIGNING_KEY"), System.getenv("SIGNING_PASSWORD")) }publishMavenJavaPublicationToMavenRepositoryTask 必须依赖signMavenJavaPublication。
- 国内加速:
~/.gnupg/gpg.conf里加 keyserver hkp://keyserver.ubuntu.com:80 并放行 11371 端口;CI 镜像若无法出网,可把公钥提前导入到 Nexus 白名单 或使用 GitHub Action 缓存公钥。 - 验证命令:
- 本地:
gpg --verify xxx-1.0.0.jar.asc xxx-1.0.0.jar - 远程:
curl -s https://keyserver.ubuntu.com/pks/lookup\?op\=get\&search\=0x${KEY_ID} | gpg --import后再验签。
- 本地:
- 失败码速查:
NO_PUBLIC_KEY→ 公钥未上传;BAD_SIGNATURE→ 私钥与上传的不匹配或文件被篡改;MISSING_SIGNATURE→signing插件未应用到该构件。
答案
分三步完成验证,确保 staging 仓库能顺利 Close。
-
本地预检
a. 确认私钥已加载:gpg --list-secret-keys --keyid-format SHORT能看到 ${KEY_ID}。
b. 执行./gradlew publishToMavenLocal,随后gpg --verify build/repo/.../xxx-1.0.0.jar.asc build/repo/.../xxx-1.0.0.jar,出现 “Good signature” 即本地签名链无误。 -
上传后远程校验
a. 推送 staging:./gradlew publishMavenJavaPublicationToOssrhRepository -Psigning.keyId=${KEY_ID} -Psigning.password=${SIGNING_PASSWORD} -Psigning.secretKeyRingFile=/tmp/secring.gpg(CI 里用 in-memory 方式更安)。
b. 登录 https://s01.oss.sonatype.org → Staging Repositories → 选中你的仓库,底部 “Activity” 标签若显示 “Repository closed” 说明签名已自动通过;若状态为 “Failed”,下载 “Rules Failures” 里的*.xml查看具体缺失的.asc文件。
c. 手动二次验签:把 staging 里的.jar与.asc下载到同一目录,执行gpg --verify xxx-1.0.0.jar.asc若提示 “Can’t check signature: No public key”,则把公钥上传到 keyserver 并等待 1~2 分钟同步,再点击 “Drop” 后重新上传。
-
CI 一键脚本(GitHub Actions 示例)
- name: Verify staging signature run: | REPO_ID=$(curl -s -u "${OSSRH_USER}:${OSSRH_PASS}" \ "https://s01.oss.sonatype.org/service/local/staging/profile_repositories" \ | xmllint --xpath "string(//data/repository[id[contains(text(),'comexample')]]/repositoryId)" -) curl -s -u "${OSSRH_USER}:${OSSRH_PASS}" \ "https://s01.oss.sonatype.org/service/local/repositories/${REPO_ID}/content/com/example/xxx/1.0.0/" \ | grep '.asc' | while read asc; do curl -sO "https://s01.oss.sonatype.org/service/local/repositories/${REPO_ID}/content/com/example/xxx/1.0.0/$asc" curl -sO "https://s01.oss.sonatype.org/service/local/repositories/${REPO_ID}/content/com/example/xxx/1.0.0/${asc%.asc}" gpg --verify "$asc" "${asc%.asc}" || exit 1 done脚本全部返回 “Good signature” 才继续执行
closeAndReleaseRepository。
拓展思考
- 自动化轮换签名:国内金融类 SDK 要求 SM2 国密签名,但 Maven Central 只认 GPG;可考虑在 双重发布 策略下,Central 走 GPG,自建仓库走国密,Gradle 用
artifact(tasks.registerSm2Signature)动态附加不同签名。 - 远程构建缓存与签名冲突:开启 Gradle Build Cache 后,
.asc文件不会被缓存,导致二次构建重复签名;解决方法是把signing任务声明为outputs.cacheIf { false },或把签名逻辑挪到 publish 阶段之后。 - 私有 Nexus 代理场景:很多国企在内网部署 Nexus Repository Manager 代理 Central,同时做 staging 预审;此时要把公钥提前导入 Nexus 的 PGP Key Server 模块,否则内网同样报
NO_PUBLIC_KEY。 - 未来趋势:Sonatype 已宣布 **2025 年后强制使用 “publish central portal” 新端点,签名验证将改为 API 上传时即验签,不再提供手动 Close 按钮;Gradle 7.6+ 的
maven-publish插件正在适配新 Portal,需关注io.github.gradle-nexus.publish-plugin的 2.x 分支 升级路线。