Publish jar package to maven central warehouse

Posted by benpaxton777 on Sat, 25 Dec 2021 06:43:36 +0100

Configuration environment

Installing the java environment

Link: https://pan.baidu.com/s/1o-wFA-m33JQs-sQJ-DgRaQ 
Extraction code: ux7j

After downloading to the server, unzip it to the specified location

$ mkdir /usr/java
$ tar xzf jdk-8u301-linux-x64.tar.gz  -C /usr/java
$ vim /etc/profile

Write the following

export JAVA_HOME=/usr/java/jdk1.8.0_301
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib
export PATH=$JAVA_HOME/bin:$PATH

Save exit
Execute source /etc/profile to make the configuration file effective.

Verify that the installation was successful

 $ java -version
   java version "1.8.0_301"
   Java(TM) SE Runtime Environment (build 1.8.0_301-b09)
   Java HotSpot(TM) 64-Bit Server VM (build 25.301-b09, mixed mode)

Seeing this indicates that the installation is successful.

Installing maven

$ mkdir /usr/mvn
$ cd /usr/mvn
$ wget https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.8.1/binaries/apache-maven-3.8.1-bin.tar.gz
$ tar -zxvf apache-maven-3.8.1-bin.tar.gz
$ vim /etc/profile

Write the following

export MAVEN_HOME=/usr/mvn/apache-maven-3.8.1

export PATH=$PATH:$MAVEN_HOME/bin

Save exit
Execute source /etc/profile to make the configuration file effective.

Verify that the installation was successful

 $ mvn -version
 Maven home: /usr/mvn/apache-maven-3.8.1
 Java version: 1.8.0_301, vendor: Oracle Corporation, runtime: /usr/java/jdk1.8.0_301/jre
 Default locale: en_US, platform encoding: UTF-8
 OS name: "linux", version: "4.18.0-240.10.1.el8_3.x86_64", arch: "amd64", family: "unix"

Other tools

$ yum install git gpg -y

If it is already installed, you can skip this step

Register sonatype account

click Register a new account.

Create a new issues after logging in

Just fill in as required, mainly a Group Id
If your code is hosted on Github, you cannot write the Group Id
com.github.xxx, I start with io. The details are in here

After creation, you will jump to this connection https://issues.sonatype.org/browse/OSSRH-xxxx , when you see the connection, you need to create a warehouse on your Github to prove that you are the owner of the Github. The name of the warehouse is ossrh XXXX in the connection.

The created warehouse connection is https://github.com/yourgithubname/OSSRH-xxxx Then leave a message at the bottom of the post to tell the administrator that you have created a warehouse named ossrh XXXX, which can save him from asking you to prove that you are the owner of the account and improve efficiency. After the administrator replies, you can upload the jar package.

The following is the administrator's reply. Seeing this means that it is completed.

io.github.xxx has been prepared, now user(s) youname can:
Publish snapshot and release artifacts to s01.oss.sonatype.org
Have a look at this section of our official guide for deployment instructions:
https://central.sonatype.org/publish/publish-guide/#deployment

Depending on your build configuration, your first component(s) might be released automatically after a successful deployment.
If that happens, you will see a comment on this ticket confirming that your artifact has synced to Maven Central.
If you do not see this comment within an hour or two, you can follow the steps in this section of our guide:
https://central.sonatype.org/publish/release/

######

As part of our efforts to improve the security and quality posture of the open source supply chain,
we plan to enable additional scanning of dependencies for security alerts soon. Since you're already
hosting your source code in Github, you can get these insights today by enabling Sonatype Lift.
Sonatype Lift is free forever on public repositories! Lift tells you about open source vulnerabilities
during code review, and goes beyond open source to scan your code for both code quality and security issues,
providing feedback right in your pull requests.
More information can be found at https://links.sonatype.com/products/lift/github-integration

######

Preparation before release

