1. 程式人生 > >如何將自己的jar包釋出到mavan中央倉庫

如何將自己的jar包釋出到mavan中央倉庫

最近自己寫了一個關於閘道器限流的外掛,然後想著肯定會有很多兄弟也需要使用到,所以就想著把jar包上傳到Maven的中央倉庫上讓大家可以更方便的使用

現在咱們來看一下這個流程是什麼樣的呢。

  1. 首先呢,你得去這個網站去註冊一個賬號https://issues.sonatype.org/secure/Dashboard.jspa
  2. 賬號註冊以後登入網站,點選上方導航條最顯眼的那個create按鈕,然後按照下圖示示的進行填寫資訊
    1
    這裡有一個小小的坑就是如果你沒有域名的話groupId可以寫com.github.你的github名或者io.github.你的github名。如果你寫域名的話他會讓你確認域名是不是你的。大家可以按照我的辦法在你的網站做一個簡單的轉發,轉發的地址就是你要上傳的專案的github地址。
    1

    另外需要注意的是因為人家上班時間是我們的晚上,所以說如果不想等的時間長的話可以晚上操作。
  3. 當你看到你的專案狀態變成了RESOLVED時並且有一條評論是這樣的時候你就已經成功了一半了。
    1
  4. 接下來呢,去這個網站https://www.gpg4win.org/下載一個gpg
  5. 在你的Maven的seeting.xml中增加以下配置
1
2
3
4
5
6
7
8
9
10
11
12
<servers>
    <server>
      <id>sonatype-nexus-snapshots</id>
      <username>Sonatype 賬號</username>
      <password>Sonatype 密碼</password>
    </server>
    <server>
      <id>sonatype-nexus-staging</id>
      <username>Sonatype 賬號</username>
      <password>Sonatype 密碼</password>
    </server>
  </servers>

在專案的pom檔案中增加配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<name>syj-ratelimit</name>
	<description>a project about ratelimit</description>
	<url>http://zhixiang.org.cn</url>
<licenses>
    <license>
        <name>The ApacheSoftware License, Version 2.0</name>
        <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
        <distribution>repo</distribution>
    </license>
</licenses>
<scm>
    <tag>master</tag>
    <url>https://github.com/2388386839/syj-ratelimit.git</url>
    <connection>scm:git:https://github.com/2388386839/syj-ratelimit.git</connection>
    <developerConnection>scm:git:https://github.com/2388386839/syj-ratelimit.git</developerConnection>
</scm>
<developers>
    <developer>
        <name>ShiYuJun</name>
        <email>
[email protected]
</email> <organization>xxx</organization> </developer> </developers> <parent> <groupId>org.sonatype.oss</groupId> <artifactId>oss-parent</artifactId> <version>7</version> </parent>
  1. 在專案中執行命令:gpg --gen-key,在執行命令的時候會讓你輸入一些資訊,按照提示輸入就行。記住其中的passphrase就行了。

  2. 然後執行命令 mvn clean deploy -P sonatype-oss-release -Darguments=“gpg.passphrase=剛才讓你記住的東西”

  3. 這個時候你的jar包已經上傳到了maven的一個倉庫,不過別人還不能用。

  4. 這個時候你應該把你剛才用來加密的金鑰上傳到一個公共的地方供別人來校驗

    執行命令 gpg --list-keys
    然後你會看到有一個pub的值,大概是這樣的CE2DF6AC
    然後執行下方命令

1
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys pub CE2DF6AC
  1. 訪問地址https://oss.sonatype.org/#stagingRepositories,用你一開始的時候註冊的使用者名稱登入。在最下方應該能找到你剛剛提交的jar包。
  2. 選擇之後先close,close之後如果沒問題的話接著點選release。當然,如果有問題的話可能就是你上傳過程中出現了什麼問題,如果解決不了歡迎在部落格下方提問。
  3. 此時在訪問https://issues.sonatype.org/secure/Dashboard.jspa會發現多了一條評論
    1


    現在大功告成。全世界的開發者都可以在maven中引入你的jar包了
     

歡迎來我的github看一下我的第一個開源專案寫的如何https://github.com/shiyujun/syj-ratelimit

本文出自http://zhixiang.org.cn,轉載請保留。