You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maven访问GitHub私有包仓库认证失败问题求助

问题:Maven依赖GitHub Packages私有仓库认证失败

项目背景

  • 项目A(security):基于Maven构建,依赖项目B的构件
  • 项目B(common-util):基于Maven构建,已将构件发布到GitHub Packages私有仓库

报错信息

Could not resolve dependencies for project com.company.security:security:jar:0.0.1-SNAPSHOT: Failed to collect dependencies at com.company:common-util:jar:0.0.1-SNAPSHOT: Failed to read artifact descriptor for com.company:common-util:jar:0.0.1-SNAPSHOT: Could not transfer artifact com.company:common-util:pom:0.0.1-SNAPSHOT from/to github (https://maven.pkg.github.com/company/common-util): authentication failed for https://maven.pkg.github.com/username/common-util/com/company/common-util/0.0.1-SNAPSHOT/common-util-0.0.1-SNAPSHOT.pom,

已做操作

已生成具备读写权限的GitHub PAT(个人访问令牌),但认证失败问题仍未解决。

相关配置

Settings.xml

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                              http://maven.apache.org/xsd/settings-1.0.0.xsd">

    <servers>
       
        <server>
            <id>github</id> 
            <username>company</username> 
            <password>************</password> 
        </server>
    </servers>

    <profiles>
        <profile>
            <id>github</id> 
            <repositories>
                <repository>
                    <id>github</id> 
                    <url>https://maven.pkg.github.com/company/common-util</url> 
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>

            <pluginRepositories>
                <pluginRepository>
                    <id>github</id>
                    <url>https://maven.pkg.github.com/company/common-util</url>  
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>

    <activeProfiles>
        <activeProfile>github</activeProfile> 
    </activeProfiles>
</settings>

项目A pom.xml 依赖仓库配置

<repositories>
        <repository>
            <id>github</id>
            <url>https://maven.pkg.github.com/company/common-util</url>
        </repository>
    </repositories>

项目B pom.xml 发布配置

<distributionManagement>
        <repository>
            <id>github</id>
            <url>https://maven.pkg.github.com/company/common-util</url>
        </repository>
    </distributionManagement>

解决建议

从报错信息和配置来看,可从以下几点排查:

  1. 仓库URL与实际归属不匹配
    报错中实际请求的路径是https://maven.pkg.github.com/username/common-util/...,但配置的URL是https://maven.pkg.github.com/company/common-util,两者的归属(username和company)不一致。GitHub Packages的正确URL格式为https://maven.pkg.github.com/OWNER/REPOSITORY,其中OWNER是GitHub用户名或组织名,需确认项目B的仓库归属,修正配置的URL与实际一致。

  2. Settings.xml用户名配置错误
    GitHub Packages认证时,<username>需填写你的个人GitHub用户名(而非组织名),<password>填写生成的PAT。当前配置的<username>company</username>如果是组织名,会导致认证失败,改为个人用户名即可。

  3. PAT权限验证
    确认PAT已勾选必要权限:

    • read:packages:允许读取私有包
    • write:packages:允许发布包(若需继续发布)
      同时检查PAT是否过期或被撤销。
  4. 清理Maven本地缓存
    本地仓库可能存在损坏的依赖缓存,执行命令强制更新:

    mvn clean install -U
    
  5. 确认构件发布状态
    登录GitHub,进入项目B仓库的Packages选项卡,确认common-util:0.0.1-SNAPSHOT的pom和jar文件已成功上传,且路径与Maven请求的一致。


内容的提问来源于stack exchange,提问作者Elan Kavi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 05:53:15