Welcome to my GitHub

https://github.com/zq2599/blog_demos

Content: All original articles are categorized and summarized and supporting source code, involving Java, Docker, Kubernetes, DevOPS, etc.;

About maven central repository

  • As a java programmer, I am very familiar with the maven central repository <font color="blue"> https://mvnrepository.com/ </font>, after all, most of the jars that our applications depend on come from here If you want to host the java library you developed on it, so that everyone can use your jar as easily and simply as using Jackson and Spring, please follow this article;
  • Let's take a look at the effect first. The following picture is the search result of the java library I released in the central warehouse:

在这里插入图片描述

Preconditions

  • Since the official sonatype will ask you to create a warehouse on github (the warehouse name is specified by the official sonatype to verify whether you have the permission to operate github), so please make sure you have a github account and can create a warehouse

Overview of this article

  • To sort it out, follow the steps below to publish your own java library to the maven central repository:

在这里插入图片描述

  • At the end of the article, I will also summarize the pits I have stepped on, hoping to help readers avoid them in advance
  • It looks a little complicated, but it's actually very simple, let's get started

Ready to work

  • First of all, please prepare your java project. I use a very common maven project named <font color="red">opencv-linux</font>, github repository address <font color="blue"> https://github.com/zq2599/opencv-client </font>
  • The software information involved in this time is as follows:
  • Operating system: macOS Monterey (12.0.1)
  • JDK:1.8.0_312
  • Maven:3.8.3

1. Register an account

在这里插入图片描述

2. Create an issue

  • Click <font color="red">New</font> in the red box above to start creating an issue, as shown below, select <font color="blue">Community Support</font> for the project, and the issue type is <font color ="blue">New Project</font>:

在这里插入图片描述

  • Next, fill in the project-related information, please note that <font color="blue">Project URL</font> is the github repository address corresponding to your own project:

在这里插入图片描述

  • Wait a few minutes after submitting, and you will receive an email in the email address you filled in when registering your account, asking you to create a repository to prove that the github account you submitted earlier belongs to you:

在这里插入图片描述

  • The above content can also be seen on the newly created issue page, as shown below, that is, sonatype's comment:

在这里插入图片描述

3. Create the repository specified by sonatype

在这里插入图片描述

4. Reply on the issue

  • Open issuse and add a comment, as shown below:

在这里插入图片描述

  • After a while (more than ten minutes on my side), you will receive a new comment, informing you that you can go to release, and giving you the release address of snapshot and release:

在这里插入图片描述

5. Install GPG

  • In the following operations, when the jar is published to the central warehouse, the uploaded data must be signed with the GPG tool, so the GPG key must be prepared next.
  • Install the GPG software first and open the website: https://www.gnupg.org/download/
  • Download the installation file, please select the one suitable for your operating system, my choice is as shown in the red box:

在这里插入图片描述

  • Install GPG

6. Generate the secret key and upload it

  • After the installation is complete, execute <font color="blue">gpg2 --gen-key</font> on the console to create a secret key
  • Enter the account, email, password, etc. according to the prompts:
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
  • After the operation is completed, the following information is obtained:
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]
  • As shown above, the pub key is obtained equal to <font color="red">561AEE4EA92EE3E4C389941811027E9876543210</font>
  • Execute the following command to synchronize the key to the cloud. Pay attention to the keyserver. You can find many on the Internet. In the actual operation of the individual, the following one can be successful:
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys 561AEE4EA92EE3E4C389941811027E9876543210

7. maven global configuration

  • Imagine writing the account and password of sonatype in the project's pom.xml, and uploading it to github for everyone to see? I believe you are not willing to do this, so it is safer to put it in the global configuration of maven, after all, it is saved on your own computer
  • Open the maven configuration file <font color="red">settings.xml</font>, add a server node under the servers, this is the configuration of the account password, which corresponds to <font color="blue"> https: //issues.sonatype.org </font> account password:
<server>
    <id>ossrh</id>
    <username>zq2599</username>
    <password>12345678</password>
</server>
  • Add a <font color="red">profile</font> node under <font color="blue">profiles</font>, the content of <font color="blue">gpg.passphrase</font> is The password you entered when you just created the gpg key:
<profile>
    <id>gpg</id>
    <properties>
    <!-- 由于我的电脑装的gpg2,所以需要指定执行gpg2,否则会报错 -->
    <gpg.executable>gpg2</gpg.executable>
        <gpg.passphrase>abcdefgh</gpg.passphrase>
    </properties>
