How to submit your Jar package to Maven central warehouse (publish your Jar package to the central warehouse)

This article will introduce how to publish your own jar package to the public central warehouse and reference it through maven

Registered account

Company Registered Address
https://issues.sonatype.org/secure/Signup!default.jspa
Fill in basic information

Here I use gitee for submission. The network is not very good. Even some cards in GitHub have the same logic.
Click create

Fill in relevant information


Instructions for filling in information:

fieldFill in content
SummarySummary (what is the purpose of writing jar package? It can be in Chinese)
DescriptionDescription (describe in detail what the jar package is used for, which can be the same as the Summary, and can be in Chinese)
Group Ide.g. com.gitee.jast (gitee is used as demo here)
Project URLe.g. https://gitee.com/jastee/jast-tool
SCM urle.g. https://gitee.com/jastee/jast-tool.git
ProejctSelect Community Support - Open Source Project Repository Hosting (OSSRH)
Issue TypeSelect New Project

After filling in, click create to create

The status is open after submission

The English version is as follows, which will be introduced in English later

Then wait for the official personnel to review, and then the official personnel reply to let us create an empty project on gitee to verify that the gitee is our own account. (it was very embarrassing during this period. I didn't set the public permission after I created the project. I could access it myself, but he couldn't. I kept asking him. He didn't say why, so he asked me to create an empty project...)

When the status changes to Status:RESOLVED, you can contract

GPG installation

install

brew install gpg
If you can't install it online, you can download it directly (I installed it online) https://gpgtools.org/

Generate key

# Enter the user name, email address and password according to the prompt. Remember to use them later
gpg --gen-key

View generated keys

mac@Mac ~ % gpg --list-keys
gpg: Checking trust database
gpg: marginals needed: 3  completes needed: 1  trust model: pgp
gpg: Depth: 0 validity: 1 signed: 0 Trust: 0-,0q,0n,0m,0f,1u
gpg: The next trust database check will take place in 2023-10-23 conduct
/Users/mac/.gnupg/pubring.kbx
-----------------------------
pub   ed25519 2021-10-23 [SC] [Valid until: 2023-10-23]
      1D3AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAF325
uid             [ absolutely ] jast <745925668@qq.com>
sub   sb11111 2021-10-23 [E] [Valid until: 2023-10-23]

1d3aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Upload public key

mac@Mac ~ % gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys 1D3AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAF325
gpg: Sending key AAAAAAAAAAAAAAAAAAAF325 reach hkp://keyserver.ubuntu.com:11371

View uploaded public key

# The reception may fail due to network reasons. You can view the success by executing it twice more
mac@Mac ~ % gpg --keyserver hkp://keyserver.ubuntu.com:11371 --recv-keys 1D3AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAF325
gpg: Failed to receive from public key server: No data
mac@Mac ~ % gpg --keyserver hkp://keyserver.ubuntu.com:11371 --recv-keys 1D3AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAF325
gpg: secret key D1BC8A428AA3F325: "jast <745925668@qq.com>" Unchanged
gpg: Total processed: 1
gpg:              Unchanged: 1

Maven configuration

Add account information in Maven's setting.xml

  <server> 
    <id>ossrh</id>
    <username>Step 1 registered account</username>
    <password>The first step is to register the password of the account</password>
  </server>

Upload to Maven warehouse

Modify the project configuration and fill in the basic information

pom.xml in Java project is configured as follows

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.gitee.jastee</groupId>
    <artifactId>jast-tool</artifactId>
    <version>0.0.1</version>
    
    <name>jast-tool</name>
    <description>
        Java tool
    </description>
    <url>https://gitee.com/jastee/jast-tool</url>

    <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>

    <issueManagement>
        <system>gitee</system>
        <url>https://gitee.com/jastee/jast-tool/issues</url>
    </issueManagement>

    <scm>
        <connection>scm:git:https://gitee.com/jastee/jast-tool.git</connection>
        <developerConnection>scm:git:https://gitee.com/jastee/jast-tool.git</developerConnection>
        <url>https://gitee.com/jastee/jast-tool</url>
    </scm>

    <developers>
        <developer>
            <name>Jast</name>
            <email>745925668@qq.com</email>
            <url>https://gitee.com/jastee</url>
        </developer>
    </developers>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <distributionManagement>
        <repository>
            <id>ossrh</id>
<!--            <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>-->
            <url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
        </repository>
        <snapshotRepository>
            <id>ossrh</id>
<!--            <url>https://oss.sonatype.org/content/repositories/snapshots</url>-->
            <url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url>
        </snapshotRepository>
    </distributionManagement>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-release-plugin</artifactId>
                    <configuration>
                        <mavenExecutorId>forked-path</mavenExecutorId>
                        <useReleaseProfile>false</useReleaseProfile>
                        <arguments>-Psonatype-oss-release</arguments>
                    </configuration>
                </plugin>

                <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>

                <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>
            </plugins>
        </pluginManagement>

        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <formats>
                        <format>html</format>
                        <format>xml</format>
                    </formats>
                    <check/>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>release</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-gpg-plugin</artifactId>
                        <version>1.6</version>
                        <executions>
                            <execution>
                                <id>sign-artifacts</id>
                                <phase>verify</phase>
                                <goals>
                                    <goal>sign</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

</project>

Execute compile command

During compilation, we need to enter the password of our previous key

> mvn clean deploy -DskipTests=true -P release
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  50.101 s
[INFO] Finished at: 2021-10-24T10:15:54+08:00
[INFO] ------------------------------------------------------------------------

Possible problems

Package upload error

mvn clean deploy -DskipTests=true -P release
gpg: Signing failed: Inappropriate ioctl for device
gpg: signing failed: Inappropriate ioctl for device

resolvent:
Setting environment variables

export GPG_TTY=$(tty)

Re execute mvn clean deploy -DskipTests=true -P release, and a password input interface will pop up. Enter the password of the key we set before

Login site configuration Publishing

https://s01.oss.sonatype.org/#stagingRepositories

Click staging repositories - > check the jar package we want to publish - > Click Close


After submitting, wait a moment to refresh, and you will see the prompt status is closed. You can proceed to the next step

Click release

Search our Jar package

You can see the Jar package we released. Each version of the package can only be submitted once and cannot be submitted to the public warehouse repeatedly
Note: do not release the SNAPSHOT version here

In the central warehouse, you can also see the jar package we passed

Application in project

Add dependency and download. It is found that the jar package is downloaded successfully

Test the application and it can be used normally

When we need to update the jar package, we can modify the version number mvn clean deploy -DskipTests=true -P release, then submit it to the warehouse for release, and execute closed - > release.

After uploading, we can directly reference it in pom.xml https://mvnrepository.com/ You need to wait a few hours for the search to upload, and you can't see it directly.

So far, we have successfully uploaded it to the central warehouse.

Problems encountered and Solutions

There was an episode in the middle
Three errors are prompted after uploading

Click Activity to view

Prompt for less POM information

Invalid POM: /com/gitee/jastee/jast-tool/0.0.1/jast-tool-0.0.1.pom: Project name missing, Project description missing, Project URL missing, License information missing

Cause of problem:
The pom.xml file is not configured with relevant parameters. The configuration must be the same as that listed above.

Tags: Maven

Posted on Sun, 24 Oct 2021 00:29:13 -0400 by cbj4074