注册sonatype账号

Maven中央仓库并不支持直接发布jar包,sonatype是其指定的第三方仓库之一,发布到此的项目会被定时同步到中央仓库
官方教程地址:https://central.sonatype.org/register/central-portal/

  • 访问网址:https://central.sonatype.com/,并点击右上角访问登陆界面
  • 选择注册账号 sign up

如果你不是使用GitHub授权注册的账号,那么你有可能收到如下提示,这是提醒你没有可用的域名空间(就是你的Java包的groupid)授权,如果你有域名可以参考它的教程进行配置:https://central.sonatype.org/register/namespace/#adding-a-namespace

上传到maven中央仓库 发布maven中央仓库_apache

使用Maven插件发布

最新的发布器前瞻版本支持通过Maven发布。为此,您需要配置项目以使用 central-publishing-maven-plugin

该插件并不会生成所有发布所必须的文件,因此您需要遵循构建 Javadoc 和源代码 .jar 文件以及 GPG 签名文件的文档。插件不会强制执行额外的元数据要求(特别是 POM 中的必需元素)。请参阅我们的要求文档以获取指导。
该插件会为捆绑包中的文件生成所需的校验和。请参阅下面的插件配置选项。默认情况下,会生成所有可接受的校验和(MD5、SHA1、SHA256 和 SHA512)。

  • pom.xml 中引入插件
<!-- sonatype 要求的插件 -->
    <build>
        <plugins>
            <!--sonatype发布插件-->
            <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>
                </configuration>
            </plugin>
            <!-- 生成source -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <id>oss</id>
                        <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>3.6.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- 工程文件自动签名-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-gpg-plugin</artifactId>
                <version>3.2.3</version>
                <executions>
                    <execution>
                        <id>sign-artifacts</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>sign</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
     </build>
  • 获取账号令牌
  • 上传到maven中央仓库 发布maven中央仓库_xml_02


  • 上传到maven中央仓库 发布maven中央仓库_apache_03

  • maven配置文件setting.xml 中配置令牌
<servers>
    <!-- 中央仓库 -->
    <server>
      <id>central</id>
      <username>{你的账户}</username>
      <password>{你的密码}</password>
    </server>
  </servers>

central 是 pom 中发布插件的 publishingServerId

上传到maven中央仓库 发布maven中央仓库_xml_04

  • 创建证书,最好设置密码保护
  • 证书创建完成后对齐右键-在服务器上发布
  • 发布完成后继续修改maven的setting.xml
</profiles>
   <profile>
        <id>gpg</id>
        <properties>
            <gpg.executable>D:\app\GnuPG\bin\gpg.exe</gpg.executable>
            <gpg.passphrase>{证书密码}</gpg.passphrase>
        </properties>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>gpg</activeProfile>
  </activeProfiles>

注意,其中gpg 在profile 和 activeProfiles 中必须对应上,不然不生效,gpg.executable 是你gpg.exe的安装路径,如果自动配置的环境变量没问题,只写个gpg也无所谓

  • 补充pom.xml 中必须的项
<url>{项目地址}</url>
  
  <description>{项目说明}</description>
  
  <issueManagement>
        <system>Github Issue</system>
        <url>{Issue 发布地址}</url>
  </issueManagement>
  
  <developers>
        <developer>
            <name>{开发者名}</name>
            <email>{开发者邮箱}</email>
        </developer>
  </developers>

   <!--源代码管理-->
   <scm>
       <connection>scm:git:{开源git拉取地址}</connection>
       <developerConnection>scm:git:{开发者git拉取地址}</developerConnection>
       <url>{git仓库地址}</url>
   </scm>

   <!--授权 (这是Apache License-2 根据你自己的开源协议来)-->
   <licenses>
       <license>
           <name>Apache License, Version 2.0</name>
           <url>http://www.apache.org/licenses/LICENSE-2.0</url>
           <distribution>repo</distribution>
       </license>
   </licenses>

完成后deploy即可,可在https://central.sonatype.com/publishing 查看检测进度或者失败原因