• 如何上传自己的Jar到Maven中央仓库


    项目开发过程中,我们常常会使用 Maven 从仓库拉取开源的第三方 Jar 包。本文将带领大家将自己写好的代码或开源项目发布到 Maven中央仓库中,让其他人可以直接依赖你的 Jar 包,而不需要先下载你的代码后 install 到本地。

    注册帐号

    点击以下链接进行账号注册,注册的信息要记住,后面还要用,而且这个密码格式要求比较严格

    https://issues.sonatype.org/secure/Signup!default.jspa

    Jira 申请

    注册登录过后,访问以下链接创建一个 issue,只有申请通过了才能进行后续的上传等操作。

    https://issues.sonatype.org/secure/CreateIssue.jspa?issuetype=21&pid=10134

    提交过后呢,过几分钟就会有回复,同时你刚刚注册使用的邮箱也会收到邮件。

    注意!

    1. 这里输入的信息全部使用英文。
    2. 关于这个 GroupID,不能是你瞎编的域名,如果你正好使用的是自己的域名(反写),可以想我这样填写,如果你没有域名,就得使用 Github 的域名了,格式填写 io.github.用户名(后续验证会验证域名或 GitHub 账号的所有权)

    接下来过了几分钟就会收到回复,我们需要对填写的 GroupID 进行验证。

    我上面 GroupID 填写的域名(反写)world.xuewei,所以我在这里需要添加一个 @ 方式的 TXT 类型的解析记录,内容为本次提交的 Issue 编号。如果你的 GroupID 的域名的下级,例如 world.xuewei.test 那么添加记录的时候可能要添加二级域名的记录,不能使用 @,我猜的,反正都试试。

    配置好后,重新点击编辑,然后直接提交即可,然后需要再等几分钟就会收到回复如下:

    这种就 OK 了,可以进行后续的操作了。

    GPG 环境安装

    GPG 的主要作用是生成密钥对,会用于后续我们组件发布的校验。下载地址:https://www.gnupg.org/download/。

    找到适合自己设备的安装包后下载即可。

    安装完成后运行:

    1. 新建密钥对

    2. 选中证书后发布

    3. 双击证书查看秘钥,然后复制出来,一会要用。

    配置 Maven setting

    找到本地安装的 Maven 的配置文件(注意这里不是项目里面的 pom.xml),打开编辑。

    首先找到 标签,在里面添加以下内容:

    <server>
        <id>ossrhid>
        <username>XUEWusername>
        <password>这里是你第一步注册账号的时候的密码password>
    server>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    然后找到 标签,在里面添加以下内容(安装目录改成你的 GPG 目录,一定要到 bin 下的 gpg 这一层):

    <profile>
      <id>ossrhid>
      <activation>
        <activeByDefault>trueactiveByDefault>
      activation>
      <properties>
        <gpg.executable>D:\Program\GnuPG\bin\gpggpg.executable>
        <gpg.passphrase>这里是你刚刚在 GPG 中复制的秘钥gpg.passphrase>
      properties>
    profile>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    配置项目 Pom

    首先注意这里的 GroupID 一定要和前面申请的一样,不然在上传的时候就会报错。这个版本号最好改成数字的形式,就不要加默认的 -SNAPSHOT 了。

    首先需要在 Pom 中配置仓库的信息,这个信息也要和申请的一样,不然也会报错,内容如下:

    <licenses>
        <license>
            <name>The Apache Software License, Version 2.0name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txturl>
            <distribution>repodistribution>
        license>
    licenses>
    
    <scm>
        <connection>scm:git@github.com:373675032/xw-fast.gitconnection>
        <developerConnection>scm:git@github.com:373675032/xw-fast.git
        developerConnection>
        <url>https://github.com/373675032/xw-fasturl>
    scm>
    
    <developers>
        <developer>
            <name>XUEWname>
            <email>isxuewei@qq.comemail>
            <organization>https://github.com/373675032organization>
            <timezone>+8timezone>
        developer>
    developers>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    然后添加一些固有的信息,不需要更改:

    <distributionManagement>
        <snapshotRepository>
            <id>ossrhid>
            <url>https://s01.oss.sonatype.org/content/repositories/snapshotsurl>
        snapshotRepository>
        <repository>
            <id>ossrhid>
            <url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/url>
        repository>
    distributionManagement>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombokgroupId>
                            <artifactId>lombokartifactId>
                        exclude>
                    excludes>
                configuration>
            plugin>
    
            <plugin>
                <groupId>org.sonatype.pluginsgroupId>
                <artifactId>nexus-staging-maven-pluginartifactId>
                <version>1.6.7version>
                <extensions>trueextensions>
                <configuration>
                    <serverId>ossrhserverId>
                    <nexusUrl>https://s01.oss.sonatype.org/nexusUrl>
                    <stagingProgressTimeoutMinutes>20stagingProgressTimeoutMinutes>
                    <autoReleaseAfterClose>trueautoReleaseAfterClose>
                configuration>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-source-pluginartifactId>
                <version>2.2.1version>
                <executions>
                    <execution>
                        <id>attach-sourcesid>
                        <goals>
                            <goal>jar-no-forkgoal>
                        goals>
                    execution>
                executions>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-gpg-pluginartifactId>
                <version>1.5version>
                <executions>
                    <execution>
                        <id>sign-artifactsid>
                        <phase>verifyphase>
                        <goals>
                            <goal>signgoal>
                        goals>
                    execution>
                executions>
            plugin>
    
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-javadoc-pluginartifactId>
                <configuration>
                    <additionalOptions>
                        <additionalOption>-Xdoclint:noneadditionalOption>
                    additionalOptions>
                configuration>
                <executions>
                    <execution>
                        <id>attach-javadocsid>
                        <goals>
                            <goal>jargoal>
                        goals>
                    execution>
                executions>
            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
    • 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

    完整的 POM 文件

    <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.0modelVersion>
    
        <groupId>world.xueweigroupId>
        <artifactId>xw-fast-parentartifactId>
        <version>1.0.0version>
        <packaging>pompackaging>
        <name>xw-fastname>
        <description>
          Xw-Fast 是一个专为 Java Web 开发的针对 Spring 系列框架封装的便捷开发脚手架,旨在降低框架的学习使用成本,提高工作效率,大大提升 Web 开发效率。
        description>
        <modules>
            <module>xw-fast-coremodule>
            <module>xw-fast-webmodule>
            <module>xw-fast-crudmodule>
            <module>xw-fast-allmodule>
        modules>
    
        <properties>
            <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
            <project.reporting.outputEncoding>utf-8project.reporting.outputEncoding>
    
            <Automatic-Module-Name>world.xuewei.fastAutomatic-Module-Name>
    
            
            <compile.version>8compile.version>
            <junit.version>5.9.2junit.version>
            <lombok.version>1.18.26lombok.version>
            <hutool.version>5.7.17hutool.version>
            <boot.version>2.7.17boot.version>
            <fastjson.version>1.2.47fastjson.version>
    
        properties>
    
        <dependencies>
            
            <dependency>
                <groupId>org.junit.vintagegroupId>
                <artifactId>junit-vintage-engineartifactId>
                <version>${junit.version}version>
                <scope>testscope>
            dependency>
    
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <version>${lombok.version}version>
                <scope>compilescope>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starterartifactId>
                <version>${boot.version}version>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
                <version>${boot.version}version>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-mailartifactId>
                <version>${boot.version}version>
            dependency>
        dependencies>
    
        <url>https://github.com/373675032/xw-fasturl>
    
        <licenses>
            <license>
                <name>The Apache Software License, Version 2.0name>
                <url>http://www.apache.org/licenses/LICENSE-2.0.txturl>
                <distribution>repodistribution>
            license>
        licenses>
        
        <scm>
            <connection>scm:git@github.com:373675032/xw-fast.gitconnection>
            <developerConnection>scm:git@github.com:373675032/xw-fast.git
            developerConnection>
            <url>https://github.com/373675032/xw-fasturl>
        scm>
        
        <developers>
            <developer>
                <name>XUEWname>
                <email>isxuewei@qq.comemail>
                <organization>https://github.com/373675032organization>
                <timezone>+8timezone>
            developer>
        developers>
    
        <distributionManagement>
            <snapshotRepository>
                <id>ossrhid>
                <url>https://s01.oss.sonatype.org/content/repositories/snapshotsurl>
            snapshotRepository>
            <repository>
                <id>ossrhid>
                <url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/url>
            repository>
        distributionManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                    <configuration>
                        <excludes>
                            <exclude>
                                <groupId>org.projectlombokgroupId>
                                <artifactId>lombokartifactId>
                            exclude>
                        excludes>
                    configuration>
                plugin>
    
                <plugin>
                    <groupId>org.sonatype.pluginsgroupId>
                    <artifactId>nexus-staging-maven-pluginartifactId>
                    <version>1.6.7version>
                    <extensions>trueextensions>
                    <configuration>
                        <serverId>ossrhserverId>
                        <nexusUrl>https://s01.oss.sonatype.org/nexusUrl>
                        <stagingProgressTimeoutMinutes>20stagingProgressTimeoutMinutes>
                        <autoReleaseAfterClose>trueautoReleaseAfterClose>
                    configuration>
                plugin>
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-source-pluginartifactId>
                    <version>2.2.1version>
                    <executions>
                        <execution>
                            <id>attach-sourcesid>
                            <goals>
                                <goal>jar-no-forkgoal>
                            goals>
                        execution>
                    executions>
                plugin>
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-gpg-pluginartifactId>
                    <version>1.5version>
                    <executions>
                        <execution>
                            <id>sign-artifactsid>
                            <phase>verifyphase>
                            <goals>
                                <goal>signgoal>
                            goals>
                        execution>
                    executions>
                plugin>
    
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-javadoc-pluginartifactId>
                    <configuration>
                        <additionalOptions>
                            <additionalOption>-Xdoclint:noneadditionalOption>
                        additionalOptions>
                    configuration>
                    <executions>
                        <execution>
                            <id>attach-javadocsid>
                            <goals>
                                <goal>jargoal>
                            goals>
                        execution>
                    executions>
                plugin>
            plugins>
        build>
    
    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
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183

    部署上传

    先刷新、然后清理,然后部署。

    部署的时间有点长,耐心等待,这个步骤就是最关键的了,我由于配置错误重试了很多次,心态都崩了…

    验证

    使用第一步注册的账号登录系统 https://s01.oss.sonatype.org/

    在前面正在部署的过程中可以观察下面这里:

    部署完成后可以观察下面这里:

    如果你找到了你的 Jar,那么恭喜你,你已经上传成功了!之后再需要等待两三个小时,在 https://search.maven.orghttps://mvnrepository.com 便可以搜到自己发布的依赖了!同时也会收到一封邮件通知。

    参考链接:

    1. JAVA 如何上传自己的jar包到Maven中央仓库_本地jar包上传到maven仓库-CSDN博客
    2. 将项目上传到 Maven 中央仓库(2023最新) - 知乎 (zhihu.com)
    3. 将jar包发布到maven的中央仓库细节整理 - 陈灬大灬海 - 博客园 (cnblogs.com)
  • 相关阅读:
    基于文本信息抽取的列控车载设备故障发现
    堆排序思想分享
    计算机网络期末98+冲刺笔记
    ArkTS声明式开发范式
    一条Select语句在MySQL-Server层的执行过程
    axios数据交互
    python进阶(26)collections标准库
    ESP32-C3 基于Arduino框架下Blinker点灯控制10路开关或继电器组
    程序员敲诈老板,或面临37年监禁
    好心情:双相情感障碍会影响记忆力吗
  • 原文地址:https://blog.csdn.net/weixin_53287520/article/details/134283785