本文旨在记录我的首个开源项目easy-socket成功发布到maven中心仓库的过程,供我自己和后来者参考。github项目地址:https://github.com/xiaoyudeguang/easy-socket。码云项目地址:https://gitee.com/xiaoyudeguang/springboot-socket
1. 注册账号
去官网https://issues.sonatype.org注册一个账号,Full name随意,username别包含中文。注册成功后回来,我们继续。
这是我注册后点头像出来的的信息面板:
2. 获取发布权限
创建一个申请单,随便找个地点Create按钮,打开申请单面板,如下:
填完点create,最好按要求来,不然会很麻烦,我的groupId按网上教程填的com.github.项目名,结果通不过的,填io.github.项目名就可以了。信哥,不然你得把大把时间花在和审核官英文聊天上。。。不得不说的血泪史啊。。。如果你晚上九点以后创建,一会就通过了,如果是白天创建的单子,你就等吧。。。如果等了一天以上还没回复,再创建一个一毛一样的申请单试试,别怀疑人品,人家审核官很忙,可能没看到吧,一直等到有回复的那个订单,你就可以下一步了(ps:我等了一天)通过了是这样的:
3. Maven准备
打开你本地的maven的setting.xml文件(要是你不会这个,我也没办法了)。在文件中找然后加上这段:
<server>
<id>oss</id>
<username>你的sonatype账号</username>
<password>你的sonatype密码</password>
</server>
4. 项目准备(在Eclipse中)
1. 打开项目的pom.xml文件,按照这个格式把自己的pom文件改改。这么说吧,我翻遍了个各大论坛,也没找到一个完全可以直接拿来用的。这是我试了三天三夜试出来的,拿走不谢,要是通不过你来削我。
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.13126851612</groupId>
<artifactId>easy-socket</artifactId>
<version>1.0.2-RELEASE</version>
<packaging>jar</packaging>
<url>https://gitee.com/xiaoyudeguang/springboot-socket</url>
<name>easy-socket</name>
<description>An good tool named easy-socket for developers when socket meet spring.</description>
<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>
<name>xiaoyudeguang</name>
<email>[email protected]</email>
</developer>
</developers>
<scm>
<connection>scm:git:[email protected]:13126851612/easy-socket.git</connection>
<developerConnection>scm:git:[email protected]:13126851612/easy-socket.git</developerConnection>
<url>[email protected]:13126851612/easy-socket.git</url>
</scm>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/>
</parent>
<distributionManagement>
<snapshotRepository>
<id>oss</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>oss</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
<profiles>
<profile>
<id>release</id>
</profile>
</profiles>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.smartboot.socket</groupId>
<artifactId>aio-core</artifactId>
<version>1.3.11</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<failOnError>false</failOnError>
<doclint>none</doclint>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.7</version>
<extensions>true</extensions>
<configuration>
<serverId>oss</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
</plugins>
</build>
</project>
5. 修改run运行方式为发布到Maven中心仓库
选中项目,右键选择run as,打开maven build面板,配置如下(红色划掉的地方需要写入你的gpg.passphrase):
是不是一脸懵逼,gpg.passphrase是个啥?你想想,你的项目发布到maven了,被人恶意篡改了怎么办?怎么证明你的项目是你的项目呢?sonatype官方也替你着急啊。怎么办呢?你在本地生成一份证书,发布到指定的网站去,然后,你发布项目的时候,带上你的证书。。。所以,你需要一个证书咯?所以呢,我们接下来是要搞一个证书出来了。之所以不写在前边,是照顾已经有证书的人,直接run就可以开始奔放了。没生成证书的人先拉到博客最后(8.生成GPG公钥和秘钥),生成证书后再回来接着往下看。
6.等待结果
如果你已经参考其他博客生成证书了,这个时候,你就可以去通知你下审核官了,像这样:
直接在下面回复就好了。然后十分钟左右会收到一个回复:
大意就是,你的项目已经发布好了 ,但是呢,推送到maven中央仓库需要点时间,大概两个小时,要不你等等先?
7.查看结果
两个小时后,打开maven中心仓库官网https://search.maven.org,长这个样子:
比如搜索我的项目“easy-socket”,出来这个,就成功了。
那么,怎么在项目中使用呢?点击last version下的版本号,进入详情界面:
接下来的事情,你懂的。这个界面不需要等两个小时就可以进来看了。
8. 生成GPG公钥和秘钥
windows系统下去gpg4win官网https://www.gpg4win.org/download.html下载软件后安装。
然后在在cmd命令行中执行
gpg --gen-key
命令生成生成公钥和秘钥,过程中需要填写名字、邮箱等,最好填写真实信息。其他步骤可以使用默认值,中间会出来一个弹窗让输入Passphase的值,这就是gpg.passphase需要的值了,一定要记住,别瞎填。
最后会出来这么三行文字。pub行被划掉的就是公钥,需要上传到 sonatype指定的网站去。私钥你自己留着就可以。在cmd中执行下面的命令(不管你看了多少篇其他博客,记住,ubuntu是最容易成功的那个):
> gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys 你的公钥
gpg: sending key 你的公钥 to hkp server keyserver.ubuntu.com
做完这步操作,你就可以返回前边点run了。