编程随笔 编程随笔
  • 前端
  • 后端
  • 嵌入式
  • 星球项目
  • 开源项目
  • 海康AGV
  • 四向车
  • 工具类
  • 项目仓库

    • 部署仓库 (opens new window)
    • 代码仓库 (opens new window)
  • vuepress插件

    • 自动生成导航栏与侧边栏 (opens new window)
    • 评论系统 (opens new window)
    • 全文搜索 (opens new window)
    • 选项卡 (opens new window)
    • 自动生成sitemap (opens new window)
  • 自主开发插件

    • 批量操作frontmatter (opens new window)
    • 链接美化 (opens new window)
    • 折叠代码块 (opens new window)
    • 复制代码块 (opens new window)

liyao52033

走运时,要想到倒霉,不要得意得过了头;倒霉时,要想到走运,不必垂头丧气。心态始终保持平衡,情绪始终保持稳定,此亦长寿之道
  • 前端
  • 后端
  • 嵌入式
  • 星球项目
  • 开源项目
  • 海康AGV
  • 四向车
  • 工具类
  • 项目仓库

    • 部署仓库 (opens new window)
    • 代码仓库 (opens new window)
  • vuepress插件

    • 自动生成导航栏与侧边栏 (opens new window)
    • 评论系统 (opens new window)
    • 全文搜索 (opens new window)
    • 选项卡 (opens new window)
    • 自动生成sitemap (opens new window)
  • 自主开发插件

    • 批量操作frontmatter (opens new window)
    • 链接美化 (opens new window)
    • 折叠代码块 (opens new window)
    • 复制代码块 (opens new window)
  • 常用软件

    • vuepress-plugin-element-ui
    • pandoc使用
    • meilisearch部署
    • cloudreve部署
    • zabbix部署
    • 常用网址
    • git使用
    • 安装 acme.sh
    • 中央仓库上传指南
      • 简介
      • 前置条件
      • 步骤概览
      • 详细步骤
      • 常见问题
      • 总结
  • 工具类
  • 常用软件
华总
2025-03-23
0
0
目录

中央仓库上传指南原创

# 简介

Maven 中央仓库是 Java 生态系统中最重要的依赖库之一,将你的项目发布到 Maven 中央仓库可以让全球的开发者更容易地使用你的库。本文将详细介绍如何将你的 Java 项目发布到 Maven 中央仓库的完整流程。

# 前置条件

  • JDK 8 或更高版本
  • Maven 3.0+
  • GPG(用于签名)
  • Sonatype OSSRH 账号

# 步骤概览

  1. 注册 Sonatype 账号
  2. 配置 GPG 签名
  3. 配置 Maven 的 settings.xml
  4. 配置项目的 pom.xml
  5. 发布到中央仓库

# 详细步骤

# 1. 注册 Sonatype 账号

从 2024 年 3 月 12 日起,所有注册都将通过 Sonatype 中央门户网站 (opens new window) 进行。

  1. 访问 Sonatype 中央门户网站 (opens new window)。
  2. 点击右上角的“注册”按钮。
  3. 填写用户名、密码、邮箱等信息。
  4. 完成注册并登录。

# 2. 配置 GPG 签名

为了保证上传到 Maven 中央仓库的文件的安全性和完整性,你需要使用 GPG 对文件进行签名。

  1. 首先查看GPG版本,确认可用,若没有则下载GnuPG for Windows (opens new window)

    gpg --version
    
    1
  2. 生成密钥

    快速生成密钥

    gpg --gen-key
    
    1

    生成完整密钥

    gpg --full-generate-key
    
    1

    在生成过程中,输入你的姓名、邮箱等信息,并设置一个安全的密码,保存公钥

  3. 查看 GPG 密钥

    gpg --list-keys
    
    1
  4. 上传密钥到maven服务器

    gpg --keyserver keyserver.ubuntu.com --send-keys 公钥
    
    1
  5. 校验公钥的字符串发往服务器是否成功

    gpg --keyserver keyserver.ubuntu.com --recv-keys 公钥
    
    1

# 3. 配置 Maven 的 settings.xml

  1. 配置 GPG 插件:在项目的 pom.xml 文件中,添加以下配置,以便 Maven 能够使用 GPG 对文件进行签名:

    <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-gpg-plugin</artifactId>
            <version>3.2.7</version>
            <executions>
              <execution>
                <id>sign-artifacts</id>
                <phase>verify</phase>
                <goals>
                  <goal>sign</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <gpgArguments>
                <!--表示密码直接输入,不需要弹出密码框-->
                <arg>--pinentry-mode</arg>
                <arg>loopback</arg>
              </gpgArguments>
            </configuration>
          </plugin>
        </plugins>
    </build>
    
    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
  2. 配置 Maven 中央仓库的服务器信息:在你的用户目录下的 .m2/settings.xml 文件中,添加以下配置:

    <servers>
        <server>
            <id>ossrh</id>
            <username>你的Sonatype用户名</username>
            <password>你的Sonatype密码</password>
        </server>
    </servers>
    
    <profiles>
    	   <profile>
    		  <id>gpg</id>
    		  <activation>
    			<activeByDefault>true</activeByDefault>
    		  </activation>
    		  <properties>
    			<gpg.executable>gpg</gpg.executable>
    			<gpg.keyname>生成密钥时设置的邮箱</gpg.keyname>
    			<gpg.passphrase>生成密钥时设置的密码</gpg.passphrase>
    			<gpg.useagent>true</gpg.useagent>
    		  </properties>
    		</profile>
    </profiles>  
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22

