一 申请发布

  1. 申请发布
    https://issues.sonatype.org/browse/OSSRH-74581
    Maven 部署中央仓库 - 图1

  2. 回复
    根据恢复创建一个新的厂库,表示你对该仓库是你的
    Maven 部署中央仓库 - 图2
    其余的根据回复修改下就行了.一般一个小时内回复.

二 开始发布

  1. 成pgp密钥并上传密钥服务器
    下载pgp:https://gnupg.org/download/index.html
    1. #生成(按照提示,依次输入用户名、邮箱。最后输入O)
    2. gpg --gen-key
    3. #查看
    4. gpg --list-keys
#上传pgp密钥到服务器
gpg --keyserver hkp://keyserver.ubuntu.com --send-keys EE5DB221F8655BB4A4F47AC7994C53F9F7D0271B
#查看是否成功
gpg --keyserver hkp://keyserver.ubuntu.com --recv-keys EE5DB221F8655BB4A4F47AC7994C53F9F7D0271B

Maven 部署中央仓库 - 图3
Maven 部署中央仓库 - 图4

  1. 配置setting

    <?xml version="1.0" encoding="UTF-8"?>
    <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    
    <mirrors>
      <mirror>
          <id>huaweicloud</id>
          <mirrorOf>*</mirrorOf>
          <url>https://repo.huaweicloud.com/repository/maven/</url>
      </mirror>
    </mirrors>
    
    <!-- Sonatype的用户名密码 -->
    <servers>
      <server>
          <id>ossrh</id>
          <username>rxc</username>
          <password>Sonatype1234@</password>
      </server>
    </servers>
    
    <profiles>
      <profile>
          <id>ossrh</id>
          <activation>
              <activeByDefault>true</activeByDefault>
          </activation>
          <properties>
              <!-- 生成密钥时输入的passphrase -->
              <gpg.passphrase>123123123</gpg.passphrase>
              <!-- gpg.exe的绝对路径,在gpg安装目录下的bin目录中 -->
              <gpg.executable>C:\Program Files (x86)\GnuPG\bin\gpg.exe</gpg.executable>
              <!-- 上一步执行gpg -list-key命令输出的路径,一般在C:\User\当前用户\AppData\Roaming\gnupg -->
              <!--<gpg.homedir>C:\Users\wangxin13\AppData\Roaming\gnupg</gpg.homedir>-->
          </properties>
      </profile>
    </profiles>
    
  1. 配置maven pom.xml
    https://github.com/simpligility/ossrh-demo/blob/master/pom.xml
    <?xml version="1.0" encoding="UTF-8"?>
    

4.0.0

<groupId>io.gitee.blogg</groupId>
<artifactId>json2sql</artifactId>
<version>1.0.5</version>

<properties>
    <java.version>11</java.version>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <!--打包跳过测试-->
    <skipTests>true</skipTests>
</properties>

<dependencies>
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>5.7.15</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>29.0-jre</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.12</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

<!--分析到 OSSRH 仓库,必须填写-->
<name>json2sql</name>
<description>A java tool class that json goes to sql</description>
<url>https://gitee.com/blogg/json2sql</url>

<licenses>
    <license>
        <name>The Apache Software License, Version 2.0</name>
        <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
    </license>
</licenses>
<developers>
    <developer>
        <!--输入在sonatype创建的账户和联系邮箱 -->
        <name>rxc</name>
        <email>837533730@qq.com</email>
    </developer>
</developers>
<scm>
    <connection>https://gitee.com/blogg/json2sql</connection>
    <developerConnection>https://gitee.com/blogg/json2sql</developerConnection>
    <url>https://gitee.com/blogg/json2sql</url>
</scm>

<build>
    <plugins>
        <!--将组件部署到 OSSRH 并将其发布到 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>
        <!--源码附件︎-->
        <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>
        <!--Javadoc-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>2.9.1</version>
            <configuration>
                <source>11</source>
                <aggregate>true</aggregate>
                <charset>UTF-8</charset>
                <encoding>UTF-8</encoding>
                <docencoding>UTF-8</docencoding>
                <additionalparam>-Xdoclint:none</additionalparam>
            </configuration>
            <executions>
                <execution>
                    <id>attach-javadocs</id>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <!--GPG 签名组件︎-->
        <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>

<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>
  1. 开始发布
    mvn clean deploy
    

Maven 部署中央仓库 - 图5

  1. 发布查看
    https://s01.oss.sonatype.org/index.html
    点击左侧 staging repositories,可以看到自己的项目, 点击上面close, 等会议release 就可以点击了.然后发布到maven仓库中心
    Maven 部署中央仓库 - 图6

三 常见问题

  1. 无法将pgp密钥发送到密钥服务器
    #修改默认仓库
    gpg --keyserver hkp://keyserver.ubuntu.com --send-keys EE5DB221F8655BB4A4F47AC7994C53F9F7D0271B
    


解决方法: https://qastack.cn/superuser/815641/unable-to-send-pgp-key-to-keyserver

  1. maven 打包java doc 版本不支持, 乱码
             <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-javadoc-plugin</artifactId>
              <version>2.9.1</version>
              <configuration>
                  <source>11</source>
                  <aggregate>true</aggregate>
                  <charset>UTF-8</charset>
                  <encoding>UTF-8</encoding>
                  <docencoding>UTF-8</docencoding>
                  <additionalparam>-Xdoclint:none</additionalparam>
              </configuration>
              <executions>
                  <execution>
                      <id>attach-javadocs</id>
                      <goals>
                          <goal>jar</goal>
                      </goals>
                  </execution>
              </executions>
          </plugin>
    
  1. mvn发布 失败
    #pom增加配置
    <name>json2sql</name>
    <description>json2sql</description>
    <url>https://gitee.com/blogg/json2sql</url>
    


解决方法: https://www.ossez.com/t/ossrh/13430

参考:

https://learnku.com/articles/58210

https://dunwu.github.io/java-tutorial/javatool/build/maven/maven-deploy.html