构建Roda应用时遭遇GitHub Maven仓库401未授权访问问题求助
问题描述
我在尝试构建Roda应用,遵循官方开发者指南,但执行Maven构建时遇到了401未授权错误,具体报错信息如下:
[ERROR] Failed to execute goal on project roda-core: Could not resolve dependencies for project org.roda-project:roda-core:jar:4.6.0-SNAPSHOT:
Failed to collect dependencies at org.roda-project:commons-ip2:jar:2.3.0: Failed to read artifact descriptor for org.roda-project:commons-ip2:jar:2.3.0:
Could not transfer artifact org.roda-project:commons-ip2:pom:2.3.0 from/to github (https://maven.pkg.github.com/keeps/): Authentication failed for https://maven.pkg.github.com/keeps//org/roda-project/commons-ip2/2.3.0/commons-ip2-2.3.0.pom 401 Unauthorized
我不是Maven专家,而且发现构建旧版本Roda 3时不会出现这个问题,猜测可能是新版本的开发依赖仓库有访问限制?希望能得到解决方法,谢谢!
解决方案
别担心,这个问题根源很明确:Roda 4用到的commons-ip2:2.3.0依赖托管在GitHub Packages的私有仓库里,而Maven访问私有GitHub仓库必须要身份验证;旧版本Roda 3的依赖没用到这个私有仓库,所以不需要认证就可以正常构建。
下面给你一步步的解决步骤:
生成GitHub个人访问令牌(PAT)
登录你的GitHub账号,依次进入「Settings」→「Developer settings」→「Personal access tokens」→「Tokens (classic)」,点击「Generate new token」。勾选read:packages权限(这是访问GitHub私有Maven包的必需权限),然后生成令牌,一定要把令牌保存好——它只会显示一次,丢了就得重新生成。配置Maven的settings.xml文件
Maven的配置文件一般在~/.m2/settings.xml(Linux/macOS系统)或者C:\Users\<你的用户名>\.m2\settings.xml(Windows系统),如果没有这个文件直接新建就行。在文件的
<servers>标签内添加以下认证配置:<server> <id>github</id> <username>你的GitHub用户名</username> <password>刚才生成的个人访问令牌</password> </server>注意这里的
<id>必须和报错信息里的仓库ID(也就是github)完全一致,这样Maven构建时才会自动匹配使用这个认证信息。确认项目pom.xml的仓库配置
检查项目根目录的pom.xml文件,确保已经正确配置了GitHub的Maven仓库,应该有类似这样的代码:<repository> <id>github</id> <name>GitHub Keeps Packages</name> <url>https://maven.pkg.github.com/keeps</url> </repository>这里的
<id>也要和settings.xml里配置的server ID保持一致。
完成以上配置后,重新运行Maven构建命令(比如mvn clean install),应该就能顺利拉取依赖、完成构建了。
备注:内容来源于stack exchange,提问作者Paul Bergström

