摘要

发布到Maven中央仓库的方式有很多,该篇文章记录了ossrh方式(2025年06月30日停止)、centralPortal方式。

正文

一、OSSRH方式

1.1 预置环境

基于Centos7预置环境(均可以使用我的一键安装脚本

  • java 1.8
  • maven 3.6.3(maven3.8以上不再支持http,弃用)
  • gpg 2.0+(Centos7内置)

需要去System Dashboard - Sonatype JIRA创建一个Issue,使用的域名还要验证是否真实持有。

创建成功之后,如图所示,此时就可以进行jar包部署了。

image-20221026211614256.png

1.2 发布

1.2.1 创建密钥

安装生成随机字节的工具,用于密钥生成

sh
1
2
yum -y install rng-tools
rngd -r /dev/urandom

着手生成密钥

sh
1
gpg --gen-key

image-20221026233249970.png

将密钥发布到密钥服务器

sh
1
gpg --keyserver keyserver.ubuntu.com --send-keys 你的密钥

若存在密钥已发布,但是jar包发布不上去,并提示没有找到public key时。说明你本地可能有多套密钥。

建议删除.gnupg后,重新生成密钥上传。

也可以直接通过地址验证http://keyserver.ubuntu.com/pks/lookup?search=0A38545CF4E58EE578003B433B77B6C8441D129D&fingerprint=on&op=index

去掉空格,建议直接浏览器console

js
1
"B515 0A97 DB5A C076 3E6A  0BC2 D241 590E E91B 2A73".replaceAll(/\s+/g,"");

1.2.2 配置Maven

在maven的settings.xml里面添加

可以which mvn来获取到路径,进而找到/usr/local/maven/conf/settings.xml

Nexus Repository Manager

注意:

s01.oss.sonatype.orgoss.sonatype.org的账号密码不互通。

xml
1
2
3
4
5
<server>
    <id>ossrh</id>
    <username>sonatype用户名</username>
    <password>sonatype密码</password>
</server>

2024-11-25更新:同样的帐号密码原来一直可以正常提交,今天提示status: 401 Content access is protected by token。

经过查阅资料发现是授权方式变了,不能直接通过明文账号密码登录,而是需要通过UserToken

image-20241125231727258.jpg

将生成的<server/>信息保持原id不变,覆盖之前的<server/>即可。

参考maven 往中央库deploy提示status: 401 Content access is protected by token

1.2.3 配置pom

以我自己的pom为例,主要是将官方提供的plugin都cv了过来。

注意此处的ossrh要保持一致。

xml
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?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>top.meethigher</groupId>
    <artifactId>object-converter</artifactId>
    <version>1.0</version>


    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>
    </dependencies>

    
    <!--若有需要发布maven,将以下直接复制过去、改吧改吧即可-->
    
    <name>object-converter</name>
    <description>object converter(deep copy) based on annovation</description>
    <url>https://github.com/meethigher/object-converter</url>
    <licenses>
        <license>
            <name>Apache License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
            <distribution>repo</distribution>
            <comments>A business-friendly OSS license</comments>
        </license>
    </licenses>
    <scm>
        <url>https://github.com/meethigher/object-converter</url>
        <connection>https://github.com/meethigher/object-converter.git</connection>
    </scm>

    <!--开发者信息-->
    <developers>
        <developer>
            <name>meethigher</name>
            <id>meethigher</id>
            <email>meethigher@qq.com</email>
            <url>https://meethigher.top</url>
            <organizationUrl>https://github.com/meethigher</organizationUrl>
            <roles>
                <role>Developer</role>
            </roles>
            <timezone>+8</timezone>
        </developer>
    </developers>
    <!--分发远程仓库-->
    <distributionManagement>
        <snapshotRepository>
            <id>ossrh</id>
            <url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
        </snapshotRepository>
        <repository>
            <id>ossrh</id>
            <url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
        </repository>
    </distributionManagement>


    <build>
        <!--配置插件-->
        <plugins>
            <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>
            <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.5</version>
                <executions>
                    <execution>
                        <id>sign-artifacts</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>sign</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.sonatype.plugins</groupId>
                <artifactId>nexus-staging-maven-plugin</artifactId>
                <version>1.6.7</version>
                <extensions>true</extensions>
                <configuration>
                    <serverId>ossrh</serverId>
                    <nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
                    <autoReleaseAfterClose>true</autoReleaseAfterClose>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

1.2.4 部署

运行

sh
1
mvn clean deploy -DskipTests=true

使用上面这个命令的前提是,需要在maven的settings.xml里面,额外添加

xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<settings>
  <profiles>
    <profile>
      <id>ossrh</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <gpg.executable>gpg2</gpg.executable>
        <gpg.passphrase>the_pass_phrase</gpg.passphrase>
      </properties>
    </profile>
  </profiles>
</settings>

上面这种方式,太麻烦,我不推荐。我自己的做法是直接在centos下运行命令

sh
1
mvn clean deploy -DskipTests=true -Dgpg.passphrase=你生成密钥时输入的密码

运行结果

image-20221026234435268.png

同步结果如下图。

image-20221030012152717.png

image-20221030012220871.png

image-20221030012245873.png

1.3 参考

gpg(GnuPG)生成密钥时卡住在We need to generate a lot of random bytes_liuxin5521的博客-CSDN博客

如何上传自己的jar包到maven中央仓库(2021最新版)_Java鱼仔的博客-CSDN博客_jar包上传到maven仓库

我把自己的java库发布到了maven中央仓库,从此可以像Jackson、Spring的jar一样使用它了_程序员欣宸的博客-CSDN博客

使用 PGP 签名 - 中央存储库文档

Apache Maven - 中央存储库文档

1.4 OSSRH停止(2025年06月30)

参考OSSRH Sunset - Documentation

服务由Nexus Repository Manager迁移至Maven Central Portal

在迁移的过程中,发现我的账户是同步过去了,但是密码没有同步。此处通过忘记密码、接收邮件、重置密码,即可解决。

image-20250831124824695.png

二、CentralPortal

参考

2.1 验证迁移后的账户权限

通过 ossrh 迁移过来的账户,首先查看账户权限是否存在。登录Maven Central

image-20250831131324744.png

2.2 发布

在 maven 的 pom.xml 中添加发布插件。完整示例参考ossrh-demo/pom.xml at master · simpligility/ossrh-demo

该插件 Java8+ 可以使用

xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<plugin>
    <groupId>org.sonatype.central</groupId>
    <artifactId>central-publishing-maven-plugin</artifactId>
    <version>0.8.0</version>
    <extensions>true</extensions>
    <configuration>
        <publishingServerId>central</publishingServerId>
        <autoPublish>true</autoPublish>
        <waitUntil>published</waitUntil>
    </configuration>
</plugin>

生成用户令牌 Generate User Token

image-20250831131718443.png

配置 maven 的 settings.xml,以下是我发布时使用的一个完整示例。

xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?xml version="1.0" encoding="UTF-8"?>
<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">
    <pluginGroups>
    </pluginGroups>
    <proxies>
        <proxy>
            <id>optional</id>
            <active>true</active>
            <protocol>http</protocol>
            <host>10.0.0.1</host>
            <port>1081</port>
            <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
        </proxy>
    </proxies>
    <servers>
        <server>
            <id>central</id>
            <username><!-- your token username --></username>
            <password><!-- your token password --></password>
        </server>
    </servers>
    <mirrors>
    </mirrors>
    <profiles>
    </profiles>
</settings>

image-20250831135812206.png

image-20250831135948964.png