迁移到 Maven Central 后 pom.xml的配置步骤
账户迁移
如果你近期登录maven中央仓库的后台 Nexus 存储库管理器,你会收到如下通知:
OSSRH 服务将于 2025 年 6 月 30 日终止使用。在此处了解有关如何转移到中央发布门户的更多信息。
6月30日很快就临近了,所以如果你有在maven中央仓库发布版本的需要,就要网站提供的链接中的说明完成账户迁移
迁移说明:
What is different between Central Portal and Legacy OSSRH? - Documentation
迁移过程并不复杂,按照上面的说明做就好了,这不是本文的重点,本文要说明的是,完成迁移后,java项目的pom.xml配置也要做相应的修改,才能使用新的maven中央仓库后台发布版本。
生成token
迁移后,要按下面的说明去生成一个用户token,并更新到$HOME/.m2/settings.xml
Generating a Portal Token for Publishing - Documentation
pom.xml配置
1. 移除旧的发布配置
删除原有的
<distributionManagement>
区块:<!-- 删除以下整个配置块 --> <distributionManagement> <snapshotRepository> <id>sonatype-nexus-snapshots</id> <name>Sonatype Nexus Snapshots</name> <url>https://oss.sonatype.org/content/repositories/snapshots/</url> </snapshotRepository> <repository> <id>sonatype-nexus-staging</id> <name>Nexus Release Repository</name> <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url> </repository> </distributionManagement>
2. 添加新的中央仓库发布插件
在 <profiles>
的构建插件部分添加 central-publishing-maven-plugin
:
<plugin>
<groupId>org.sonatype.central</groupId>
<artifactId>central-publishing-maven-plugin</artifactId>
<version>0.7.0</version> <!-- 建议检查最新版本 -->
<extensions>true</extensions>
<configuration>
<publishingServerId>maven-central-release</publishingServerId>
<autoPublish>false</autoPublish> <!-- 手动发布 -->
<waitUntil>uploaded</waitUntil> <!-- 等待上传完成 -->
</configuration>
</plugin>
3. 关键配置说明
publishingServerId
必须与 Mavensettings.xml
中的<server>
ID 匹配(包含 Sonatype 账户凭据)autoPublish
false
(推荐):手动验证后再发布,避免自动发布到生产环境true
:构建完成后自动发布(仅限测试环境)
waitUntil
uploaded
:等待文件上传到暂存库后即完成构建published
:等待文件发布到中央仓库后才完成构建(已弃用)
central-publishing-maven-plugin
插件的完整配置参数说明参见官方说明
《Publishing By Using the Maven Plugin》
4. 发布工作流程
mvn clean deploy # 将构件上传到暂存库
# 后续手动操作:
# 1. 登录 [Sonatype Central 控制台](https://central.sonatype.com/)
# 2. 验证暂存库中的构件
# 3. 关闭(Close)暂存库
# 4. 发布(Release)到 Maven Central
验证步骤
确保
~/.m2/settings.xml
包含:<servers> <server> <!-- 注意这个ID要与上面central-publishing-maven-plugin插件中定义的publishingServerId保持一致 --> <id>maven-central-release</id> <username>您的Sonatype账号</username> <password>您的令牌密码</password> </server> </servers>
测试部署:
mvn clean deploy -DskipTests
在 Central 控制台 检查暂存库
注意:插件要求 Maven 3.6.3+,建议定期检查插件最新版本以获取安全更新
完整的pom.xml示例可以参见我在码云上的项目:
https://gitee.com/l0km/sql2java/blob/master/pom.xml