掘金 后端 ( ) • 2024-05-06 11:44

theme: hydrogen

前两天打算发布自己的框架到maven仓库上,才发现从2024 年3月12日起之前OSSRH的发布方式已经不再支持新的仓库进行发布了,之后的代码发布只能使用新版的发布流程了。
image.png

中央仓库官方发布指南:https://central.sonatype.org/register/central-portal/

新版的发布步骤与旧版OSSRH类似,不过多了几个步骤而已。

注册SONATYPE账号并验证namespace

地址:https://central.sonatype.com

image.png
在注册时,使用GitHub登录的话,就无需验证namespace了。因为在使用GitHub登录时已经验证了namespace了image.png
目前支持的代码托管服务命名空间
目前支持的代码托管服务命名空间

添加其他namespace

假设现在我需要添加其他命名空间该咋办呢?
image.png
添加对应的命名空间后,就能得到一个密钥,使用该密钥创建一个公开的空仓库就能进行namespace的认证了。
image.png
添加完成后,回到sonatype中,验证namespace即可。
image.png
此时,就完成了新的namespace的添加了。
image.png

pom文件中添加相应属性

GAV

首先是pom文件三要素GAV

    <groupId>io.github.azirzsk</groupId>
    <artifactId>deploy-demo</artifactId>
    <version>0.0.1</version>

这里要注意的是,groupId需要与上面已经认证过的namespace相同,否则将推送不上去。

Project和Description和Url

项目信息以及描述

    <name>${project.groupId}:${project.artifactId}</name>
    <description>
        deploy demo to maven central
    </description>
    <url>https://github.com/AzirZsk/deploy-demo</url>

License

对应的开源协议

    <licenses>
        <license>
            <name>The Apache License, Version 2.0</name>
            <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
        </license>
    </licenses>

developer

开发者信息

    <developers>
        <developer>
            <name>Azir</name>
            <email>[email protected]</email>
        </developer>
    </developers>

SCM

版本控制

    <scm>
        <connection>scm:git:git://github.com/AzirZsk/deploy-demo.git</connection>
        <developerConnection>scm:git:ssh://github.com/AzirZsk/deploy-demo.git</developerConnection>
        <url>http://github.com/AzirZsk/deploy-demo/tree/master</url>
    </scm>

添加maven插件

提供source、javadocJar包

发布到中央仓库的代码还有一些其他的要求,除了提供普通的jar包之外,还需要提供source以及javadoc的jar包,所以就需要用到打包的插件了。

  <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-source-plugin</artifactId>
      <version>3.1.0</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-javadoc-plugin</artifactId>
      <version>3.1.0</version>
      <configuration>
          <additionalJOptions>
              <additionalJOption>-Xdoclint:none</additionalJOption>
          </additionalJOptions>
      </configuration>
      <executions>
          <execution>
              <id>attach-javadocs</id>
              <goals>
                  <goal>jar</goal>
              </goals>
          </execution>
      </executions>
  </plugin>

PGP签名

打包之后,还需要对打包后的jar包进行签名,用于jar包的认证

下载PGP软件

windows版本:https://www.gpg4win.org/

下载完Gpg4win后,创建一个密钥对
image.png
创建时需勾选上密码认证
image.png

上传密钥到服务器

创建完成后,还需要将密钥上传到服务器,用于其他人对于签名进行认证。
目前,中央仓库支持以下三个GPG密钥服务器
image.png
上传命令

gpg --keyserver keyserver.ubuntu.com --send-keys CA925CD6C9E8D064FF05B4728190C4130ABA0F98

CA925CD6C9E8D064FF05B4728190C4130ABA0F98这串字符串你刚刚生成的密钥ID

maven添加GPG插件

将密钥上传到服务器后,我们就能添加GPG插件,使用插件对生成的jar包进行签名了。

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-gpg-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <id>sign-artifacts</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>sign</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

添加新发布插件

其实,上面的步骤就是之前发布到中央仓库的步骤了,但是由于变更了发布逻辑导致之前的步骤不适用了。
image.png
为此,中央仓库新发布流程提供了一个新的maven插件。

        <plugin>
          <groupId>org.sonatype.central</groupId>
          <artifactId>central-publishing-maven-plugin</artifactId>
          <version>0.4.0</version>
          <extensions>true</extensions>
          <configuration>
            <publishingServerId>central</publishingServerId>
            <tokenAuth>true</tokenAuth>
            <deploymentName>${project.groupId}:${project.artifactId}:${project.version}</deploymentName>
          </configuration>
        </plugin>

在上面的插件中,我额外配置了一个属性<deploymentName>,这个属性等等将影响到页面上的显示。

配置服务器token

在使用该插件之前,还需要在中央仓库获取token。
image.png
image.png
复制server配置到你本地的maven的settings.xml文件中。

deploy到中央仓库

执行完以上步骤后,只需要执行deploy就能将服务打包到中央仓库了。
image.png
等到显示BUILD SUCCESS后,就能到后台页面看到刚刚deploy的服务了。image.png
此时,点击Publish等待中央仓库对服务进行验证通过后,就能使用了。
image.png
在中央仓库进行搜索,也能看到刚刚发布的代码了。
image.png


示例仓库:https://github.com/AzirZsk/deploy-demo