这篇文章教会你把个人开发的jar包发布到maven中央仓库

2次阅读

共计 7846 个字符,预计需要花费 20 分钟才能阅读完成。

作者:小傅哥
博客:https://bugstack.cn – 原创系列专题案例

沉淀、分享、成长,让自己和他人都能有所收获!????

前言介绍

(19 年 12 月)最近想基于 Spring Boot 做个开源共享项目,开发一个分布式任务 DcsSchedule 中间件可以把 Schedule 增强。那么遇到一个问题希望把 Jar 包发包到 Maven 中央仓库,这样需要使用的用户就可以直接引入。

发布准备

内容 备注
1 申请 Github 帐号: https://github.com 用于上传开源代码:https://github.com/fuzhengwei…
2 GPG 生成密钥工具: https://gpg4win.org/download…. 在后续流程中下载安装生成密钥并上传置服务器,本文使用的服务器是:hkp://keyserver.ubuntu.com:80
3 工单系统: https://issues.sonatype.org 负责申请上传资格及完成第一次上传,后续更新不需要使用,相当于一个启动装置
4 构件仓库: https://oss.sonatype.org 上传的 jar 包会先存放到这里,在这里进行 Release 后即可发布到 maven 中央仓库,也可以本地设置自动发布
5 镜像仓库: http://search.maven.org 最终成功发布的 jar 可以在这里搜到
6 Maven 仓库:https://mvnrepository.com 经过几个小时耐心的等待会在 Maven 仓库中搜到
7 阿里云仓库:https://maven.aliyun.com 阿里云的仓库会同步的快一些
8 个人域名:https://bugstack.cn 这里主要用于工单资格验证(Add a TXT record to your DNS referencing this JIRA ticket: OSSRH-53637 (Fastest))

跟着节奏

1. 下载安装 Gpg 生成密钥

我们需要一个 GPG 环境,用来对上传的文件进行加密和签名,保证你的 jar 包不被篡改

