I released my java library to the maven central repository and can use it like Jackson and Spring jar s

Posted by Canadiengland on Mon, 24 Jan 2022 11:17:28 +0100

Welcome to my GitHub

https://github.com/zq2599/blog_demos

Content: classification and summary of all original articles and supporting source code, involving Java, Docker, Kubernetes, DevOPS, etc;

About maven central warehouse

  • As a java programmer, the maven central warehouse https://mvnrepository.com/ Naturally, I am very familiar with it. After all, most of the jars that our applications rely on come from here. If you want to host your own java library on it, so that you can use your jars as easily and simply as Jackson and Spring, please operate with this article;

  • Let's see the effect first. The following figure shows the search results of the java library I published in the central warehouse:

prerequisite

  • Since the official sonatype will ask you to create a warehouse in github (the warehouse name is specified by the official sonatype to verify whether you have github operation permission), please ensure that you have a github account and can create a warehouse

Overview of this article

  • Sort it out and publish your java library to maven central warehouse according to the following steps:

  • At the end of the paper, I will summarize the small pits I have stepped on, hoping to help readers avoid them in advance

  • It seems a little cumbersome, but it's actually very simple. Let's start next

preparation

  • First, please prepare your java project. I use a very common maven project called opencv Linux and github warehouse address https://github.com/zq2599/opencv-client

  • The software information involved in this is as follows:

  1. Operating system: macOS Monterey(12.0.1)
  2. JDK: 1.8.0_312
  3. Maven: 3.8.3

1. Registered account

2. Create an issue

  • Click New in the red box in the figure above to start creating an issue, as shown in the figure below. Select Community Support for the project, and the problem type is New Project:

  • Next, fill in the project related information. Please note that the github warehouse address corresponding to your own project is in the Project URL:

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

  • The above contents can also be seen on the newly created issue page, as shown in the following figure, that is, the comments of sonatype:

3. Create the warehouse specified by sonatype

4. Reply on the issue

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

  • In a short time (more than ten minutes on my side), I will receive a new comment, inform you that you can publish, and give you the release address of snapshot and release:

5. Install GPG

  • In the following operations, when publishing jar s to the central warehouse, you need to use the GPG tool to sign the uploaded data, so you need to prepare the GPG secret key next

  • First install the GPG software and open the website: https://www.gnupg.org/download/

  • To download the installation file, please select one that is suitable for your operating system. My choice is shown in the red box below:

  • Install GPG

6. Generate and upload the secret key

  • After installation, execute gpg2 -- Gen key on the console to start creating the secret key

  • Enter the account number, email address, password, etc. according to the prompt:

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, 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 equal to 561AEE4EA92EE3E4C389941811027E9876543210

  • Execute the following command to synchronize the secret keys to the cloud. Note the keyserver. Many can be found on the Internet. In personal actual operation, the following can be successful:

gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys 561AEE4EA92EE3E4C389941811027E9876543210

7. maven global configuration

  • Imagine writing the sonatype account password in the project POM XML, and then upload it to github for everyone to see? I'm sure you don't want to do this, so it's safer to put it in the global configuration of maven. After all, it's saved on your own computer

  • Open maven's configuration file settings XML, add a server node under servers, which is the configuration of account and password, corresponding to https://issues.sonatype.org Account and password:

<server>
	<id>ossrh</id>
    <username>zq2599</username>
    <password>12345678</password>
</server>
  • Add a profile node under profiles, gpg The content of the passphrase is the password entered when the gpg secret key was just created:
<profile>
	<id>gpg</id>
    <properties>
    <!-- Because my computer installed gpg2,Therefore, you need to specify execution gpg2,Otherwise, an error will be reported -->
    <gpg.executable>gpg2</gpg.executable>
        <gpg.passphrase>abcdefgh</gpg.passphrase>
    </properties>
</profile>
  • The global configuration involving account and password is completed. Next, open your java project and let's modify POM XML configuration

8. maven project configuration

  • First, find out the address of the release warehouse. The official guidance is as follows, giving the warehouse address of snapshot and release:

  • The following is the POM of java project There are Chinese comments on XML files that need to be paid attention to. Please do not omit the comments with serial numbers. These are the most critical configurations. There are 11 such comments in total:
<?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 It must be, or an error will be returned when submitting remotely -->
    <url>https://github.com/zq2599/opencv-client</url>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <packaging>jar</packaging>

    <!-- 2. Open source certificate -->
    <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. Source code warehouse information -->
    <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. Developer Information -->
    <developers>
        <developer>
            <name>zq2599</name>
            <email>zq2599@gmail.com</email>
            <organization>https://github.com/zq2599</organization>
            <timezone>+8</timezone>
        </developer>
    </developers>
    <!-- 5. The uploaded warehouse address, and which account and password to use -->
    <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>
    	<!-- Configure the properties of each plug-in -->
        <pluginManagement>
            <plugins>
            	<!-- 6. Upload to sonatype Plug in for -->
                <plugin>
                    <groupId>org.sonatype.plugins</groupId>
                    <artifactId>nexus-staging-maven-plugin</artifactId>
                    <version>1.6.7</version>
                    <extensions>true</extensions>
                    <configuration>
                        <!-- there id Must be in the global configuration server agreement -->
                        <serverId>ossrh</serverId>
                        <!-- This address must be with issue The address given in your comments is the same! -->
                        <nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
                        <!-- If you want to execute automatically after publishing close and release Operation, which can be adjusted to true -->
                        <autoReleaseAfterClose>false</autoReleaseAfterClose>
                    </configuration>
                </plugin>

				<!-- 7. Upload source code plug-in -->
                <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>