其实网上有很多类似的文章,这里我只是记录下我自己发布成功的一个流程,和踩的坑
准备工作:
- 首先,要在https://issues.sonatype.org申请一个账号
- 在gitee或者github上创建一个项目,就是你要上传的jar的源码项目,如果已经有可以忽略
- 得有一个属于自己的域名,或者说有这个域名的管理权限,这个是用来审核用的,其他方式也可以,这种方式审核最快,我也是用这种方式,具体怎么用在后面会介绍。
准备好以上工作后,现在具体操作:
一.登录
1.登录https://issues.sonatype.org,点击创建按钮来创建一个新的issues工单
2.随后你会收到一封邮件,或者在你发布的这个工单下会看到他们工作人员的留言
其实他们主要审核的就是这个GroupId ,这里他说了两点验证方式,用第一种最快,这里介绍第一种,这也就是前面说的为什么要用自己的域名或者有管理权限的了,这里我用阿里云申请的域名,
然后去你的新建的工单底下用英文回复,说你拥有此域名,并且添加了那条TXT记录,再截个图一并发出去。
二. 配置 GPG:
打开 https://www.gpg4win.org/ 下载 GPG,点下一步直接安装。
记住一点,安装完 一定要重启电脑,别人我不知道,反正在这我坑了好久!
- 打开创建一个密钥对,
- 输入姓名和邮箱,
- 最后确认新建,
- 新建完成会创建结果中会有一个指纹,直接复制下来。
- 过程中需要自己输入一个8位以上的密码,请记住该密码,后面还需要用到.
三.上传公钥到 GPS key-servers
打开命令提示符窗口,输入下面命令将证书发送到远程服务器
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys 填前面复制的那个指纹
四.配置项目
1.配置 maven 目录 conf 文件夹中的 settings.xml 文件,设置一个 server,里面添加 JIRA 的账号和密码。
注意:这里的id要记住
<servers>
<server>
<id>ossrh</id>
<username>JIRA账号</username>
<password>JIRA密码</password>
</server>
</servers>
2.配置 Pom.xml
注意:这里后面有-SNAPSHOT的是快照版,不会发布在中央仓库,要发布中央仓库,去掉-SNAPSHOT即可,参考https://blog.csdn.net/pange1991/article/details/86360229
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>注意这里填写你创建工单时候验证的那个groupId</groupId>
<artifactId>就是用的时候的包名</artifactId>
<!--注意:这里后面有-SNAPSHOT的是快照版,不会发布在中央仓库,要发布中央仓库,去掉-SNAPSHOT即可-->
<version>0.0.1-SNAPSHOT</version>
<url>https://gitee.com/xxx/xxx</url>
<packaging>jar</packaging>
<name>safdsafsafa</name>
<description>sdfsdt</description>
<!--licenses信息-->
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<!--scm信息,创建工单的时候填过,就是你项目的源码地址,项目地址-->
<scm>
<url>https://gitee.com/xxxx/xxxx.git</url>
<connection>scm:https://gitee.com/xxxx/xxxx.git</connection>
<developerConnection>scm:git:https://gitee.com/xxxx/xxxx.git</developerConnection>
</scm>
<developers>
<developer>
<name>xxxx</name>
<email>xxxx@xxxx.com</email>
<organization>https://xxx.com</organization>
<organizationUrl>https://xxx.com</organizationUrl>
</developer>
</developers>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
</dependencies>
<build>
<plugins>
<!-- doc plugin,Maven API文档生成插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- resources plugin,Maven 资源插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- compiler plugin,Maven 编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
<!-- gpg plugin,用于签名认证 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<!--staging puglin,用于自动执行发布阶段(免手动),其他文章里写的要手动-->
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.7</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<!-- release plugin,用于发布到release仓库部署插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.4.2</version>
</plugin>
</plugins>
</build>
<!-- 这里引入 Settings.xml 中设置的用户名、密码 ,就是前面让你记住的那个ID-->
<distributionManagement>
<snapshotRepository>
<!--就是前面让你记住的那个ID-->
<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>
</project>
最后注意:正常的部署流程应该为:open阶段—>close阶段—>release阶段 执行完成这三个阶段就会发布到中央仓库,
但是我们配置了自动部署,所以不必担心。
<autoReleaseAfterClose>true</autoReleaseAfterClose>
五.发送项目到 Maven 中央仓库
在项目下,执行 maven 命令,部署项目到 Maven 仓库,过程中需要输入之前设置的 GPG 密码,输入一下即可。
mvn clean deploy
由于之前配置了自动发布,所以只要命令执行成功,就等几个小时到https://search.maven.org去搜索自己发布成功的jar吧,
也可以先登录https://oss.sonatype.org,账号密码还是JIRA申请的那个,在这里可以先搜搜看!
- 这个网站也就是手动发布的时候,需要登录的,
- 在Staging Repositories拉到最下,找到自己发布的,
- 依次open阶段—>close阶段—>release阶段 执行完成这三个阶段就会发布到中央仓库