Location>code7788 >text

Graphic Tutorial: Publishing a Project to the Maven Central Repository from 0 to 1

Popularity:58 ℃/2024-09-01 21:24:18

preamble

This article is based on the official documentation/publish/publish-guide/ Writing.

Publishing Steps:

  1. Create an account
  2. Create User Token
  3. Creating a Namespace
  4. Configuring the GPG
  5. Configuration items
  6. post

Caveats:

  • Successfully published items cannot be modified or deleted

preparatory phase

Create an account

If you already have a Google or Github account, you can skip this step and log in directly using their associations.

The steps to create an account are as follows:

  1. Open the official website./ Click on the upper right corner of theSign InThen, on the login page that opens, clickSign up
    Note: The email address should be a real one with verification.
    官网
    登录页

  2. After successful registration, you will be prompted to verify your email address.
    邮箱验证提示

  3. Open your mailbox, collect the email, and clickConfirm My AccountVerify Email.
    邮件

  4. The page that opens prompts that the e-mail address has been verified, indicating that the registration has been successful.
    邮箱验证成功

Create User Token

  1. Open the official website./ Click on the upper right corner of theSign InThen enter your username and password directly into the login page that opens, or you can choose to log in directly with your Google or Github affiliation.

  2. Click on Account in the upper right corner and in the drop down menu selectView Account
    View Account

  3. strike (on the keyboard)Generate User Token
    Generate User Token

  4. Then in the dialog box that pops up clickOkIf you have a Token, it will generate a Token of the following form.

    <server>
       <id>${server}</id>
       <username>0j6XV+5V</username>
       <password>lSbp/2aMFiSiyzwNstB9zmzo6+5YyLqNHr9JDD8yKC5T</password>
    </server>
    

    Notice here theidThe information, which needs to be changed to your customized one, will be used later. Then copy the above content to the file you want to publish the project to use.file, where theidThe value should readcentral

    <servers>
       <server>
          <id>central</id>
          <username>0j6XV+5V</username>
          <password>lSbp/2aMFiSiyzwNstB9zmzo6+5YyLqNHr9JDD8yKC5T</password>
       </server>
    </servers>
    

Creating a Namespace

The official website explains the namespace:

Before you can publish your components, you must choose a namespace. In the Maven ecosystem, this is also known as a groupId, which is one of the three required coordinates to describe any component published to Maven Central, . groupId, artifactId, version.

Before publishing a component, you must choose a namespace. In the Maven ecosystem, this is also known as the groupId, and it is one of the three coordinates required to describe any component published to Maven Central - groupId, artifactId, version.

There are two ways to choose your namespace:

  1. You own the domain name and can use a reverse domain name for the namespace as follows:

    • ->
    • ->
    • -> -domain

    This method requires you to add a specified domain name resolution for the corresponding domain name to verify your ownership of the domain name, which can be deleted after successful verification.

  2. You have a code hosting service account from the following list, which can be used as a namespace in the form of a reverse domain name of a personal domain name

    Service Example namespace
    GitHub
    GitLab
    Gitee
    Bitbucket

    In this way, you need to create a public repository with the specified name under the corresponding account to verify the account ownership, and you can delete it after successful verification.

The steps to create a namespace are as follows:

  1. Open the official website./ Click on the upper right corner of theSign InThen enter your username and password directly into the login page that opens, or you can choose to log in directly with your Google or Github affiliation.

  2. Click on Account in the upper right corner and in the drop down menu selectView Namespaces
    View Namespaces

  3. In the page that opens click onAdd Namespace
    Add Namespace

  4. Enter the custom Namespace in the pop-up dialog box.
    Namespace

  5. After successful creation, the Namespace is shown as pending validation, click theVerify NamespacePerform validation.
    Verify Namespace

  6. The dialog box that pops up will give you different prompts depending on the type of Namespace you fill in. If you fill in a personal domain name for a code hosting service, you will be prompted to go to the corresponding platform to create a public repository with a specified name; if it's a personal domain name, you will be prompted to create a public repository with a specified content of theTXTtype of domain name resolution. Anyway, you'll get aVerification KeyYou need to use this value to do the corresponding operation. Click on the dialog box'sConfirmbutton, Namespace becomes Pending Validation:
    待验证
    Choose one of the following two verification methods:

    • Create a public repository:
      创建公共代码仓库
    • Add domain name resolution:
      添加域名解析
  7. Wait for Namespace to validate successfully, then you can remove the public repository you just created or the domain resolution records you added.
    Namespace验证成功