Modify pom file

  <groupId>io.github.xxxx</groupId>
  <artifactId>xxxx</artifactId>
  <version>1.0.0</version>
  <name>xxx</name>
  <url>xxxx</url>
  <description>xxxxx</description>
  <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.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.1</version>
              <configuration>
                  <source>1.8</source>
                  <target>1.8</target>
              </configuration>
          </plugin>
          <!-- Source -->
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-source-plugin</artifactId>
              <version>2.2.1</version>
              <executions>
                  <execution>
                      <phase>package</phase>
                      <goals>
                          <goal>jar-no-fork</goal>
                      </goals>
                  </execution>
              </executions>
          </plugin>
          <!-- Javadoc -->
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-javadoc-plugin</artifactId>
              <version>2.9.1</version>
              <configuration>
                  <additionalparam>-Xdoclint:none</additionalparam>
              </configuration>
              <executions>
                  <execution>
                      <phase>package</phase>
                      <goals>
                          <goal>jar</goal>
                      </goals>
                  </execution>
              </executions>
          </plugin>
          <!-- GPG mvn clean deploy -P release -Dgpg.passphrase=YourPassphase -->
          <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>
      </plugins>
  </build>

  <licenses>
      <license>
          <name>MIT License</name>
          <url>https://github.com/xxx/xxx/xxxxxx/master/LICENSE</url>
          <distribution>repo,manual</distribution>
      </license>
  </licenses>
  <developers>
      <developer>
          <name>xxx</name>
          <email>xxxx</email>
          <url>xxxx</url>
      </developer>
  </developers>
  <scm>
      <connection>scm:git:https://github.com/xxx/xxxx.git</connection>
      <developerConnection>scm:git:https://github.com/xxxx/xxxx.git</developerConnection>
      <url>https://github.com/xxxx/xxxxx</url>
      <tag>0.0.1</tag>
  </scm>

You only need to modify the content related to yourself, which is represented by xxxx in the text, and others do not need to be modified

The Group Id must be consistent with that of the application

Upload gpg key

$  gpg generate-key
# Follow the prompts to enter the user name and email address. Finally, you will enter a password. Remember this password, which will be used below 

Send public key to PGP key server

$   gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys 6107DF0A8EE6A62EABFDD12914F722543E7D2C1E

Return results

gpg: Will key'14F722543E7D2C1E'Upload to hkp://keyserver.ubuntu.com:11371

Verify whether the upload is successful

$   gpg --keyserver hkp://keyserver.ubuntu.com:11371 --recv-keys 6107DF0A8EE6A62EABFDD12914F722543E7D2C1E

Return results

gpg: Key 14 F722543E7D2C1E: "houbb <XXX@XX.com>"Unchanged
gpg: Total quantity processed: 1
gpg:           Unchanged: 1

setting.xml

<?xml version="1.0" encoding="UTF-8"?>

<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 http://maven.apache.org/xsd/settings-1.2.0.xsd">
  <servers>
    <server>
      <id>ossrh</id>
      <username>sonatype account number</username>
      <password>sonatype password</password>
    </server>
  </servers>
  <profiles>
   <profile>
      <id>ossrh</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <gpg.executable>gpg2</gpg.executable>
        <gpg.passphrase>gpg Password of public key</gpg.passphrase>
      </properties>
    </profile>
  </profiles>
</settings>

Pay attention to the setting used by mvn Where is XML? It is usually in the conf folder and user directory under the mvn home directory m2 folder

Upload program

Execute under the project root directory

$ mvn clean deploy

Wait a moment and a window will appear to enter the password of gpg

Seeing this indicates that the upload has been successful.

After uploading successfully, log in to https://s01.oss.sonatype.org/ , the account is the sonatype account registered at the beginning.

After logging in, click Staging Repositories to see this page. Select the record and click close. After success, click Release.

At this point, you have finished. You can see the uploaded package here in a moment

For example, Group Id is Io github. xxx

artifactId is tool

version is 1.0 0

Check the address https://repo.maven.apache.org/maven2/io/github/xxx/tool/1.0.0

If the above connection can be found, it can already be used in the project. The time of synchronization to the central warehouse is uncertain.

After the release is successful, reply to the post and tell the administrator that the release is successful.

Problems encountered

  • When executing mvn deploy, 401 error is always returned, which cannot be solved after finding various methods. Just re register an account. I don't know why.

  • The repository address has changed. Now most articles on the Internet are old and new

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

Official documents If you have the latest configuration information, you should check the official documents first after encountering problems

  • Error in centos release
    Failed to execute goal org.apache.maven.plugins:maven-gpg-plugin:1.6:sign

    resolvent

Topics: Java