最近在看 Spring。依托Spring Framework写了一个轻量级的 ORM 框架,但是想把他上传到 Maven central repository,其他人引入 pom 文件就可以使用了。
查看了一下网上的资料,发现大部分资料都是旧版通过 OSSRH 发布的教程比较麻烦,而且从 2024 年 3 月 12 日起,maven central reository 可以通过 Central Portal(中央门户)发布操作相对来说比较简单。
本篇文章记录了通过 Central Portal(中央门户)发布自己项目的这样一个过程,也踩了很多坑。
一、注册账号
如果没有注册过(Central Portal)中央门户的账号则需要通过 https://central.sonatype.com/ 注册一下账号。
二、创建 NameSpace
在注册账号之后,登录进去,首先需要创建一个 NameSpace 名称空间。在发布组件的时候,必须选择一个名称空间,在 Maven 生态中,也可以将 NameSpace 看作为 groupId。它是描述发布到 Maven Central 的任何组件所需的三个坐标之一,即 groupId、artifactId、version。
Add NameSpace
名称空间采用反向 DNS 的方式,你可以设置你自己的gitee账号,或者github账号,实际就是设置你的项目在maven中 groupId。
gitee:com.gitee.你的gitee用户名
github:io.github.你的github用户名
我采用的是github的方式创建NameSpace。
创建完成之后需要验证你的GitHub或者Gitee账号,验证方式就是在Github或者GItee中创建一个 Verification Key 的公用仓库。我需要在GitHub中创建一个 uetwnyns2i 公有仓库。如图:
名为 uetwnyns2i 公有仓库创建完成。
验证名称空间。
校验完成。
三、安装GnuPG软件,并生成签名
新建NameSpace校验通过之后,我们需要在开发PC上安装GnuPG,因为我们上传到sonatype maven库的所有文件都要经过GnuPG进行签名,不经过签名的文件无法上传成功。下载地址:https://gpg4win.org/thanks-for-download.html 。下载完成之后,傻瓜式的“下一步”“下一步”安装即可。
下载完成之后,我们打开windows的 CMD命令行,执行如下命令。(注意:我的操作都是在CMD命令行下面执行的,不要power shell ,不要git bash。我都试过,不是不行,是操作过程不一致。如果和我的操作过程不一致,可能导致你操作失败!)
gpg --gen-key
下图中红色的部分是我填写的内容,我的名字、邮箱,以及一个O表示ok。会提示我们输入一个Passphrase,填写2遍。这个Passphrase就是一个密码,一定要记住,下文会用到。
GnuPG签名的公钥私钥对生成完成之后,可以使用如下命令查看结果。
C:\Users\hanxt>gpg --list-key
gpg: checking the trustdb
gpg: marginals needed: 3 completes needed: 1 trust model: pgp
gpg: depth: 0 valid: 1 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 1u
gpg: next trustdb check due at 2024-08-09
C:\Users\hanxt\AppData\Roaming\gnupg\pubring.kbx
------------------------------------------------
pub ed25519 2022-08-10 [SC] [expires: 2024-08-09]
6B4F6A477A1BE195326AEAFA0EE41461FB92CD0B
uid [ultimate] HanXiaotong <hanxiaotongtong@163.com>
sub cv25519 2022-08-10 [E] [expires: 2024-08-09]
密钥对生成完成之后,将其公钥发往sonatype认可的keyserver,公钥的字符串可以通过上文中的gpg --list-key 查看到
gpg --keyserver keyserver.ubuntu.com --send-keys 6B4F6A477A1BE195326AEAFA0EE41461FB92CD0B
通过下面的命令行,可以校验公钥的字符串发往服务器是否成功。
gpg --keyserver keyserver.ubuntu.com --recv-keys 6B4F6A477A1BE195326AEAFA0EE41461FB92CD0B
四、使用 Maven 构建插件发布到 Maven Central Repository
配置Maven pom 文件
<groupId>io.github.y00112</groupId>
<artifactId>zhaoysdb-orm</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>com.zhaoyss:zhaoysdb-orm</name>
<description>A simple ORM framework</description>
<url>https://github.com/y00112/zhaoyss-orm</url>
<!-- 组件的开源协议 -->
<licenses>
<license>
<name>MIT License</name>
<url>https://opensource.org/licenses/MIT</url>
<distribution>repo</distribution>
</license>
</licenses>
<scm>
<connection>https://github.com/y00112/zhaoyss-orm.git</connection>
<url>https://github.com/y00112/zhaoyss-orm</url>
</scm>
<developers>
<developer>
<name>zhaoyss</name>
<email>925191823@qq.com</email>
<roles>
<role>Developer</role>
</roles>
<timezone>+8</timezone>
</developer>
</developers>
<!-- Maven 打包配置-->
<build>
<plugins>
<!-- javadoc -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.3.1</version>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 签名文件:maven-gpg-plugin插件会调用上文中安装的GnuPG软件,对文件进行签名 -->
<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>
<!-- 附加源代码文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<!-- 需要 settings.xml 使用用户令牌凭据进行配置 -->
<plugin>
<groupId>org.sonatype.central</groupId>
<artifactId>central-publishing-maven-plugin</artifactId>
<version>0.5.0</version>
<extensions>true</extensions>
<configuration>
<publishingServerId>central</publishingServerId>
</configuration>
</plugin>
</plugins>
</build>
在 Maven Setting 文件中配置用户令牌凭据。生成用户令牌 https://central.sonatype.com/account
复制 Username 和 Password 添加到 Maven Setting中。
<settings>
<servers>
<server>
<id>central</id>
<username><!-- your token username --></username>
<password><!-- your token password --></password>
</server>
</servers>
</settings>
配置完成,运行 maven deploy 命令,或者在 IDEA 软件中 Maven 那双击 deploy 即可。
配置生成后,running mvn deploy 将生成一个捆绑包并将其上传到门户进行验证。成功的发布将类似于以下内容:
$ mvn deploy
[INFO] Scanning for projects...
[INFO] Inspecting build with total of 1 modules
[INFO] Installing Central Publishing features
...
[INFO] --- central-publishing-maven-plugin:0.5.0:publish (injected-central-publishing) @ example ---
[INFO] Using Central baseUrl: https://central.sonatype.com
[INFO] Using credentials from server id central in settings.xml
[INFO] Using Usertoken auth, with namecode: XXXXXXX
[INFO] Staging 2 files
[INFO] Staging /central/tests/sonatype-central-example/pom.xml
[INFO] Installing /central/tests/sonatype-central-example/pom.xml to /central/tests/sonatype-central-example/target/central-staging/org/sonatype/central/test/example/1.0.0/example-1.0.0.pom
[INFO] Staging /central/tests/sonatype-central-example/target/example-1.0.0.pom.asc
[INFO] Installing /Users/sonatype/workspace/central/tests/sonatype-central-example/target/example-1.0.0.pom.asc to /Users/sonatype/workspace/central/tests/sonatype-central-example/target/central-staging/org/sonatype/central/test/example/1.0.0/example-1.0.0.pom.asc
[INFO] Pre Bundling - deleted /central/tests/sonatype-central-example/target/central-staging/org/sonatype/central/test/example/maven-metadata-central-staging.xml
[INFO] Generate checksums for dir: org/sonatype/central/test/example/1.0.0
[INFO] Going to create /central/tests/sonatype-central-example/target/central-publishing/central-bundle.zip by bundling content at /central/tests/sonatype-central-example/target/central-staging
[INFO] Created bundle successfully /central/tests/sonatype-central-example/target/central-staging/central-bundle.zip
[INFO] Going to upload /central/tests/sonatype-central-example/target/central-publishing/central-bundle.zip
[INFO] Uploaded bundle successfully, deployment name: Deployment, deploymentId: 9590fb21-a026-4451-9722-a7216b258f4d. Deployment will require manual publishing
[INFO] Waiting until Deployment 9590fb21-a026-4451-9722-a7216b258f4d is validated
[INFO] Deployment 9590fb21-a026-4451-9722-a7216b258f4d has been validated. To finish publishing visit https://central.sonatype.com/publishing/deployments
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 15.000 s
[INFO] Finished at: 2024-01-01T00:00:00-00:00
[INFO] ------------------------------------------------------------------------
五、完成
发布成功之后,就可以其他项目中通过 pom 导入使用你的工具类了。
<dependency>
<groupId>io.github.y00112</groupId>
<artifactId>zhaoysdb-orm</artifactId>
<version>1.0.0</version>
</dependency>
六、参考
- https://central.sonatype.org/register/central-portal/
- https://www.cnblogs.com/zimug/p/16575819.html