我把自己的java库发布到了maven中央仓库,从此可以像Jackson、Spring的jar一样使用它了

欢迎访问我的GitHub

https://github.com/zq2599/blog_demos

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

关于maven中央仓库

前提条件

本篇概览

准备工作

  1. 操作系统:macOS Monterey(12.0.1)
  2. JDK:1.8.0_312
  3. Maven:3.8.3

1. 注册帐号

2. 创建问题(issue)

3. 创建sonatype指定的仓库

4. 在issue上进行回复

5. 安装GPG

6. 生成秘钥并上传

GnuPG needs to construct a user ID to identify your key.

Real name: zq2599
Email address: zq2599@gmail.com
You 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 trusted
gpg: directory '/Users/will/.gnupg/openpgp-revocs.d' created
gpg: 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]
      561AEE4EA92EE3E4C389941811027E9876543210
uid                      zq2599 <zq2599@gmail.com>
sub   rsa3072 2021-11-10 [E] [expires: 2023-11-10]
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys 561AEE4EA92EE3E4C389941811027E9876543210

7. maven全局配置

<server>
	<id>ossrh</id>
    <username>zq2599</username>
    <password>12345678</password>
</server>
<profile>
	<id>gpg</id>
    <properties>
    <!-- 由于我的电脑装的gpg2,所以需要指定执行gpg2,否则会报错 -->
    <gpg.executable>gpg2</gpg.executable>
        <gpg.passphrase>abcdefgh</gpg.passphrase>
    </properties>
</profile>

8. maven项目配置

<?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. 编译、构建、上传

mvn clean javadoc:jar deploy -P release

...
[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.zq2599
Uploading 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

10. 登录指定的仓库网址

11. 发布

12. issue收到评论,提示完成时间

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

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

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

<dependency>
    <groupId>io.github.zq2599</groupId>
    <artifactId>opencv-linux</artifactId>
    <version>0.0.3</version>
</dependency>

踩坑记录

  1. 同步gpg秘钥到云端的时候,网上有文章提到用hkp://subkeys.pgp.net,我在使用该地址的时候一直在报错,改为hkp://keyserver.ubuntu.com:11371之后上传成功
  2. maven工程的pom.xml文件中,一定要有url节点,如下图,否则会在同步到云端的时候报错Project url missing

  1. 发布的操作是在网页上进行的,网上有的文章提到网站是https://oss.sonatype.org,最初我也打开了该网页并尝试登录,可惜始终登录失败,最终,在issue的评论上发现如下图红框,要登录的网站是https://s01.oss.sonatype.org

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

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