# 4. 配置项目的 pom.xml

确保 pom.xml 文件中包含以下信息:

  • 项目基本信息:groupId、artifactId、version、name、url、description。

  • 开源许可证:

    <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>
    
    1
    2
    3
    4
    5
    6
    7
    8
  • 开发者信息:

    <developers>
        <developer>
            <name>你的名字</name>
            <email>你的邮箱</email>
            <url>你的个人主页或GitHub主页</url>
        </developer>
    </developers>
    
    1
    2
    3
    4
    5
    6
    7
  • SCM 信息:

    <scm>
        <url>你的项目主页地址</url>
        <connection>你的项目Git地址</connection>
    </scm>
    
    1
    2
    3
    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.github.liyao52033</groupId>
      <artifactId>liyao5-spring-boot-starter-common</artifactId>
      <version>1.2.1</version>
      <name>common-spring-boot-starter</name>
      <description发布描述</description>
      <url>https://github.com/liyao52033/youlai-boot.git</url>
    
    
      <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.7.18</spring-boot.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
      </properties>
    
      <developers>
        <developer>
            <name>你的名字</name>
            <email>你的邮箱</email>
            <url>你的个人主页或GitHub主页</url>
        </developer>
     </developers>
    
      <scm>
        <connection>你的项目Git地址</connection>
        <developerConnection>scm:git:ssh:// 你的项目Git地址(ssh地址)
        </developerConnection>
        <url>你的项目Git地址</url>
      </scm>
    
      <dependencies>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter</artifactId>
          <version>${spring-boot.version}</version>
        </dependency>
    
       // 其他项目依赖...
        
      </dependencies>
    
      <distributionManagement>
        <snapshotRepository>
          <id>ossrh</id>
          <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        </snapshotRepository>
        <repository>
          <id>ossrh</id>
          <url>https://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>3.2.1</version>
            <executions>
              <execution>
                <id>attach-sources</id>
                <phase>verify</phase>
                <goals>
                  <goal>jar-no-fork</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>3.5.0</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>3.2.7</version>
            <executions>
              <execution>
                <id>sign-artifacts</id>
                <phase>verify</phase>
                <goals>
                  <goal>sign</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <gpgArguments>
                <!--表示密码直接输入,不需要弹出密码框-->
                <arg>--pinentry-mode</arg>
                <arg>loopback</arg>
              </gpgArguments>
            </configuration>
          </plugin>
          <plugin>
            <groupId>org.sonatype.central</groupId>
            <artifactId>central-publishing-maven-plugin</artifactId>
            <version>0.5.0</version>
            <extensions>true</extensions>
            <configuration>
              <publishingServerId>发布id</publishingServerId>
              <checksums>required</checksums>
              <deploymentName>发布人</deploymentName>
    
            </configuration>
          </plugin>
        </plugins>
      </build>
    
      <licenses>
        <license>
          <name>MIT License</name>
          <url>http://www.opensource.org/licenses/mit-license.php</url>
        </license>
      </licenses>
    
    
    </project>
    
    
    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
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135

# 5. 发布到中央仓库

  1. 在项目的根目录下,运行以下命令:

    mvn clean deploy 
    
    1

    这个命令会清理项目,然后构建项目,并将生成的文件上传到 Maven 中央仓库。

  2. 如果上传成功,你可以在 Maven 中央仓库 (opens new window) 中搜索你的项目,查看是否已经成功上传。

  3. 最后,在 Sonatype 中央门户网站 (opens new window) 点击“Publish”正式发布。

# 常见问题

# GPG 密钥问题

  • 问题:GPG 密钥生成失败。
    • 解决方法:检查你的系统是否安装了 GPG 工具。如果没有安装,可以通过以下命令安装:

      • 在 Ubuntu 系统中:
        sudo apt-get install gnupg
        
        1
      • 在 macOS 系统中:
        brew install gpg
        
        1

# Maven 配置问题

  • 问题:Maven 无法找到 settings.xml 文件。
    • 解决方法:确保你的 settings.xml 文件位于你的用户目录下的 .m2 文件夹中。如果文件不存在,你可以手动创建一个。

# 项目上传问题

  • 问题:项目上传失败,提示缺少某些文件。
    • 解决方法:确保你的项目满足 Maven 中央仓库的要求,例如必须有 Javadoc 文件、源码文件等。如果缺少,需要在 pom.xml 中添加相应的插件配置。

# 总结

将项目上传到 Maven 中央仓库是一个相对简单的过程,但需要一些准备工作和配置。通过本指南,你应该已经了解了如何创建 Sonatype 账号、生成 GPG 密钥、配置 Maven 以及提交项目申请和上传项目。希望你的项目能够成功上传到 Maven 中央仓库,为全球的开发者提供便利。

如果你在上传过程中遇到任何问题,可以参考 Sonatype 的官方文档或在相关社区寻求帮助。

#gpg
上次更新: 2025/03/23 15:42:56
安装 acme.sh

← 安装 acme.sh

最近更新
01
jFlash使用 原创
03-24
02
模板生成工具 原创
02-18
03
RTC实时时钟 原创
02-12
04
keil调试 原创
01-21
05
GPIO概述与配置 原创
01-20
更多文章>
Copyright © 2023-2025 liyao52033  All Rights Reserved
备案号:鄂ICP备2023023964号-1    
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式