How to upload your own Jar to the Maven central repository

During project development, we often use Maven to pull open source third-party Jar packages from the warehouse. This article will guide you to publish your own code or open source projects to the Maven central warehouse, so that others can directly rely on your Jar package without first downloading your code and then installing it locally.

Register an account

Click the link below to register an account. Remember the registration information as it will be used later. Moreover, the password format requirements are relatively strict.

https://issues.sonatype.org/secure/Signup!default.jspa

Jira application

After registering and logging in, visit the following link to create an issue. Only if the application is approved can subsequent uploading and other operations be carried out.

https://issues.sonatype.org/secure/CreateIssue.jspa?issuetype=21 & amp;pid=10134

After submitting, you will receive a reply in a few minutes, and you will also receive the email in the email address you just registered.

Notice!

  1. All information entered here is in English.
  2. Regarding this GroupID, it cannot be a domain name you made up. If you happen to be using your own domain name (written backwards), you can fill it in like I did. If you don’t have a domain name, you have to use Github’s domain name. Fill in the format io.github.username (Subsequent verification will verify the ownership of the domain name or GitHub account)

It will take a few minutes to receive a reply, and we need to verify the filled-in GroupID.

The domain name I filled in the GroupID above (written in reverse) is world.xuewei, so I need to add a TXT type parsing record here in the @ format. , the content is the Issue number submitted this time. If the domain name of your GroupID is subordinate to the domain name, such as world.xuewei.test, you may need to add the record of the second-level domain name when adding the record. You cannot use @, I guess. , try it anyway.

After configuring, click Edit again and submit directly. Then you will need to wait a few minutes to receive the following reply:

This is OK and you can proceed with subsequent operations.

GPG environment installation

The main function of GPG is to generate key pairs, which will be used for subsequent verification of our component releases. Download address: https://www.gnupg.org/download/.

Just find the installation package that suits your device and download it.

After the installation is complete, run:

  1. Create a new key pair

  2. After selecting the certificate, publish

  3. Double-click the certificate to view the secret key, and then copy it, you will need it later.

Configure Maven setting

Find the configuration file of the locally installed Maven (note that this is not the pom.xml in the project) and open it for editing.

First find the tag and add the following content inside:

<server>
    <id>ossrh</id>
    <username>XUEW</username>
    <password>This is the password you used when registering an account in the first step</password>
</server>

Then find the tag and add the following content inside (change the installation directory to your GPG directory, and be sure to go to the gpg layer under bin):

<profile>
  <id>ossrh</id>
  <activation>
    <activeByDefault>true</activeByDefault>
  </activation>
  <properties>
    <gpg.executable>D:\Program\GnuPG\bin\gpg</gpg.executable>
    <gpg.passphrase>Here is the secret key you just copied in GPG</gpg.passphrase>
  </properties>
</profile>

Configuration project Pom

First of all, please note that the GroupID here must be the same as the one applied previously, otherwise an error will be reported when uploading. It is best to change this version number to a numeric form instead of adding the default -SNAPSHOT.

First, you need to configure the warehouse information in Pom. This information must be the same as the one applied for, otherwise an error will be reported. The content is as follows:

<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>
<!-- Warehouse information -->
<scm>
    <connection>scm:[email protected]:373675032/xw-fast.git</connection>
    <developerConnection>scm:[email protected]:373675032/xw-fast.git
    </developerConnection>
    <url>https://github.com/373675032/xw-fast</url>
</scm>
<!-- Developer information -->
<developers>
    <developer>
        <name>XUEW</name>
        <email>[email protected]</email>
        <organization>https://github.com/373675032</organization>
        <timezone> + 8</timezone>
    </developer>
</developers>