1991 年,程序员 Phil Zimmermann 为了避开政府监视,开发了加密软件 PGP。这个软件非常好用,迅速流传开来,成了许多程序员的必备工具。但是,它是商业软件,不能自由使用。所以,自由软件基金会决定,开发一个 PGP 的替代品,取名为 GnuPG。这就是 GPG 的由来。

  1. 下载地址:https://gpg4win.org/download.html
  2. 下载完成后直接安装即可,比较傻瓜式安装很简单,记得选中文(如果你英文硬也可以不选)
  3. 生成密钥(可以使用命令行生成,也可以直接在操作界面生成)

    1. 文件 > 新建密钥对(Ctrl+N) — 创建个人 OpenPGP 密钥对

  1. 填写个人信息姓名和邮箱 并点击到 新建

  1. 填写密钥密码

  1. 将公钥上传到目录服务{如果上传失败,则通过:设置(S)-> 配置 Kleopatra(C),修改 OpenPGP 密钥服务器为:hkp://keyserver.ubuntu.com:80}

2. 工单系统帐号注册 issues.sonatype

1. 注册地址:https://issues.sonatype.org/s…

2. 创建工单

  • 项目:Community Support – Open Source Project Repository Hosting
  • 概要:发布 Jar 的名称
  • 描述:非必填,最好描述清晰
  • Group Id:org.itatack.middleware & 和你的域名有关系,因为后续需要使用域名验证
  • Project URL:Github 项目站点(https://github.com/fuzhengwei…
  • SCM url:源码仓库(https://github.com/fuzhengwei…

3. 配合人工审核

  1. 当创建完工单后,会收到信息反馈(国外与我们有时间差,半夜的时候他们审核的更快);

    
    ```java
    Do you own the domain itstack.org? If so, please verify ownership via one of the following methods:
    
    Add a TXT record to your DNS referencing this JIRA ticket: OSSRH-53637 (Fastest)
    Setup a redirect to your Github page (if it does not already exist)
    If you do not own this domain, please read:
    http://central.sonatype.org/pages/choosing-your-coordinates.html
    You may also choose a groupId that reflects your project hosting, in this case, something like io.github.fuzhengwei or com.github.fuzhengwei
    
    Would you like to use a free managed security reporting service (recommended)?
    Put https://hackerone.com/central-security-project/reports/new as your project's security issue reporting URL. We'll take care of the rest.
    For more details on the Central Security Project, visit https://www.sonatype.com/central-security-project
    ```
    ![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly91c2VyLWdvbGQtY2RuLnhpdHUuaW8vMjAxOS8xMi83LzE2ZWRmMGI3ZmZjYTAzZWM?x-oss-process=image/format,png)
  2. 配置域名验证签名;TXT 指向问题域:https://issues.sonatype.org/b…

    ![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly91c2VyLWdvbGQtY2RuLnhpdHUuaW8vMjAxOS8xMi83LzE2ZWRmMGI3ODc5YjIzNGQ?x-oss-process=image/format,png)
  3. 在域名验证截图,回复到问题下,人工审核会进行验证处理

    ![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly91c2VyLWdvbGQtY2RuLnhpdHUuaW8vMjAxOS8xMi83LzE2ZWRmMGI4MGI0NTFjOWU?x-oss-process=image/format,png)
  4. 验证成功后,会收到邮件回复,也可以在 issues 看到{意思就说去发布你的 Jar 吧宝贝,发布完告诉我一下(来这里回复下,我就让你用了)}

     org.itstack.middleware has been prepared, now user(s) fuzhengwei can:
     * Deploy snapshot artifacts into repository https://oss.sonatype.org/content/repositories/snapshots
     * Deploy release artifacts into the staging repository https://oss.sonatype.org/service/local/staging/deploy/maven2
     * Release staged artifacts into repository 'Releases'
    please comment on this ticket when you promoted your first release, thanks
   ```
  1. 接下来等待发布 Jar 包成功后,到这里回复并收到反馈,如下(证明你成功了!);

    Central sync is activated for org.itstack.middleware. After you successfully release, your component will be published to Central, typically within 10 minutes, though updates to search.maven.org can take up to two hours.

3. 配置 Maven Settings.xml

1. Maven Settings.xml 配置,在 servers 中添加;

<server>
    <id>sonatype-nexus-snapshots</id>
    <username>https://issues.sonatype.org 的账号 </username>
    <password>https://issues.sonatype.org 的密码 </password>
</server>
<server>
    <id>sonatype-nexus-staging</id>
    <username>https://issues.sonatype.org 的账号 </username>
    <password>https://issues.sonatype.org 的密码 </password>
</server>
<server>
    <id>ossrh</id>
    <username>https://issues.sonatype.org 的账号 </username>
    <password>https://issues.sonatype.org 的密码 </password>
</server>

2. 为了更快的加载 Jar,在 mirrors 中配置了阿里云仓库;

<mirror>
  <id>alimavenrepository</id>
  <name>aliyun maven repository</name>
  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
  <mirrorOf>central</mirrorOf>        
</mirror>

3. 密钥配置,在 profile 中添加 ossrh

<profile>
    <id>ossrh</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
        <gpg.executable>D:/Program Files (x86)/GnuPG/bin/gpg.exe</gpg.executable>
        <gpg.passphrase> 上面生成的密钥密码:bugstack.cn</gpg.passphrase>
        <gpg.homedir>{找到 dir:cmd->gpg --list-key}C:/Users/fuzhengwei/AppData/Roaming/gnupg</gpg.homedir>
    </properties>
</profile>

4. 配置 POM 文件

1. 在 pom 文件里添加 scm、licenses、developers、distributionManagement

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

<scm>
    <url>https://github.com/fuzhengwei/schedule-spring-boot-starter</url>
    <connection>https://github.com/fuzhengwei/schedule-spring-boot-starter.git</connection>
    <developerConnection>https://github.com/fuzhengwei/schedule-spring-boot-starter</developerConnection>
</scm>

<developers>
    <developer>
        <name>fuzhengwei</name>
        <email>184172133@qq.com</email>
        <url>https://github.com/fuzhengwei/schedule-spring-boot-starter</url>
    </developer>
</developers>

<distributionManagement>
    <snapshotRepository>
        <id>ossrh</id>
        <url>https://oss.sonatype.org/content/repositories/snapshots</url>
    </snapshotRepository>
</distributionManagement>

2. 配置编译发布信息 build,gpg 相关插件

  • maven-source-plugin 用来生成 Source Jar 文件
  • maven-javadoc-plugin 用来生成 javadoc 文档
  • maven-gpg-plugin 用来对工程文件进行自动签名
  • nexus-staging-maven-plugin 用来将工程发布到中央仓库,另外注意生成 javadoc 文档时需要指定关闭 doclint,不然可能因为使用了不规范的 javadoc 注解而导致失败,完整配置如下
<!-- 发布 Jar 到 Maven 仓库 Begin -->
<!-- 生成 Source jar 文件 -->
<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,关闭 doclint, 避免注解检查不通过 -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-javadoc-plugin</artifactId>
    <version>2.9.1</version>
    <executions>
        <execution>
            <id>attach-javadocs</id>
            <goals>
                <goal>jar</goal>
            </goals>
            <configuration>
                <additionalparam>-Xdoclint:none</additionalparam>
            </configuration>
        </execution>
    </executions>
</plugin>
<!--Maven 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>
<!--Nexus Staging Maven 插件是将组件部署到 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://oss.sonatype.org/</nexusUrl>
        <autoReleaseAfterClose>true</autoReleaseAfterClose>
    </configuration>
</plugin>
<!-- release plugin, 用于发布到 release 仓库部署插件 -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-release-plugin</artifactId>
    <version>2.4.2</version>
</plugin>
<!-- 发布 Jar 到 Maven 仓库 End -->

4. 执行发布;Idea Maven -> Lifecycle -> Deploy

  1. 接下来开发发布 Jar 包,中间会提示输入密钥生成时候的密码
  2. https://oss.sonatype.org 查看发布内容
  3. 到上面 ” 配合人工审核 ”, 按照说明提交发布成功信息,验证成功后会收到回复,如下;

    Central sync is activated for org.itstack.middleware. After you successfully release, your component will be published to Central, typically within 10 minutes, though updates to search.maven.org can take up to two hours.
  4. https://search.maven.org 搜索版本信息
  5. https://maven.aliyun.com 阿里云仓库同步较快,可以查看

综上总结

  • 整体流程还是很长的,如果第一次尝试去弄,嗯,你可能有一个不眠夜
  • 中间可能会遇到各种异常错误,包括密钥、打包、发版等等,注意仔细阅读本文细节以及自己多次尝试,总归是会成功的
  • 比较常识性的问题;同一个 RELEASE 版本只能上传一次否则会失败、老外真的是半夜回复比较快因为咱们半夜他们正好天亮了

推荐阅读

  • 1. 重学 Java 设计模式:实战工厂方法模式(多种类型商品发奖场景)
  • 2. 重学 Java 设计模式:实战建造者模式(装修物料组合套餐选配场景)
  • 3. 重学 Java 设计模式:实战原型模式(多套试每人题目和答案乱序场景)
  • 4. 重学 Java 设计模式:实战桥接模式(多支付渠道「微信支付宝」与多支付模式「刷脸、指纹」场景)
  • 5. 重学 Java 设计模式:实战组合模式(营销差异化人群发券决策树引擎搭建场景)
  • 6. 重学 Java 设计模式:实战外观模式「基于 SpringBoot 开发门面模式中间件,统一控制接口白名单场景」
正文完
 0