发布 Jar 包到 Maven Central

default

概要

将打包的 jar 包推送到 sonartype,之后可通过 mvnrepository 搜索到。

注册 sonartype

注册地址:https://central.sonatype.com/

此处演示为使用 github 注册。

sonartype 配置

创建命名空间

名称:io.github.myusername

验证 github

根据创建命名空间时,弹窗提示的内容,在github中创建对应名称的仓库,用于验证是否为该账号的拥有者。

Generate User Token

点击右上角头像区域,选择“Generate User Token”,复制生成的 server 信息,粘贴到 maven 的 setting.xml 的 servers 节点下。

1
2
3
4
5
<server>
    <id>自定义ID</id>
    <username>用生成的</username>
    <password>用生成的</password>
</server>

本地配置

下载安装 GPG

下载地址:https://gnupg.org/download/index.html

选择 Gpg4win 下载,双击安装。

配置 GPG 证书

  • 打开 kleopatra

  • 点击文件-新建密钥对

  • 名称填入 io.github.myusername,邮箱填入可用邮箱

  • 复制生成的指纹字符串,或者生成后,在证书列表里右键查看细节复制指纹

  • 点击主界面的“在服务器上查找”,输入指纹,点击搜索,有结果则可

本地打包推送

修改 pom 文件,添加以下内容

  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
<!--注意 version 不可包含 SNAPSHOT-->
<version>0.0.1</version>

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

<developers>
    <developer>
        <id>gear</id>
        <name>guoyingdong</name>
        <email>masiyi163163@163.com</email>
        <roles>
            <role>Project Manager</role>
            <role>Architect</role>
        </roles>
    </developer>
</developers>

<scm>
    <!--远程仓库git地址-->
    <connection>https://github.com/Gearinger/gear-common.git</connection>
    <!--github仓库地址-->
    <url>https://github.com/Gearinger/gear-common</url>
</scm>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <!--打包时排除lombok-->
                    <exclude>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>
        <!--   central发布插件    -->
        <plugin>
            <groupId>org.sonatype.central</groupId>
            <artifactId>central-publishing-maven-plugin</artifactId>
            <version>0.4.0</version>
            <extensions>true</extensions>
            <configuration>
                <!--与 setting.xml 中自定义 ID 一致-->
                <publishingServerId>gear</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>
            <configuration>
                <failOnError>false</failOnError>
            </configuration>
            <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>
            <configuration>
                <!--配置本地 gpg 的地址-->
                <executable>C:\Program Files (x86)\GnuPG\bin\gpg.exe</executable>
                <!--配置命名空间的名字-->
                <keyname>io.github.gearinger</keyname>
            </configuration>
            <executions>
                <execution>
                    <id>sign-artifacts</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>sign</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

打包推送

点击 maven 的 clean deploy,等待推送完成

发布

线上(https://central.sonatype.com/publishing/deployments)查看推送结果,其中会描述推送失败的原因,可根据描述调整

若无异常,点击 publish 即可发布

搜索 jar 包

访问 https://central.sonatype.com/,输入 jar 包名称搜索

参考

【1】如何发布jar包到maven中央仓库(2024年3月最新版保姆级教程)

Licensed under CC BY-NC-SA 4.0
Gear(夕照)的博客。记录开发、生活,以及一些不足为道的思考……