Publish your own open source project to Maven central warehouse

Posted by ego0 on Sat, 29 Jan 2022 19:50:17 +0100

1, Create an OSS account

The password requirements of OSS account are relatively strict. It is recommended to record and make notes; The user name and password of the OSS account need to be configured to Maven's setting XML file.

2, Create Jira issue for new project hosting

Log in to the newly registered OSS account and click new

After creating a Jira Issue and submitting it, wait for the staff to approve it. If there is no problem, the Issue you submitted will change its status to RESOLVED. Indicates that the configuration is successful.

3, Install and configure GPG

GNU PG Download

We need to install GNU PG. after installation, enter the command in our Terminal:

gpg -- version

Check whether the installation is successful.

After installation, generate a key pair and enter the command GPG -- full Gen key

gpg --full-gen-key
gpg --full-gen-key
        gpg (GnuPG) 2.1.15; Copyright (C) 2016 Free Software Foundation, Inc.
        This is free software: you are free to change and redistribute it.
        There is NO WARRANTY, to the extent permitted by law.
        gpg: keybox 'C:/Users/Nadeem/AppData/Roaming/gnupg/pubring.kbx' created

        Please select what kind of key you want:
        (1) RSA and RSA (default)
        (2) DSA and Elgamal
        (3) DSA (sign only)
        (4) RSA (sign only)
        Your selection? 1
        RSA keys may be between 1024 and 4096 bits long.
        What keysize do you want? (2048)
        Requested keysize is 2048 bits
        Please specify how long the key should be valid.
        0 = key does not expir

After entering the user name, email and other information, an input box will pop up asking us to enter Passphrase:

We need to remember the set Passphrase and follow it up in Maven's setting XML file needs to be used!

After setting the encryption key, we need to publish the public key to the OSSRH server, because you will use this public key to encrypt your jar package. When you upload your jar package to the OSSRH server, it will be decrypted with the private key.

Enter command:

gpg --list-key

You can view the key we set

pub   rsa2048 2021-06-10 [SC]
      EAA2F85838644032D5FC5A3070DB8094C525F6FE
uid           [ultimate] jinrunheng (yes) <1175088275@qq.com>
sub   rsa2048 2021-06-10 [E]

Eaa2f8583644032d5fc5a3070db8094c525f6fe is the public key

Upload the public key to pool sks-keyservers. net

gpg --keyserver hkp://pool.sks-keyservers.net --send-keys EAA2F85838644032D5FC5A3070DB8094C525F6FE 

4, Configure Maven's setting xml

Configure the authentication information of oss warehouse

<servers>
    <server>
        <id>ossrh</id>
        <username>You registered oss User name for</username>
        <password>You registered oss Password for</password>
    </server>
    <server>
        <id>oss</id>
        <username>You registered oss User name for</username>
        <password>You registered oss Password for</password>
    </server>
</servers>

Key information on configuring GPG keys:

<profiles>
    <profile>
        <id>ossrh</id>
        <activation>
        <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
        <gpg.executable>gpg</gpg.executable>
        <gpg.passphrase>You set Passphrase </gpg.passphrase>
        <gpg.executable>/usr/local/bin/gpg</gpg.executable>
        <gpg.homedir>/Users/macbook/.gnupg</gpg.homedir>
        </properties>
    </profile>
</profiles>

gpg. For the information of executable, we can use the command:

which gpg

To see

gpg.homedir information can be obtained through the command:

gpg --list-key

To view the path of homedir before the public key list

➜  ~ gpg --list-key
/Users/macbook/.gnupg/pubring.kbx

5, Configure the POM of the project xml

My pom configuration reference

<?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.jinrunheng</groupId>
    <artifactId>sensitive-words-filter</artifactId>
    <version>0.0.1</version>
    <name>sensitive-words-filter</name>
    <description>This is a Chinese sensitive words filter implemented in Java</description>
    <url>https://github.com/jinrunheng/sensitive-words-filter</url>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
    </properties>
    <dependencies>
        <!--commons-lang3-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.7.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.11</version>
        </dependency>
    </dependencies>

    <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>
    <developers>
        <developer>
            <!--Input in sonatype Account and contact email created -->
            <name>dubyKim</name>
            <email>1175088275@qq.com</email>
        </developer>
    </developers>
    <scm>
        <connection>scm:git:git@github.com:jinrunheng/sensitive-words-filter.git</connection>
        <developerConnection>scm:git:git@github.com:jinrunheng/sensitive-words-filter.git</developerConnection>
        <url>git@github.com:jinrunheng/sensitive-words-filter.git</url>
        <tag>sensitive-words-filter-0.0.1</tag>
    </scm>
    <build>
        <plugins>
            <plugin>
                <!--for unit test-->
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-scm-plugin</artifactId>
                <version>1.8.1</version>
            </plugin>
            <!--source code-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.1.2</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!--Java doc-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.9.1</version>
                <configuration>
                    <source>8</source>
                    <aggregate>true</aggregate>
                    <charset>UTF-8</charset>
                    <encoding>UTF-8</encoding>
                    <docencoding>UTF-8</docencoding>
                    <additionalparam>-Xdoclint:none</additionalparam>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!--deploy-->
            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.2</version>
                <executions>
                    <execution>
                        <id>default-deploy</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>deploy</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!--GPG Package plug-in-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-gpg-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>verify</phase>
                        <goals>
                            <goal>sign</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!--Deploy components to OSSRH And publish it to Central Repository-->
            <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>
                    <autoReleaseAfterClose>true</autoReleaseAfterClose>
                </configuration>
            </plugin>

        </plugins>
    </build>
    <distributionManagement>
        <snapshotRepository>
            <id>oss</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
        </snapshotRepository>
        <repository>
            <id>ossrh</id>
            <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
        </repository>
    </distributionManagement>
</project>

The configuration of pom is strictly required, including name,description,url,licenses,developers,scm and other basic information. In addition, it should be noted that the id in snapshot repository and repository must be consistent with setting The id of the server in XML should be consistent!

It should be noted that in many old documents, the address of nexusUrl configuration of nexus staging Maven plugin is oss.sonatype.org/.

However, in February 2021, the latest official document has suggested that we configure the address as s01.oss.sonatype.org/

For details, please move to the link: central.sonatype.org/publish/relea...

6, Publish jar package

Execute command

mvn clean deploy

If the project builds successfully, after waiting for a period of time, we can Nexus Last, we found the package we released

7, Reference link

Article reference link:

How to publish your own open source project to Maven central warehouse?

How to upload customized jar s to Maven central warehouse

Pit encountered when publishing components to Maven central warehouse

Topics: Java