Then add some information that is inherent and does not need to be changed:

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

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.sonatype.plugins</groupId>
            <artifactId>nexus-staging-maven-plugin</artifactId>
            <version>1.6.7</version>
            <extensions>true</extensions>
            <configuration>
                <serverId>ossrh</serverId>
                <nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
                <stagingProgressTimeoutMinutes>20</stagingProgressTimeoutMinutes>
                <autoReleaseAfterClose>true</autoReleaseAfterClose>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.2.1</version>
            <executions>
                <execution>
                    <id>attach-sources</id>
                    <goals>
                        <goal>jar-no-fork</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-gpg-plugin</artifactId>
            <version>1.5</version>
            <executions>
                <execution>
                    <id>sign-artifacts</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>sign</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <configuration>
                <additionalOptions>
                    <additionalOption>-Xdoclint:none</additionalOption>
                </additionalOptions>
            </configuration>
            <executions>
                <execution>
                    <id>attach-javadocs</id>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Complete POM file

<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>world.xuewei</groupId>
    <artifactId>xw-fast-parent</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>
    <name>xw-fast</name>
    <description>
      Xw-Fast is a convenient development scaffolding developed specifically for Java Web and encapsulated by the Spring series framework. It aims to reduce the cost of learning and using the framework, improve work efficiency, and greatly improve the efficiency of Web development.
    </description>
    <modules>
        <module>xw-fast-core</module>
        <module>xw-fast-web</module>
        <module>xw-fast-crud</module>
        <module>xw-fast-all</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>utf-8</project.reporting.outputEncoding>

        <Automatic-Module-Name>world.xuewei.fast</Automatic-Module-Name>

        <!-- versions -->
        <compile.version>8</compile.version>
        <junit.version>5.9.2</junit.version>
        <lombok.version>1.18.26</lombok.version>
        <hutool.version>5.7.17</hutool.version>
        <boot.version>2.7.17</boot.version>
        <fastjson.version>1.2.47</fastjson.version>

    </properties>

    <dependencies>
        <!-- Global unit testing -->
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>${boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
            <version>${boot.version}</version>
        </dependency>
    </dependencies>

    <url>https://github.com/373675032/xw-fast</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>
    <!-- Warehouse information -->
    <scm>
        <connection>scm:[email protected]:373675032/xw-fast.git</connection>
        <developerConnection>scm:[email protected]:373675032/xw-fast.git
        </developerConnection>
        <url>https://github.com/373675032/xw-fast</url>
    </scm>
    <!-- Developer information -->
    <developers>
        <developer>
            <name>XUEW</name>
            <email>[email protected]</email>
            <organization>https://github.com/373675032</organization>
            <timezone> + 8</timezone>
        </developer>
    </developers>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.sonatype.plugins</groupId>
                <artifactId>nexus-staging-maven-plugin</artifactId>
                <version>1.6.7</version>
                <extensions>true</extensions>
                <configuration>
                    <serverId>ossrh</serverId>
                    <nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
                    <stagingProgressTimeoutMinutes>20</stagingProgressTimeoutMinutes>
                    <autoReleaseAfterClose>true</autoReleaseAfterClose>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.2.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-gpg-plugin</artifactId>
                <version>1.5</version>
                <executions>
                    <execution>
                        <id>sign-artifacts</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>sign</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <configuration>
                    <additionalOptions>
                        <additionalOption>-Xdoclint:none</additionalOption>
                    </additionalOptions>
                </configuration>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

Deployment upload

First refresh, then clean, then deploy.

The deployment time is a bit long, so be patient and wait. This step is the most critical. I retried many times due to configuration errors and my mentality collapsed…

Verification

Log in to the system using the account registered in the first step https://s01.oss.sonatype.org/.

You can observe the following during the previous deployment process:

After the deployment is completed, you can observe the following:

If you found your Jar, congratulations, you have successfully uploaded it! After that, you need to wait another two or three hours, and you can search for the dependencies you published at https://search.maven.org and https://mvnrepository.com! You will also receive an email notification.

Reference links:

  1. JAVA How to upload your own jar package to the Maven central repository_Upload the local jar package to the maven repository – CSDN Blog
  2. Upload the project to the Maven central warehouse (latest in 2023) – Zhihu (zhihu.com)
  3. Organizing the details of publishing jar packages to maven’s central warehouse – Chen Hao and Da Haohai – Blog Park (cnblogs.com)