Maven 中央仓库发布教程:基于 Gpg4win 与 Sonatype Central Portal

2026-09-14T03:22:26+08:00 | 5分钟阅读

Mizar
Maven 中央仓库发布教程:基于 Gpg4win 与 Sonatype Central Portal

Maven 中央仓库发布教程:基于 Gpg4win 与 Sonatype Central Portal

准备工作

工具说明
IDEA开发工具
JAVA JDKjava开发环境
Git拉取代码工具
GPG签名验证工具
Mavenmaven仓库
一个gihub账号

获取GPG签名

GPG下载

  1. 到官网(GnuPG - 下载),下载GPG工具。image-20240405192053716

  2. 安装GPG工具,安装成功结果如下图。image-20260913180454986

创建签名

Kleopatra中,点击新建openPGP密钥对

image-20260913180913714

  • 名称:自定义。

  • 电子邮箱地址

  • 使用密码短语保护生成的密钥。:勾选。

    • 密码

      image-20260913181317745

  • 创建成功之后,结果如下:

    image-20260913181908054

    创建成功之后,务必要备份私钥!!!

    image-20260914002004857

发布证书到服务器

发布

选择指定证书,点击在服务器上发布

image-20260913182642928

验证

发布成功后,点击在服务器上查找。通过邮箱查找,看一下是否发布成功。

image-20260914002135537

注意:点击搜索后,会导致Kleopatra 把服务器上的纯公钥下载到了本地。直接显示了这个纯公钥版本,私钥变成了 n/a。所以务必提前备份私钥!!!

问题与解决方案

  1. 如果发布证书到服务器遇到无法将证书导出到密钥服务器,因为密钥服务器的使用已被明确禁用这个错误,

    image-20260913182702498

    解决步骤如下:

    1. 点击顶部菜单栏的 设置(S),然后选择 配置 Kleopatraimage-20260913224848402

    2. 点击勾选 使用 OpenPGP 密钥服务器,在OpenPGP 密钥服务器:中填入hkps://keys.openpgp.orghkps://keyserver.ubuntu.com。点击应用,然后点击确定image-20260913224221363

    3. 重试发布证书到服务器,会出现警告在将您的证书导出到公共目录服务器之前,请确保您已创建吊销证书,以便在证书被泄露、丢失或您忘记密码时可以吊销该证书。 您确定要继续吗?image-20260913231046756

      原因:OpenPGP 的公共目录服务器(如 keys.openpgp.org)是去中心化的,一旦你把公钥上传上去,它就会被同步到全球各地的服务器上。你几乎无法将其彻底删除。因此,系统在提醒你:上传前请务必确认你已经创建了吊销证书,万一以后私钥泄露或忘记密码,你还可以通过吊销证书来声明该密钥作废。

    4. 生成吊销证书。选中证书,点击细节,然后点击生成吊销证书image-20260913230744643image-20260913230434077

    5. 再次重新发布,并在提示框中点击导出证书image-20260913231414248

    6. 点击后,Kleopatra 就会把你的公钥上传到你设置的公共密钥服务器(keys.openpgp.org),成功之后结果如下图。image-20260913231659343

      注意:可能需要邮箱验证(如果是首次发布到 keys.openpgp.org):这个服务器会给你证书里的邮箱发一封验证邮件。你需要登录邮箱,点击邮件里的确认链接,别人才能在服务器上搜索到你的公钥。

登录maven central repository

进入maven中央仓库官网(Maven Central),使用github账号登录maven中央仓库。

  1. 登录你的github账号 »> https://github.com/image-20260914033306246

  2. 登录完成之后 前去新版本maven中央仓库上传地址image-20260914033342221

  3. 选择你的github账号进行登录image-20260914033436449

  4. 登录成功后,结果如下图。

    image-20260914003702922

获取token

创建Token

点击View User Tokens,然后点击Generate User Token,创建Token。

image-20260914033456794

image-20260914004151077

image-20260914004528129

复制token

创建成功后,如下图所示,然后复制Token。

image-20260914004415933

添加Token到Maven的setting.xml文件

  1. 打开maven下的setting.xml文件。image-20260914033519559

  2. 将刚刚的token复制到setting.xml文件中,并修改id为centralimage-20260914033528512

创建Maven项目

创建Maven项目

在idea中,创建maven项目。

image-20260914005200893

image-20260914005249795

添加配置到pom.xml文件

