上传自己java项目到maven中央仓库pom

发布时间 2023-06-15 16:02:34作者: DaenMax

前提

首先的你项目需要在Gitee或者Github上有仓库

我这里以Gitee是的yhchat-sdk-core仓库为例

开始

在sonatype上创建问题

访问sonatype注册并登录

创建一个问题

概要

填仓库名称

描述

随意写写

Group Id

填写自己的域名,如果没有域名的话,可以自行百度使用GiteeGithub的域名

需要根据项目的maven坐标按需填写,与maven项目pom配置一致

Project URL

填仓库地址:https://gitee.com/daenmax/yhchat-sdk-core

SCM url

填仓库Git地址:https://gitee.com/daenmax/yhchat-sdk-core.git

点击创建

然后会有人审核你这个问题,全程跟进,稍等几分钟,就会有评论

添加DNS解析

需要验证组ID域名是否是你的,所以需要添加一条DNS TXT解析

主机记录

OSSRH-92477是你的问题编号,字母改成消息就是主机记录ossrh-92477

记录值

问题页面的地址栏网址就是记录值:https://issues.sonatype.org/browse/OSSRH-92477

解析添加好之后,一般过几分钟生效,

添加完成按钮

因为我这里已经点过了,所以没这个按钮了,具体就是 response 还是 完成 来着,我也忘了,点就行了

如果过几分钟后,状态或者解决结果 变成了:已解决,就代表验证通过了

安装GnuPG软件,并生成签名

下载安装

去官网下载并安装:https://www.gpg4win.org/

创建秘钥

发布秘钥

弹窗提示就代表成功了

配置maven

settings.xml里添加以下内容


<server>
    <id>ossrh</id>
    <username>sonatype账号</username>
    <password>sonatype密码</password>
</server>

如图

项目POM配置

主要就是添加urllicensesscmdevelopersdistributionManagement等节点,具体参考下面我的完整POM文件

需要注意的是,plugins打包插件建议使用和我一样的,或者你自己弄也行,打包时,必须同时打包出来源码文档jar,否则后面的检查无法通过

另外需要注意的是,我们上传的远程仓库的地址是带s01前缀的,而比较旧的文章中不带s01前缀

这是因为旧的sonatype maven仓库已经资源满载了,所以官方新建了一个s01 maven仓库,未来可能还会有s02、s03

现阶段在sonatype JIRA管理平台上注册的用户,只能上传这个新的s01仓库

我的POM示例:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.12</version>
        <relativePath/>
    </parent>
    <groupId>cn.daenx</groupId>
    <artifactId>yhchat-sdk-core</artifactId>
    <version>1.0.0</version>
    <name>yhchat-sdk-core</name>
    <description>
        yhchat-sdk-core是云湖社交的Java版本的SDK核心服务,通过此Core可以快速构建您的云湖机器人,能让您以非常便捷的方式和云湖服务进行交互。
    </description>
    <url>https://gitee.com/daenmax/yhchat-sdk-core</url>

    <properties>
        <java.version>1.8</java.version>
        <spring-boot.version>2.7.12</spring-boot.version>
        <hutool.version>5.8.19</hutool.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- Hutool -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>${hutool.version}</version>
            <optional>true</optional>
        </dependency>
        <!--常用工具类 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <!--开源协议...-->
    <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>

    <scm>
        <connection>https://gitee.com/daenmax/yhchat-sdk-core.git</connection>
        <developerConnection>git@gitee.com:daenmax/yhchat-sdk-core.git</developerConnection>
        <url>https://gitee.com/daenmax/yhchat-sdk-core</url>
    </scm>

    <developers>
        <developer>
            <id>DaenMax</id>
            <name>DaenMax</name>
            <email>1330166565@qq.com</email>
            <roles>
                <role>Developer</role>
            </roles>
            <timezone>+8</timezone>
        </developer>
    </developers>

    <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>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.17</version>
                    <configuration>
                        <skip>true</skip>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.10.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <!--生成源码jar包-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- GPG -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-gpg-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>
                        <phase>verify</phase>
                        <goals>
                            <goal>sign</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <autoVersionSubmodules>true</autoVersionSubmodules>
                    <useReleaseProfile>false</useReleaseProfile>
                    <releaseProfiles>release</releaseProfiles>
                    <goals>deploy</goals>
                </configuration>
            </plugin>

            <!-- jdk1.8中必须增加Xdoclint:none,因为1.8 javadoc要求必须严格格式检查,否则会报错-->

            <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>
                        <configuration>
                            <additionalparam>-Xdoclint:none</additionalparam>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


</project>

打包并上传

需要注意以下几点:

1.环境变量里的JDK版本需要和你项目使用的JDK版本一致

2.不要使用IDEA的打包,需要使用CMD命令打包

在项目根目录打开CMD

执行打包命令

mvn clean install deploy -P release

然后会弹出一个框框,然你输入秘钥,这个就是上面说的这个密码一定要记住

如果成功的话,就代表成功了(好像有点废话)

关闭和发行

上面maven命令执行完,我们还需要进行进一步的操作
这是因为我们上传的jar放入了staging暂存库里面,我们需要把它转到release库

这其中sonatype平台会针对你的项目信息、jar文档、源码等内容进行完整性校验

完整性校验不通过的项目是无法转到release的,也就无法同步到maven中央仓库

暂存库登录地址:https://s01.oss.sonatype.org/#stagingRepositories

账号密码和sonatype一样

登录上来后,点击Refresh,就可以看到你刚刚上传的了

如果认为刚刚打包上传错了,那就点击Drop删除,然后重新执行maven打包上传命令

觉得无误后,点击Close,sonatype就会开始扫描和检查你的项目了,

如果检查通过,就点击Release开始发行了,发行成功后,就可以点击Drop删除

如果检查不通过,就点击下面的SummaryActivity来查看为啥不通过

在仓库中查看

至此,就完事了,在 Maven Central 可以立即进行搜索到

Maven Repository大概第二天才能搜索到

(在此期间,无法搜索到也不影响项目中使用坐标引用你的项目)

结束

一切都ok之后,回到Sonatype JIRA管理平台将“问题”关闭,至此,恭喜你~