Configuring the GPG

  1. Open the official website./download/#sec-1-2Download the corresponding version for your system and install it. Usegpg --versionVerify that the installation was successful.

    C:\Users\Administrator>gpg --version
    gpg (GnuPG) 2.3.8
    libgcrypt 1.10.1
    Copyright (C) 2021 g10 Code GmbH
    License GNU GPL-3.0-or-later </licenses/>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.
    
    Home: C:\Users\Administrator\AppData\Roaming\gnupg
    Supported algorithms:
    Pubkey: RSA, ELG, DSA, ECDH, ECDSA, EDDSA
    Cipher: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH,
          CAMELLIA128, CAMELLIA192, CAMELLIA256
    AEAD: EAX, OCB
    Hash: SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224
    Compression: Uncompressed, ZIP, ZLIB, BZIP2
    
  2. utilizationgpg --gen-keycommand to generate a key pair, you will be prompted to enter theReal namecap (a poem)Email address(It is recommended to enter a real e-mail address to be used later for secret key recovery), after entering it, a pop-up window will ask you to enter a password (Note: This password is used every time you publish a project), then the key pair is generated.
    The following content in the5BBA74D9F7E76AAA234ACB337B583099E6B290D8It's the generated public key, which you need to send to the secret key server next, because other people will need this public key to validate the files you publish.

    C:\Users\Administrator>gpg --gen-key
    gpg (GnuPG) 2.3.8; Copyright (C) 2021 g10 Code GmbH
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.
    
    Note: Use "gpg --full-generate-key" for a full featured key generation dialog.
    
    GnuPG needs to construct a user ID to identify your key.
    
    Real name: t1
    Email address: t1@
    You selected this USER-ID:
       "t1 <t1@>"
    
    Change (N)ame, (E)mail, or (O)kay/(Q)uit? O
    We need to generate a lot of random bytes. It is a good idea to perform
    some other action (type on the keyboard, move the mouse, utilize the
    disks) during the prime generation; this gives the random number
    generator a better chance to gain enough entropy.
    We need to generate a lot of random bytes. It is a good idea to perform
    some other action (type on the keyboard, move the mouse, utilize the
    disks) during the prime generation; this gives the random number
    generator a better chance to gain enough entropy.
    gpg: revocation certificate stored as 'C:\\Users\\Administrator\\AppData\\Roaming\\gnupg\\\\'
    public and secret key created and signed.
    
    pub   ed25519 2024-08-26 [SC] [expires: 2026-08-26]
          5BBA74D9F7E76AAA234ACB337B583099E6B290D8
    uid                      t1 <t1@>
    sub   cv25519 2024-08-26 [E] [expires: 2026-08-26]
    
  3. utilizationgpg --keyserver --send-keys <public-key>command to send the public key to the server, if it fails to send, you can choose another server to retry several times, it will always succeed.

    The GPG key servers supported by the central server are:

    # Send public key
    C:\Users\Administrator>gpg --keyserver --send-keys 5BBA74D9F7E76AAA234ACB337B583099E6B290D8
    gpg: sending key 7B583099E6B290D8 to hkp://
    
    # Failed to validate public key
    C:\Users\Administrator>gpg --keyserver --recv-keys 5BBA74D9F7E76AAA234ACB337B583099E6B290D8
    gpg: keyserver receive failed: No data
    
    # Verify Public Key Successfully
    C:\Users\Administrator>gpg --keyserver --recv-keys 5BBA74D9F7E76AAA234ACB337B583099E6B290D8
    gpg: key 7B583099E6B290D8: "t1 <t1@>" not changed
    gpg: Total number processed: 1
    gpg: unchanged: 1
    

