之前很久没发布Maven中央仓库了,2024年6月发布突然报 status code 401 错误,一顿查询后发现仓库发布改到中央门户网站了 https://central.sonatype.com/。报错如下:  

发布构件到Maven中央仓库(2024-06更新版 - 解决2024年6月后发布报 status code 401 错误)_git

 没办法只能对应修改:

一、注册命名空间

1、访问 https://central.sonatype.com/ 使用之前注册的 jira账号 登陆

2、登陆后点击 Publish

发布构件到Maven中央仓库(2024-06更新版 - 解决2024年6月后发布报 status code 401 错误)_git_02

3、点击 Namespace 下的 Add  Namespace 创建一个命名空间。由于我的项目在 gitee 下,所以我的命名空间输入的是 io.gitee.rslai。其中 rslai 是我的gitee地址,例如我的 gitee 地址是 https://gitee.com/rslai,所以拼接后就是 io.gitee.rslai。

注意:如果你项目的 groupId 如下 <groupId>io.gitee.rslai.base.commons</groupId> 那你的命名空间就可以是 io.gitee.rslai

发布构件到Maven中央仓库(2024-06更新版 - 解决2024年6月后发布报 status code 401 错误)_maven_03

4、创建成功后如下图,会给你一个 Verification Key,此时需要在你 gitee 中创建一个 public 的仓库,仓库名称就是 Verification Key 的内容,我创建的是 v7zyxuylhc。

发布构件到Maven中央仓库(2024-06更新版 - 解决2024年6月后发布报 status code 401 错误)_git_04

 5、仓库创建好后点击 Verify Namespace 验证。

发布构件到Maven中央仓库(2024-06更新版 - 解决2024年6月后发布报 status code 401 错误)_git_05

 二、重新创建 push 临时 token

使用之前的用户名密码都会报 401,需要通过创建 token 生成用户名、密码

1、点击 View Accout 看到如下界面

发布构件到Maven中央仓库(2024-06更新版 - 解决2024年6月后发布报 status code 401 错误)_apache_06

2、点击 Generate User Token 生成用户名密码,记住此用户名密码,后续修改 mvn 的 settings.xml 时需要使用。

发布构件到Maven中央仓库(2024-06更新版 - 解决2024年6月后发布报 status code 401 错误)_apache_07

三、修改 mvn  的 settings.xml

1、打开你的 settings.xml 配置文件,修改 username、password 为创建 token 中生成的用户名、密码。

发布构件到Maven中央仓库(2024-06更新版 - 解决2024年6月后发布报 status code 401 错误)_git_08

2、id 是项目 pom.xml 中 publishingServerId 的内容,需要根据你的项目修改

发布构件到Maven中央仓库(2024-06更新版 - 解决2024年6月后发布报 status code 401 错误)_apache_09

 四、修改 pom.xml 配置

由于更改了发布方式,所以项目中的 pom.xml 配置也要跟着修改,主要修改 3个部分。

1、修改 groupId,需要根据之前创建的命名空间修改

发布构件到Maven中央仓库(2024-06更新版 - 解决2024年6月后发布报 status code 401 错误)_git_10

 2、去掉 distributionManagement ,入下图

发布构件到Maven中央仓库(2024-06更新版 - 解决2024年6月后发布报 status code 401 错误)_apache_11

 3、改动最大的就是 build 段

发布构件到Maven中央仓库(2024-06更新版 - 解决2024年6月后发布报 status code 401 错误)_maven_12

 4、完整 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>io.gitee.rslai.base.commons</groupId>
    <artifactId>util</artifactId>
    <version>0.12</version>

    <name>${project.groupId}:${project.artifactId}</name>
    <description>Encapsulation  httpclient</description>
    <url>https://gitee.com/rslai</url>

    <licenses>
        <license>
            <name>The Apache License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
        </license>
    </licenses>

    <developers>
        <developer>
            <name>rslai</name>
            <email>netlrs@163.com</email>
            <organization>rslai</organization>
            <organizationUrl>https://gitee.com/rslai</organizationUrl>
        </developer>
    </developers>

    <scm>
        <connection>scm:git:git@gitee.com:rslai/commons.util.git</connection>
        <developerConnection>scm:git:ssh://gitee.com:rslai/commons.util.git</developerConnection>
        <url>https://gitee.com/rslai/commons.util.git</url>
    </scm>

    <!-- 指定字符集 -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <!--   central发布插件    -->
            <plugin>
                <groupId>org.sonatype.central</groupId>
                <artifactId>central-publishing-maven-plugin</artifactId>
                <version>0.4.0</version>
                <extensions>true</extensions>
                <configuration>
                    <publishingServerId>ossrh</publishingServerId>
                    <tokenAuth>true</tokenAuth>
                </configuration>
            </plugin>
            <!--   source源码插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.2.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!--   javadoc插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.9.1</version>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-gpg-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>
                        <id>sign-artifacts</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>sign</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.2</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.17</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng-jdk14</artifactId>
            <version>4.4.7</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant</artifactId>
            <version>1.9.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant-compress</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.14</version>
        </dependency>
        <!-- XMind 库 -->
        <dependency>
            <groupId>com.github.eljah</groupId>
            <artifactId>xmindjbehaveplugin</artifactId>
            <version>0.8</version>
        </dependency>
    </dependencies>

    <profiles>
        <profile>
            <id>release</id>
            <build>
                <!--javadoc, source and gpg plugin from above-->
            </build>
        </profile>
    </profiles>

</project>

 

 五、发布到仓库

1、同样执行 deploy 发布到仓库

mvn clean deploy

2、发布失败在这里会查到对应错误,成功后点击 Publish 发布仓库

发布构件到Maven中央仓库(2024-06更新版 - 解决2024年6月后发布报 status code 401 错误)_maven_13

 3、Publish 后这个图标是蓝色,需要变成绿色后才能在仓库中查到

发布构件到Maven中央仓库(2024-06更新版 - 解决2024年6月后发布报 status code 401 错误)_apache_14

 六、在中央仓库中查找刚发布的 jar 包

1、等图标变绿后,输入 groupId 就可以查到刚才发布的 jar 包

发布构件到Maven中央仓库(2024-06更新版 - 解决2024年6月后发布报 status code 401 错误)_git_15

 2、这里可以看到详情

发布构件到Maven中央仓库(2024-06更新版 - 解决2024年6月后发布报 status code 401 错误)_git_16

 3、Versions 中可以看到历史版本

发布构件到Maven中央仓库(2024-06更新版 - 解决2024年6月后发布报 status code 401 错误)_git_17