<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.github.dreampathway</groupId>
  <artifactId>amoon-common</artifactId>
  <version>1.0.0</version>
  <packaging>jar</packaging>

  <name>${project.groupId}:${project.artifactId}</name>
  <url>你的项目仓库地址</url>
  <description>描述信息</description>

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

  <!-- 项目许可证,使用协议 -->
  <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>作者名称</name>
      <email>你github的邮箱</email>
      <organization>Sonatype</organization>
      <organizationUrl>http://www.sonatype.com</organizationUrl>
    </developer>
  </developers>
    
  <!-- SCM (Software Configuration Management) 配置:声明项目源代码的版本控制地址 -->
  <scm>
    <!-- 只读连接,用于别人克隆代码 -->
    <connection>scm:git:git://github.com/你的github账号/你的项目名称.git</connection>
    <!-- 开发者连接,用于你推送代码,使用 SSH 协议 -->
    <developerConnection>scm:git:ssh://git@github.com/你的github账号/你的项目名称.git</developerConnection>
    <!-- 浏览器查看地址,注意这里加了 .git 后缀,有的审核工具会校验 -->
    <url>https://github.com/你的github账号/你的项目名称</url>
  </scm>

   <!-- 发布地址 -->
  <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>
      <!--    Maven插件发布    -->
      <plugin>
        <groupId>org.sonatype.central</groupId>
        <artifactId>central-publishing-maven-plugin</artifactId>
        <version>0.11.0</version>
        <extensions>true</extensions>
        <configuration>
          <publishingServerId>central</publishingServerId>
          <autoPublish>false</autoPublish>
        </configuration>
      </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.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>
      <!--     生成源javadocs文件       -->
      <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>

    </plugins>
  </build>

</project>

github创建代码仓库

创建远程仓库

image-20240405193729413

image-20240405193834845

将项目共享到远程仓库

将项目与关联远程仓库。

修改pom.xml配置文件

  <groupId>io.github.dreampathway</groupId>
  <artifactId>amoon-common</artifactId>
  <version>1.0.0</version>

获取groupId

image-20240405194117089

image-20240405194149460

artifactId

项目名称。

version

版本自定义设置即可。

url

填写刚刚创建的远程仓库地址。

<url>你的项目仓库地址</url>
例如:<url>https://github.com/DreamPathway/amoon-common</url>

SCM配置

将项目地址改为刚刚创建的远程仓库项目。

<!-- SCM (Software Configuration Management) 配置:声明项目源代码的版本控制地址 -->
  <scm>
    <!-- 只读连接,用于别人克隆代码 -->
    <connection>scm:git:git://github.com/DreamPathway/amoon-common.git</connection>
    <!-- 开发者连接,用于你推送代码,使用 SSH 协议 -->
    <developerConnection>scm:git:ssh://git@github.com/DreamPathway/amoon-common.git</developerConnection>
    <!-- 浏览器查看地址,注意这里加了 .git 后缀,有的审核工具会校验 -->
    <url>https://github.com/DreamPathway/amoon-common</url>
  </scm>

部署至Maven中央仓库

部署maven项目

  1. 点击deploy,进行部署。image-20260914033729284

  2. 发布时,输入PGP密码。

    image-20260914024354496

    注意:执行发布的时候需要密码要输入(前提是你设置了密钥密码的化则需要输入)

  3. 执行编译成功之后的提示image-20260914033741103

发布

  1. 打开maven中央仓库官网,点击View Deployments,查看部署。image-20260914033800016

  2. 选中指定版本,点击publish,进行发布。image-20260914033808223

  3. 发布成功后,显示蓝色图标。

    image-20260914025310247

使用

通过dependency引入jar包。

    <dependency>
      <groupId>io.github.dreampathway</groupId>
      <artifactId>amoon-common</artifactId>
      <version>1.0.0</version>
    </dependency>

© 2024 - 2026 拾光舍

湘ICP备2025117227号-1 | 公安备案图标 湘公网安备43040002000293号
在线工具资源网站

在线工具官网

ProcessOn:👉ProcessOn在线作图

腾讯文档:👉腾讯文档

FlowUs 息流:👉FlowUs 息流

Mermaid Live Editor:👉在线流程图和图表编辑器 - 美人鱼实时编辑器

ME2在线工具(加密工具):👉ME2在线工具-在线开发学习生活工具箱

Textln:👉Textln文本智能云平台-通用文本解析

MinerU:👉MinerU数据提取工具

Crontab:👉Cron Expression Examples - Crontab.guru

正则表达式测试网站:👉regex101: build, test, and debug regex

技术资源网站

技术资源官网

Spring:👉Spring官网

MyBatis中文网 :👉MyBatis 中文网

Redis中文网:👉Redis中文网

Zookeeper:👉Apache ZooKeeper官网

Raft算法:👉Raft Consensus Algorithm官网

Hadoop:👉Apache Hadoop官网

Hive:👉Apache Hive官网

Phoenix:👉Apache Phoenix官网

Bootstrap中文网:👉Bootstrap中文网

Element:👉组件 | Element

Layui :👉Layui - 极简模块化前端 UI 组件库(官方文档)

FreeMarker :👉FreeMarker 中文官方参考手册

ztree树插件: 👉zTree – jQuery 树插件官网

x File Storage:👉x File Storage官网

社交链接

© 2024 - 2026 拾光舍

湘ICP备2025117227号-1 | 公安备案图标 湘公网安备43040002000293号