</profile>
  • The global configuration involving account passwords is completed. Next, open your java project and modify the configuration of pom.xml.

8. maven project configuration

  • First of all, it is necessary to find out where the address of the release warehouse is. The official guidance is as follows, and the warehouse addresses of snapshot and release are given:

在这里插入图片描述

  • The following is the pom.xml file of the java project. There are Chinese comments in the places that need to be paid attention to. <font color="red"> Please do not miss the comment with the serial number </font>, these are the most critical configurations , there are 11 such annotations:
<?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. Compile, build, upload

  • Compiling, building and uploading is actually very simple. The following command is done (enter the directory where pom.xml is located and execute the following command):
mvn clean javadoc:jar deploy -P release
  • During the build, a window will pop up for you to enter a password, please enter the password you set when creating the GPG key:

在这里插入图片描述

  • After the build and upload are successful, the console output is as follows (selected):
...
[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
  • Remember the <font color="blue">iogithubzq2599-1008</font> shown in the above information, this is the ID corresponding to this upload operation in the warehouse

10. Log in to the specified warehouse URL

  • Next, log in to the Nexus website, the specific URL <font color="red"> must read the comment of the issue </font>, as shown in the red box below, what I want to log in here is: <font color="blue"> https://s01.oss.sonatype.org </font>

在这里插入图片描述

  • Click the red box in the upper right corner of the figure below to log in. The account password is the one registered at <font color="blue"> https://issues.sonatype.org </font>:

在这里插入图片描述

11. Post

  • After successful login, click <font color="blue">Staging Repositories</font> in the red box below:

在这里插入图片描述

  • As shown in the figure below, find the appropriate record (I am here <font color="blue">iogithubzq2599-1008</font>), click <font color="blue">Release</font> in the red box 3 to publish, If the status is not closed, expand the Activity at the bottom to see what happened:

在这里插入图片描述

  • The operation is successful, as shown in the following figure:

在这里插入图片描述

12. The issue receives a comment and prompts the completion time

在这里插入图片描述

  • There is an article on the Internet that mentions that the first release needs to be commented on issuse to trigger the synchronization operation. I have not encountered it here (I was planning to comment, but found that the synchronization has already started)

13. Sync to within 30 minutes https://repo1.maven.org

在这里插入图片描述

14. Sync to within four hours https://search.maven.org

在这里插入图片描述

15. Sync to within 24 hours https://mvnrepository.com/

  • The time of synchronization to https://mvnrepository.com is not exactly twenty-four hours, but I can search my own library on this website after twenty-four or so:

在这里插入图片描述

  • At this point, your own java library has been successfully released to the maven central repository, and you can use this library like Jackson and Spring libraries. The usage is to add this dependency:
<dependency>
    <groupId>io.github.zq2599</groupId>
    <artifactId>opencv-linux</artifactId>
    <version>0.0.3</version>
</dependency>

Stepping on the pit record

  • Now all the operations have been completed. Looking back, the whole process is actually relatively smooth. There are only three small pits that need your attention:
  • When synchronizing the gpg key to the cloud, there is an article on the Internet that mentions the use of <font color="red">hkp://subkeys.pgp.net</font>, I keep reporting an error when I use this address, change it to Uploaded successfully after <font color="blue">hkp://keyserver.ubuntu.com:11371</font>
  • In the pom.xml file of the maven project, there must be a url node, as shown below, otherwise an error will be reported when synchronizing to the cloud <font color="blue">Project url missing</font>

在这里插入图片描述

  1. The publishing operation is carried out on the web page. Some articles on the Internet mention that the website is <font color="blue"> https://oss.sonatype.org </font>. I tried to log in, but unfortunately it always failed to log in. In the end, I found the following red box in the comment of the issue. The website to log in is <font color="blue"> https://s01.oss.sonatype.org </font >

在这里插入图片描述

  • At this point, all operations have been completed. If you are publishing your own java library to the maven central repository, I hope this article can give you some reference

Welcome to the public number: Programmer Xin Chen

Search "Programmer Xinchen" on WeChat, I am Xinchen, I look forward to traveling the Java world with you...
https://github.com/zq2599/blog_demos

程序员欣宸
147 声望24 粉丝

热爱Java和Docker