Configuration items

Below is a project complete withConfiguration example, you need to make changes based on the actual information of the project:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
    xmlns:xsi="http:///2001/XMLSchema-instance"
    xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.">
    <modelVersion>4.0.0</modelVersion>

    <groupId></groupId>
    <artifactId>no-bug-spring-boot-starter</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <name>No Bug</name>
    <description>Bugdisperse,Bufffill up</description>
    <url>/iminghe/no-bug</url>

    <licenses>
        <license>
            <name>Apache License, Version 2.0</name>
            <url>/licenses/LICENSE-2.</url>
        </license>
    </licenses>

    <scm>
        <url>/iminghe/no-bug</url>
        <connection>scm:git:/iminghe/</connection>
        <developerConnection>scm:git:/iminghe/</developerConnection>
    </scm>

    <developers>
        <developer>
            <name>minghe</name>
            <email>iminghe@</email>
        </developer>
    </developers>

    <properties>
        <>8</>
        <>8</>
        <>UTF-8</>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId></groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.7.18</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId></groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- Compiler -->
            <plugin>
                <groupId></groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.13.0</version>
                <configuration>
                    <source>${}</source>
                    <target>${}</target>
                    <encoding>${}</encoding>
                </configuration>
            </plugin>
            <!-- Source -->
            <plugin>
                <groupId></groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.3.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- Javadoc -->
            <plugin>
               <groupId></groupId>
               <artifactId>maven-javadoc-plugin</artifactId>
               <version>3.10.0</version>
               <executions>
                     <execution>
                        <id>attach-javadocs</id>
                        <goals>
                           <goal>jar</goal>
                        </goals>
                     </execution>
               </executions>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>release</id>
            <build>
                <plugins>
                    <!-- GPG -->
                    <plugin>
                        <groupId></groupId>
                        <artifactId>maven-gpg-plugin</artifactId>
                        <version>3.2.5</version>
                        <executions>
                            <execution>
                                <id>sign-artifacts</id>
                                <phase>verify</phase>
                                <goals>
                                    <goal>sign</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId></groupId>
                        <artifactId>central-publishing-maven-plugin</artifactId>
                        <version>0.5.0</version>
                        <extensions>true</extensions>
                        <configuration>
                            <publishingServerId>central</publishingServerId>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

Description:

  • groupId: The Namespace certified in the previous steps.
  • licenses: License information for the project.
  • scmsource control systemSource code management system. This part of the element is required if the URL itself does not need to be public. You can specify a URL that is not accessible to the public, or even a placeholder indicating that your SCM URL is private and not suitable for public access.
  • developers: Developer information.
  • build: Packaged public plug-ins.
  • profiles: Define configurations for different environments usingidAs an identifier, it can be used to performmvncommand when selecting the specified environment. This defines thereleaseenvironment is used for project publishing, wherecentral-publishing-maven-pluginis used to publish the project, wherepublishingServerIdcorresponding to the user's Token settingid

release phase

existProfilesdecide upon a candidatereleaseand then execute thecleandeployCommand.
发布

Alternatively, you can execute it directly from the project directory in your terminal.mvn clean deploy -P release

Wait for the command to finish executing, then log in to the official website, click on Account in the upper right corner, and selectView Deployments, you will see the items that have been verified to be published.
发布页面

After confirming that there are no errors, clickPublishThe status will change toPUBLISHINGand then wait a while for the status to change toPUBLISHED, that means the release was successful, congratulations!