maven:中央仓库验证方式改变:401 Content access is protected by token

前几天向maven中央仓库发布版本,执行上传命令mvn release:perform时报错了:

ERROR\] Failed to execute goal org.sonatype.plugins:nexus-staging-maven-plugin:1.6.13:deploy (injected-nexus-deploy) on project xxxxx: Failed to deploy artifacts: Could not transfer artifact xxxxx:pom:6.0.7-20240619.183701-1 from/to ossrh (https://oss.sonatype.org/content/repositories/snapshots): authentication failed for https://oss.sonatype.org/content/repositories/snapshots/xxxxxx.pom, status: 401 Content access is protected by token -\> \[Help 1

ERROR

ERROR\] To see the full stack trace of the errors, re-run Maven with the -e switch. \[ERROR\] Re-run Maven using the -X switch to enable full debug logging. \[ERROR

ERROR\] For more information about the errors and possible solutions, please read the following articles: \[ERROR\] \[Help 1\] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

我的maven中央仓库的账号注册了好多年了,一直正常使用,没有出现过问题。

最终找到mave官方说明:《401 Unauthorized error》知道了原因:

此错误消息表示您可能通过提供用户名密码明文验证执行到OSSRH的发布。然而这是以前允许的与OSSRH交互的方式,现在OSSRH发布的身份验证过程的更改了,要求使用用户令牌机制。我们的有关于《设置用户令牌》的文档会对您有所帮助。

如果你是maven中央仓库的老用户,而不是新注册用户,而且忽略了最近Sonatype启用了新的基于令牌的身份验证机制,则可能遇到与我同样的情况。

以前我们在$HOME/.m2/settings.xml如下以明文形式保存maven中央仓库的验证信息

xml 复制代码
    <server>
      <id>sonatype-nexus-snapshots</id>
      <username>${username}</username>
      <password>${password}</password>
    </server>
    <server>
      <id>sonatype-nexus-staging</id>
      <username>${username}</username>
      <password>${password}</password>
    </server>

新的基于令牌的身份验证机制要求将<username><password>字段以加密令牌形式保存。

需要参照《Generate a Token on NXRM servers》登录Nexus Repository Manager后台,在Profile/User Token/Access User Token界面如下 获取你的令牌

将得到的令牌数据如下更新$HOME/.m2/settings.xml中的 <username><password>字段就解决问题了。

参考资料

《401 Unauthorized error》
《Generate a Token on NXRM servers》
《Sonatype -- 401 Content access is protected by token》

相关推荐
誰能久伴不乏7 分钟前
Qt 开发中的父类与父对象的区别和父对象传递:如何选择 `QWidget` 或 `QObject`?
java·开发语言·qt
喜欢踢足球的老罗16 分钟前
在Spring Boot 3.3中使用Druid数据源及其监控功能
java·spring boot·后端·druid
Winn~33 分钟前
JVM垃圾回收器-ZGC
java·jvm·算法
bytebeats41 分钟前
深入探索 Java 21 的核心特性
java
程序员小假1 小时前
说一说 SpringBoot 中 CommandLineRunner
java·后端
sky_ph1 小时前
JAVA-GC浅析(一)
java·后端
爱coding的橙子1 小时前
每日算法刷题Day24 6.6:leetcode二分答案2道题,用时1h(下次计时20min没写出来直接看题解,节省时间)
java·算法·leetcode
岁忧1 小时前
(nice!!!)(LeetCode每日一题)2434. 使用机器人打印字典序最小的字符串(贪心+栈)
java·c++·算法·leetcode·职场和发展·go
天天摸鱼的java工程师1 小时前
@Autowired 注入失效?
java·后端
sss191s1 小时前
校招 Java 面试基础题目解析学习指南含新技术实操要点
java·python·面试