欢送拜访我的GitHub

https://github.com/zq2599/blog_demos

内容:所有原创文章分类汇总及配套源码,波及Java、Docker、Kubernetes、DevOPS等;

对于maven地方仓库

  • 作为一个java程序员,对maven地方仓库<font color="blue">https://mvnrepository.com/</font>天然是十分相熟的,毕竟咱们的利用依赖的jar大部分都来自此处,如果您想把本人开发的java库也托管在下面,让大家像应用Jackson、Spring那样轻松简略的应用您的jar,就请随本文一起操作吧;
  • 先看看成果,下图是我公布的java库在地方仓库的搜寻后果:

前提条件

  • 因为sonatype官网会要求您在github创立一个仓库(仓库名由sonatype官网指定,以此验证您是否有github操作权限),因而请确保您本人有github账号,并且能够创立仓库

本篇概览

  • 梳理一下,依照以下步骤将本人的java库公布到maven地方仓库:

  • 文末还会对已经踩过的小坑做了总结,心愿能帮忙读者们提前避开
  • 看起来略有些繁琐,但其实很简略,接下来开始吧

筹备工作

  • 首先请把您的java工程筹备好,我用的是一个十分一般的maven工程,名为<font color="red">opencv-linux</font>,github仓库地址<font color="blue">https://github.com/zq2599/ope...</font>
  • 本次波及的软件信息如下:
  • 操作系统:macOS Monterey(12.0.1)
  • JDK:1.8.0_312
  • Maven:3.8.3

1. 注册帐号

  • 关上网站https://issues.sonatype.org,注册一个帐号,登录后成果如下图:

2. 创立问题(issue)

  • 点击上图红框中的<font color="red">新建</font>开始创立issue,如下图,项目选择<font color="blue">Community Support</font>,问题类型是<font color="blue">New Project</font>:

  • 接下来填写我的项目相干的信息,请留神<font color="blue">Project URL</font>外面是您本人的工程对应的github仓库地址:

  • 提交后期待几分钟,注册账号时填写的邮箱会收到一封邮件,要求你创立一个仓库,以此来证实之前提交的github帐号属于你本人:

  • 上述内容在方才新建的issue页面也能看到,如下图,即sonatype的评论:

3. 创立sonatype指定的仓库

  • 登录您的github,依照要求创立仓库,我这里要创立的是<font color="blue">https://github.com/zq2599/OSS...</font>
  • 老老实实的创立进去就行了:

4. 在issue上进行回复

  • 关上issuse,减少一个评论,如下图:

  • 不久后(我这边是十多分钟),就会收到一条新的评论,告诉你能够去做公布了,并给你了snapshot和release的公布地址:

5. 装置GPG

  • 前面的操作中,在将jar公布到地方仓库时,要用GPG工具对上传的数据进行签名,因而接下来要筹备好GPG秘钥
  • 先装置GPG软件,关上网站:https://www.gnupg.org/download/
  • 下载安装文件,请抉择适宜您的操作系统的,我的抉择如下图红框:

  • 装置GPG

6. 生成秘钥并上传

  • 装置实现后,在控制台执行<font color="blue">gpg2 --gen-key</font>开始创立秘钥
  • 依据提醒输出账号、邮箱、明码等:
GnuPG needs to construct a user ID to identify your key.Real name: zq2599Email address: zq2599@gmail.comYou selected this USER-ID:    "zq2599 <zq2599@gmail.com>"Change (N)ame, (E)mail, or (O)kay/(Q)uit? O
  • 操作实现后失去如下信息:
gpg: key 11027EJIHGFEDCBA marked as ultimately trustedgpg: directory '/Users/will/.gnupg/openpgp-revocs.d' createdgpg: revocation certificate stored as '/Users/will/.gnupg/openpgp-revocs.d/561AEE4EA92EE3E4C389941811027E9876543210.rev'public and secret key created and signed.pub   rsa3072 2021-11-10 [SC] [expires: 2023-11-10]      561AEE4EA92EE3E4C389941811027E9876543210uid                      zq2599 <zq2599@gmail.com>sub   rsa3072 2021-11-10 [E] [expires: 2023-11-10]
  • 如上所示,失去了pub key等于<font color="red">561AEE4EA92EE3E4C389941811027E9876543210</font>
  • 执行以下命令,将秘钥同步到云端,留神keyserver,网上能够搜到很多个,集体实际操作中,上面这个是能够胜利的:
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys 561AEE4EA92EE3E4C389941811027E9876543210

7. maven全局配置

  • 试想把sonatype的账号密码写在我的项目的pom.xml中,再上传到github让所有人都看到?置信您肯定不违心这样,所以还是放在maven的全局配置中比拟平安,毕竟是保留在本人的电脑上
  • 关上maven的配置文件<font color="red">settings.xml</font>,在servers上面减少一个server节点,这是个账号密码的配置,对应的是<font color="blue">https://issues.sonatype.org</font>的账号密码:
<server>    <id>ossrh</id>    <username>zq2599</username>    <password>12345678</password></server>
  • 在<font color="blue">profiles</font>下减少一个<font color="red">profile</font>节点,<font color="blue">gpg.passphrase</font>的内容是方才创立gpg秘钥时输出的明码:
<profile>    <id>gpg</id>    <properties>    <!-- 因为我的电脑装的gpg2,所以须要指定执行gpg2,否则会报错 -->    <gpg.executable>gpg2</gpg.executable>        <gpg.passphrase>abcdefgh</gpg.passphrase>    </properties></profile>
  • 波及到账号密码的全局配置就实现了,接下来关上您的java工程,咱们去批改pom.xml的配置

8. maven我的项目配置

  • 首先要搞清楚公布仓库的地址在哪,官网领导如下,给出了snapshot和release的仓库地址:

  • 以下是java工程的pom.xml文件,需重点关注的中央都有中文正文,<font color="red">请不要漏掉带序号的正文</font>,这些都是最要害的配置,这种正文共11个:
<?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>    <groupId>io.github.zq2599</groupId>    <artifactId>opencv-linux</artifactId>    <version>0.0.3</version>    <name>opencv-linux</name>    <description>opencv-linux</description>    <!-- 1. url必须要有,不然近程提交时会返回谬误 -->    <url>https://github.com/zq2599/opencv-client</url>    <properties>        <java.version>1.8</java.version>    </properties>    <packaging>jar</packaging>    <!-- 2. 开源证书 -->    <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>    <!-- 3. 源码仓库信息 -->    <scm>        <connection>scm:git:git@github.com:zq2599/opencv-client.git</connection>        <developerConnection>scm:git:git@github.com:zq2599/opencv-client.git</developerConnection>        <url>https://github.com/zq2599/opencv-client/tree/main</url>    </scm>    <!-- 4. 开发人员信息 -->    <developers>        <developer>            <name>zq2599</name>            <email>zq2599@gmail.com</email>            <organization>https://github.com/zq2599</organization>            <timezone>+8</timezone>        </developer>    </developers>    <!-- 5. 上传的仓库地址,以及应用哪个账号密码配置 -->    <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>    <dependencies>        <dependency>            <groupId>org.projectlombok</groupId>            <artifactId>lombok</artifactId>            <version>1.16.18</version>        </dependency>    </dependencies>    <build>        <!-- 配置好每个插件的属性 -->        <pluginManagement>            <plugins>                <!-- 6. 上传到sonatype的插件 -->                <plugin>                    <groupId>org.sonatype.plugins</groupId>                    <artifactId>nexus-staging-maven-plugin</artifactId>                    <version>1.6.7</version>                    <extensions>true</extensions>                    <configuration>                        <!-- 这里的id必须要和全局配置中的server统一 -->                        <serverId>ossrh</serverId>                        <!-- 这个地址,肯定要和issue的评论中给出的地址统一! -->                        <nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>                        <!-- 如果心愿公布后主动执行close和release操作,此处能够调整为true -->                        <autoReleaseAfterClose>false</autoReleaseAfterClose>                    </configuration>                </plugin>                <!-- 7. 上传源码的插件 -->                <plugin>                    <groupId>org.apache.maven.plugins</groupId>                    <artifactId>maven-source-plugin</artifactId>                    <version>3.1.0</version>                    <inherited>true</inherited>                    <executions>                        <execution>                            <id>attach-sources</id>                            <goals>                                <goal>jar</goal>                            </goals>                        </execution>                    </executions>                    <configuration>                        <excludeResources>true</excludeResources>                        <useDefaultExcludes>true</useDefaultExcludes>                    </configuration>                </plugin>                <!-- 8. 生成doc文档的插件 -->                <plugin>                    <groupId>org.apache.maven.plugins</groupId>                    <artifactId>maven-javadoc-plugin</artifactId>                    <version>3.0.0</version>                    <inherited>true</inherited>                    <executions>                        <execution>                            <id>bundle-sources</id>                            <phase>package</phase>                            <goals>                                <goal>jar</goal>                            </goals>                        </execution>                    </executions>                    <configuration>                        <maxmemory>1024</maxmemory>                        <encoding>UTF-8</encoding>                        <show>protected</show>                        <notree>true</notree>                        <!-- Avoid running into Java 8's very restrictive doclint issues -->                        <failOnError>false</failOnError>                        <doclint>none</doclint>                    </configuration>                </plugin>                <!-- 9. 编译构建maven工程的插件 -->                <plugin>                    <groupId>org.apache.maven.plugins</groupId>                    <artifactId>maven-compiler-plugin</artifactId>                    <configuration>                        <source>1.8</source>                        <target>1.8</target>                        <encoding>UTF-8</encoding>                    </configuration>                </plugin>            </plugins>        </pluginManagement>        <!-- 10. 确定要应用哪些插件 -->        <plugins>            <plugin>                <groupId>org.sonatype.plugins</groupId>                <artifactId>nexus-staging-maven-plugin</artifactId>            </plugin>                        <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-source-plugin</artifactId>            </plugin>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>            </plugin>        </plugins>    </build>    <profiles>        <profile>            <id>release</id>            <build>                <plugins>                    <!-- 11. 生成签名,确定应用那个gpg秘钥 -->                    <plugin>                        <groupId>org.apache.maven.plugins</groupId>                        <artifactId>maven-gpg-plugin</artifactId>                        <version>1.5</version>                        <executions>                            <execution>                                <!-- 必须和配置中的gpg校验id统一 -->                                <id>gpg</id>                                <phase>verify</phase>                                <goals>                                    <goal>sign</goal>                                </goals>                            </execution>                        </executions>                    </plugin>                </plugins>            </build>        </profile>    </profiles></project>

9. 编译、构建、上传

  • 编译构建上传其实很简略,上面一行命令搞定(进入pom.xml所在目录下执行以下命令):
mvn clean javadoc:jar deploy -P release
  • 构建期间会弹出窗口让你输出明码,请输出创立GPG秘钥时设置的明码:

  • 构建和上传胜利后,控制台输入如下(截选):
