這篇文章教會你把我的開發的jar包發佈到maven中央倉庫

做者:小傅哥
博客:https://bugstack.cn - 原創系列專題案例html

沉澱、分享、成長,讓本身和他人都能有所收穫!😄

前言介紹

(19年12月)最近想基於Spring Boot作個開源共享項目,開發一個分佈式任務DcsSchedule中間件能夠把Schedule加強。那麼遇到一個問題但願把Jar包發包到Maven中央倉庫,這樣須要使用的用戶就能夠直接引入。java

發佈準備

內容 備註
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包不被篡改git

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. 當建立完工單後,會收到信息反饋(國外與咱們有時間差,半夜的時候他們審覈的更快);github

    ```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...spring

    ![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly91c2VyLWdvbGQtY2RuLnhpdHUuaW8vMjAxOS8xMi83LzE2ZWRmMGI3ODc5YjIzNGQ?x-oss-process=image/format,png)
  3. 在域名驗證截圖,回覆到問題下,人工審覈會進行驗證處理apache

    ![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly91c2VyLWdvbGQtY2RuLnhpdHUuaW8vMjAxOS8xMi83LzE2ZWRmMGI4MGI0NTFjOWU?x-oss-process=image/format,png)
  4. 驗證成功後,會收到郵件回覆,也能夠在issues看到{意思就說去發佈你的Jar吧寶貝,發佈完告訴我一下(來這裏回覆下,我就讓你用了)}ubuntu

    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版本只能上傳一次不然會失敗、老外真的是半夜回覆比較快由於我們半夜他們正好天亮了

推薦閱讀

相關文章
相關標籤/搜索