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

Posted by jakeklem on Wed, 19 Jan 2022 16:12:02 +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, I am familiar with maven central warehouse < font color = "blue" > https://mvnrepository.com/ < / font > naturally, we are 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 take a look at the results. The following figure shows the search results of the java library published by me 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 < font color = "red" > opencv Linux < / font >, github warehouse address < font color = "blue" > https://github.com/zq2599/ope...</font>
  • The software information involved in this is as follows:
  • Operating system: macOS Monterey(12.0.1)
  • JDK: 1.8.0_312
  • Maven: 3.8.3

1. Registered account

2. Create an issue

  • Click < font color = "red" > New < / font > in the red box in the figure above to start creating an issue. As shown in the figure below, select < font color = "blue" > community support < / font >, and the problem 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 warehouse address corresponding to your own project:

  • Wait for 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 before 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

  • Log in to your github and create a warehouse as required. What I want to create here is < font color = "blue" > https://github.com/zq2599/OSS...</font>
  • Just create it honestly:

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 the secret key and upload it

  • After installation, execute < font color = "blue" > gpg2 -- Gen key < / font > on the console to start creating the secret key
  • Enter account, email, 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 < font color = "red" > 561aee4ea92ee3e4c389941811027e9876543210 < / font >
  • 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 < font color = "red" > settings XML < / font >, add a server node under servers, which is the configuration of account and password, corresponding 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 >, < font color = "blue" > gpg The content of passphrase < / font > is the password entered when creating the gpg secret key just now:
<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 XML file, there are Chinese comments on key points to be paid attention to, < font color = "red" > please do not omit the comment with serial number < / font >, which 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>
                        </execution>
                    </executions>
                    <configuration>
                        <excludeResources>true</excludeResources>
                        <useDefaultExcludes>true</useDefaultExcludes>
                    </configuration>
                </plugin>

                <!-- 8. generate doc Plug ins for documents -->
                <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. Compile build maven Project plug-in -->
                <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. Determine which plug-ins to use -->
        <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. Generate a signature and confirm which one to use gpg Secret key -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-gpg-plugin</artifactId>
                        <version>1.5</version>
                        <executions>
                            <execution>
                                <!-- Must be the same as in the configuration gpg check id agreement -->
                                <id>gpg</id>
                                <phase>verify</phase>
                                <goals>
                                    <goal>sign</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

</project>

9. Compile, build and upload

  • Compiling, building and uploading are actually very simple. The following line of command is completed (enter the directory where pom.xml is located and execute the following command):
mvn clean javadoc:jar deploy -P release
  • A pop-up window will pop up during construction for you to enter the password. Please enter the password set when creating the GPG secret key:

  • After the build and upload are successful, the console output is as follows (cut and select):
...
[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 > displayed in the above information, which is the ID corresponding to the upload operation in the warehouse

10. Log in to the designated warehouse website

  • Next, log in to Nexus website. The specific website address < font color = "red" > must see the comment of 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, and the account password is < font color = "blue" > https://issues.sonatype.org < / font > registered:

11. Release

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

  • As shown in the following figure, find the appropriate record (I'm < font color = "blue" > iogithubzq2599-1008 < / font >), and click < font color = "blue" > release < / font > in red box 3 to publish. If the status is not closed, expand the Activity at the bottom to see what happens:

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

12. issue receives a comment indicating the completion time

  • Wait for more than ten minutes and receive a comment on the issue, indicating that the synchronization operation has been activated. Synchronize to within 30 minutes https://repo1.maven.org/ , sync to within four hours https://search.maven.org:

  • An article on the Internet mentioned that the synchronization operation will be triggered only after commenting on issuse for the first time. I didn't encounter it here (I was going to comment and found that synchronization has started)

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

  • Wait 30 minutes and you can go to the website https://repo1.maven.org See the uploaded project related files on the, as shown below:

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

  • Wait four hours and you can go to the website https://search.maven.org See the uploaded project related files on the, as shown below:

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

  • Sync to https://mvnrepository.com The time is not exactly 24 hours, but I can search my library on this website at an interval of about 24 hours:

  • So far, your java library has been successfully published to the maven central warehouse. 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>

Pit record

  • Now that all operations have been completed, the whole process is relatively smooth. Only three small pits have been encountered, which need your attention:
  • When synchronizing the gpg secret key to the cloud, an online article mentioned using < font color = "red" > hkp://subkeys.pgp.net < / font >, I have been reporting errors when using this address. Change it to < font color = "blue" > hkp://keyserver.ubuntu.com:11371 < / font > upload succeeded
  • POM of maven project There must be a url node in the XML file, as shown in the figure 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 >, at first, I also opened the page and tried to log in, but it was a pity that I always failed to log in. Finally, I found the red box in the comment of issue, as shown in the figure below. The website to log in is < font color = "blue" > https://s01.oss.sonatype.org</font>

  • So far, all operations have been completed. If you are publishing your java library to maven central warehouse, I hope this article can give you some reference

Welcome to the official account: programmer Xin Chen

Wechat search "programmer Xinchen". I'm Xinchen. I look forward to traveling with you in the Java World
https://github.com/zq2599/blog_demos

Topics: cloud computing