...[INFO] Installing /Users/zhaoqin/github/blog_demos/opencv-linux/target/opencv-linux-0.0.3-sources.jar.asc to /Users/zhaoqin/github/blog_demos/opencv-linux/target/nexus-staging/staging/543da2cd9af848/io/github/zq2599/opencv-linux/0.0.3/opencv-linux-0.0.3-sources.jar.asc[INFO] Performing remote staging...[INFO] [INFO]  * Remote staging into staging profile ID "543da2cd9abc12"[INFO]  * Created staging repository with ID "iogithubzq2599-1008".[INFO]  * Staging repository at https://s01.oss.sonatype.org:443/service/local/staging/deployByRepositoryId/iogithubzq2599-1008[INFO]  * Uploading locally staged artifacts to profile io.github.zq2599Uploading to ossrh: ...https://s01.oss.sonatype.org:443/service/local/staging/deployByRepositoryId/iogithubzq2599-1008/io/github/zq2599/opencv-linux/0.0.3/opencv-linux-0.0.3-sources.jar.asc (659 B at 1.2 kB/s)[INFO]  * Upload of locally staged artifacts finished.[INFO]  * Closing staging repository with ID "iogithubzq2599-1008".Waiting for operation to complete......[INFO] Remote staged 1 repositories, finished with success.[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time:  27.199 s[INFO] Finished at: 2021-11-12T08:08:37+08:00
  • 记住上述信息中展现的<font color="blue">iogithubzq2599-1008</font>,这是本次上传操作在仓库中对应的ID

10. 登录指定的仓库网址

  • 接下来登录Nexus网站,具体的网址<font color="red">肯定要看issue的评论</font>,如下图红框,我这里要登录的是:<font color="blue">https://s01.oss.sonatype.org</font>

  • 点击下图右上角红框登录,账号密码是在<font color="blue">https://issues.sonatype.org</font>注册的那个:

11. 公布

  • 登录胜利后,点击下图红框中的<font color="blue">Staging Repositories</font>:

  • 如下图,找到适合的记录(我这里是<font color="blue">iogithubzq2599-1008</font>),点击红框3中的<font color="blue">Release</font>进行公布,如果状态不是closed,就要开展底部的Activity查看产生了什么异样:

  • 操作胜利,如下图所示:

12. issue收到评论,提醒实现工夫

  • 静候十多分钟,issue上收到一条评论,提醒同步操作已激活,三十分钟内同步到https://repo1.maven.org/,四小时内同步到https://search.maven.org:

  • 网上有文章提到第一次公布要在issuse上评论,才会触发同步操作,我这里没有遇到(正打算评论呢,发现曾经开始同步了)

13. 三十分钟内同步到https://repo1.maven.org

  • 静候三十分钟,能够在网站https://repo1.maven.org上看到上传的工程相干文件了,如下图:

14. 四小时内同步到https://search.maven.org

  • 静候四小时,能够在网站https://search.maven.org上看到上传的工程相干文件了,如下图:

15. 二十四小时内同步到https://mvnrepository.com/

  • 同步到https://mvnrepository.com的工夫并不是准确的二十四小时,而是我时隔二十四左右在此网站上能够搜寻到本人的库:

  • 至此,本人的java库已胜利公布到maven地方仓库,能够像Jackson、Spring库那样应用这个库了,用法就是增加这个依赖:
<dependency>    <groupId>io.github.zq2599</groupId>    <artifactId>opencv-linux</artifactId>    <version>0.0.3</version></dependency>

踩坑记录

  • 当初已实现了所有的操作,回顾一下,整个过程其实绝对比较顺利,只遇到了三个小坑须要您留神:
  • 同步gpg秘钥到云端的时候,网上有文章提到用<font color="red">hkp://subkeys.pgp.net</font>,我在应用该地址的时候始终在报错,改为<font color="blue">hkp://keyserver.ubuntu.com:11371</font>之后上传胜利
  • maven工程的pom.xml文件中,肯定要有url节点,如下图,否则会在同步到云端的时候报错<font color="blue">Project url missing</font>

  1. 公布的操作是在网页上进行的,网上有的文章提到网站是<font color="blue">https://oss.sonatype.org</font>,最后我也关上了该网页并尝试登录,惋惜始终登录失败,最终,在issue的评论上发现如下图红框,要登录的网站是<font color="blue">https://s01.oss.sonatype.org</font>

  • 至此,所有操作都已实现,如果您正在将本人的java库公布到maven地方仓库,心愿本文能给您一些参考

欢送关注公众号:程序员欣宸

微信搜寻「程序员欣宸」,我是欣宸,期待与您一起畅游Java世界...
https://github.com/zq2599/blog_demos