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

Maven部署至GitHub Packages时遭遇301永久重定向失败求助

问题描述

开发Java工具类依赖,本地部署使用正常,但部署到GitHub Packages时执行mvn clean deploy返回301永久重定向错误,错误日志如下:

[INFO] --- deploy:2.8.2:deploy (default-deploy) @ MyArtifactName ---
Uploading to github: https://www.github.com/MyUserName/MyRepoName/io/github/MyUserName/MyArtifactName/1.0/MyArtifactName-1.0.jar
Uploading to github: https://www.github.com/MyUserName/MyRepoName/io/github/MyUserName/MyArtifactName/1.0/MyArtifactName-1.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  19.692 s
[INFO] Finished at: 2024-09-08T00:10:46-04:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy) on project MyArtifactName: Failed to deploy artifacts: Could not transfer artifact io.github.MyUserName:MyArtifactName:jar:1.0 from/to github (https://www.github.com/MyUserName/MyRepoName): status code: 301, reason phrase: Moved Permanently (301) -> [Help 1]
[ERROR] 

环境:Mac OSX,VS Code,Maven 3.9.2/3.8.8,已配置GPG密钥、settings.xml和pom.xml。

现有配置

settings.xml

<settings>
    <servers>
        <server>
            <id>github</id>
            <username>MyUserName</username>
            <password>GITHUB_CLASSIC_TOKEN_GENERATED</password>
        </server>
    </servers>

    <profiles>
        <profile>
            <id>github</id>
            <repositories>
                <repository>
                    <id>central</id>
                    <url>https://repo1.maven.org/maven2</url>
                </repository>
                <repository>
                    <id>github</id>
                    <url>https://maven.pkg.github.com/MyUserName/MyRepoName</url>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
        </profile>
    </profiles>
</settings>

pom.xml(关键片段)

<distributionManagement>
    <snapshotRepository>
        <id>github</id>
        <name>GitHub Packages</name>
        <url>https://www.github.com/MyUserName/MyRepoName</url>
    </snapshotRepository>
    <repository>
        <id>github</id>
        <name>GitHub Packages</name>
        <url>https://www.github.com/MyUserName/MyRepoName</url>
    </repository>
</distributionManagement>

<scm>
    <connection>scm:git:git://github.com/MyUserName/MyRepoName.git</connection>
    <developerConnection>scm:git:ssh://github.com:MyUserName/MyUserName.git</developerConnection>
    <url>http://github.com/MyUserName/MyRepoName/tree/main</url>
</scm>

<plugin>
    <groupId>org.sonatype.plugins</groupId>
    <artifactId>nexus-staging-maven-plugin</artifactId>
    <version>1.6.7</version>
    <extensions>true</extensions>
    <configuration>
        <serverId>github</serverId>
        <nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
        <autoReleaseAfterClose>true</autoReleaseAfterClose>
    </configuration>
</plugin>
错误原因及解决办法

核心原因

301错误的直接原因是GitHub Packages的仓库URL配置错误:pom.xml的distributionManagement中使用了https://www.github.com/MyUserName/MyRepoName,这是GitHub仓库的网页地址,而非Maven包的部署地址,GitHub会将这个请求重定向到正确的packages地址,但Maven默认不跟随重定向,导致报错。

具体修复步骤

1. 修正distributionManagement的URL

将pom.xml中distributionManagement下的snapshotRepository和repository的url修改为GitHub Packages的正确地址:

<distributionManagement>
    <snapshotRepository>
        <id>github</id>
        <name>GitHub Packages</name>
        <url>https://maven.pkg.github.com/MyUserName/MyRepoName</url>
    </snapshotRepository>
    <repository>
        <id>github</id>
        <name>GitHub Packages</name>
        <url>https://maven.pkg.github.com/MyUserName/MyRepoName</url>
    </repository>
</distributionManagement>

该地址需与settings.xml中repository的url保持一致。

2. 修正SCM配置中的developerConnection错误

当前developerConnection的仓库名写错,应改为项目仓库MyRepoName,同时修正SSH地址格式:

<scm>
    <connection>scm:git:git://github.com/MyUserName/MyRepoName.git</connection>
    <developerConnection>scm:git:ssh://git@github.com/MyUserName/MyRepoName.git</developerConnection>
    <url>http://github.com/MyUserName/MyRepoName/tree/main</url>
</scm>

3. 调整nexus-staging-maven-plugin配置

该插件用于部署到Sonatype OSS(Maven Central前置仓库),与当前部署到GitHub Packages的需求冲突,建议:

  • 若仅需部署到GitHub Packages,暂时注释或移除该插件;
  • 若后续需部署到Maven Central,将该插件放到单独的profile中,避免影响GitHub部署。

示例:

<!-- 注释或移除该插件,或放到专属profile中 -->
<!--
<plugin>
    <groupId>org.sonatype.plugins</groupId>
    <artifactId>nexus-staging-maven-plugin</artifactId>
    <version>1.6.7</version>
    <extensions>true</extensions>
    <configuration>
        <serverId>github</serverId>
        <nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
        <autoReleaseAfterClose>true</autoReleaseAfterClose>
    </configuration>
</plugin>
-->

4. 验证GitHub Token权限

确保你的GitHub经典token拥有write:packages、read:packages和repo权限,否则会出现认证失败或权限不足问题。

验证修复

修改配置后,执行mvn clean deploy -P github(指定github profile),即可正常部署到GitHub Packages。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 15:33:10