自建工具包推送maven公共仓,使其可以直接maven pom 从http://repo1.maven.org/maven2仓库拉取,推送后阿里云公共仓也可以同步拉取到。
一、登录网站
地址:https://central.sonatype.com/,注册登录,可以使用google账号快捷登录
二、Namespace创建以及校验
1、点击如下菜单

2、点击如下按钮新建namespace

会弹出

1、namespace的名称分情况,如果是github
输入io.github.你的github名称 ,然后点击确定,列表会增加一条数据,如下

然后登录你的github 创建一个仓库名为图示Verification Key后面的字符串的public仓库,然后再点击如图的verify namespace的按钮,不出意外,刷新下会变成verified,就表示namespace可以用了**。**
2、如果你自己的域名
那么添加namespace时可以填你自己的域名,但是填入的为反DNS格式(别急下面会说明),在域名解析那里解析为TXT类型,记录值填图示Verification Key后面的字符串,例如,你域名解析如下

那么namespace填入的就是com.tfgee.mv(这就是反dns),同样确定后在列表里面点击verify namespace的按钮,然后刷新下列表,不出意外就可以了
三、发布component准备工作
1、Pom文件增加
1.1、增加profiles 用于build代码
XML
<!--to maven central start -->
<profiles>
<profile>
<id>central-publishing</id>
<build>
<plugins>
<plugin>
<groupId>org.sonatype.central</groupId>
<artifactId>central-publishing-maven-plugin</artifactId>
<version>0.9.0</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<url>https://github.com/486-LM/tbtools.git</url>
<licenses>
<license>
<name>The Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
<developers>
<developer>
<name>tb</name>
<email>developer@example.com</email>
</developer>
</developers>
<scm>
<connection>scm:git:git://github.com/486-LM/tbtools.git</connection>
<developerConnection>scm:git:ssh://github.com/486-LM/tbtools.git</developerConnection>
<url>https://github.com/486-LM/tbtools.git</url>
</scm>
<pluginRepositories>
<pluginRepository>
<id>sonatype-releases</id>
<url>https://oss.sonatype.org/content/repositories/releases/</url>
</pluginRepository>
</pluginRepositories>
<!--to maven central end -->
1.2 增加编译sources和javadoc以及gpg签名插件
XML
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.3.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- 解决控制台乱码 -->
<additionalJOptions>
-Xdoclint:none -J-Dfile.encoding=utf-8
</additionalJOptions>
<charset>UTF-8</charset>
<encoding>UTF-8</encoding>
<docencoding>UTF-8</docencoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
2、Maven setting文件
2.1 在<repositories>增加maven中央仓地址
XML
<repository>
<id>central</id>
<url>http://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
2.2 增加servers配置
XML
<servers>
<server>
<id>central</id>
<username>xxxx</username>
<password>xxxxxxx</password>
</server>
</servers>
这里的name和password需要去之前的sonatype网站获取,如下

点击tokens菜单
然后点击generate user token,就能得到

3、安装gpg软件
上传需要进行签名,要用到gpg插件,这里需要安装
3.1 下载及证书秘钥生成
- 下载地址https://www.gpg4win.org/get-gpg4win.html,选择$0的就行,不需要钱,然后正常的安装。
- 安装好之后,打开软件,文件->新建秘钥对,名称跟namespace一致就行,电子邮件sonatype登录邮件地址就行,秘钥材料选择rsa2048就行。
- 点击设置->点击配置kleopatra,图示处输入hkps://keys.openpgp.org

3.2 证书公钥id上传密钥服务器
1、打开命令窗口,执行gpg --list-keys,查看密钥列表
Pub公钥id类似8D235EC2E48F697CA78C9A4B107B819EC3CDC3FC
2、执行:gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys 8D235EC2E48F697CA78C9A4B107B819EC3CDC3FC进行上传
3、执行gpg --keyserver keyserver.ubuntu.com --recv-keys 8D235EC2E48F697CA78C9A4B107B819EC3CDC3FC查看是否上传成功,返回如下既是成功了

四、构建及发布代码
1、构建推送sonatype
注意:
- groupId需要是之前添加的namespace名称
- 每个pom必须要有name,跟artifactId一致就行
Maven 运行clean deploy -P central-publishing,等待执行完成后,显示如下图便是成功了

2、到Sonatype网站上push
到sonatype网站,点击

就可以看到你推送的component,就可以点击publish进行发布,注意这里发布之后不可删除,请慎重,(想想也是,别人今天引用了,明天就发现没有,直接炸裂)

五、自此就完成了推送maven公共仓的操作了。
可以在pom里面配置dependency,从http://repo1.maven.org/maven2仓库拉取了,当然也可以从阿里云仓拉取