如何發佈Maven依賴到中央倉庫

平時咱們都是從Maven中央倉庫下載依賴,若是咱們想發佈咱們本身寫的Maven依賴到中央倉庫供別人下載使用應該怎麼辦?這裏以上傳本身寫的simian-maven-plugin(https://github.com/jiangxincode/simian-maven-plugin)爲例說明如何發佈Maven依賴到中央倉庫。html

開始以前,請注意幾個地址: java

一、工單管理:https://issues.sonatype.org/secure/Dashboard.jspagit

說明:註冊帳號、建立和管理issue,依賴的發佈是以解決issue的方式起步的。github

二、緩存倉庫:https://oss.sonatype.org/#stagingRepositoriesredis

說明:當咱們發佈非SNAPSHOT版本時,會先將依賴上傳到該過渡倉庫,以後才能正式發佈到中央倉庫。apache

建立工單

訪問工單管理的網址:https://issues.sonatype.org/secure/Dashboard.jspawindows

若是以前沒有註冊過sonatype帳號,須要先註冊一個帳號,註冊過程本文不在贅述,務必記住用戶名和密碼。 緩存

 Create Issue 填寫內容說明: 安全

===Step 1=== 服務器

Project:Community Support - Open Source Project Repository Hosting

Issue Type:New Project

===Step 2===

Summary:依賴名稱,如:simian-maven-plugin

Group Id:對應你的依賴的groupId,如com.github.jiangxincode

Project URL:項目站點,如:https://github.com/jiangxincode/simian-maven-plugin

SCM url:項目源碼倉庫,如:https://github.com/jiangxincode/simian-maven-plugin.git

 

其餘內容不用填寫,建立Issue後須要等待一小段時間,Sonatype的工做人員審覈處理,速度仍是很快的,通常一個工做日之內,當Issue的Status變爲RESOLVED後,就能夠進行下一步操做了。

 

配置pom.xml

在工程的pom.xml文件中,引入Sonatype官方的一個通用配置oss-parent,這樣作的好處是不少pom.xml的發佈配置不須要本身配置了:

<parent>
    <groupId>org.sonatype.oss</groupId>
    <artifactId>oss-parent</artifactId>
    <version>7</version>
</parent>

並增長Licenses、SCM、Developers信息:

    <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>
        <tag>master</tag>
        <url>git@github.com:jiangxincode/simian-maven-plugin.git</url>
        <connection>scm:git:git@github.com:jiangxincode/simian-maven-plugin.git</connection>
        <developerConnection>scm:git:git@github.com:jiangxincode/simian-maven-plugin.git</developerConnection>
    </scm>
    <developers>
        <developer>
            <name>Aloys</name>
            <email>jiangxinnju@163.com</email>
            <organization>Github</organization>
        </developer>
    </developers>

修改maven配置文件settings.xml,在servers中增長server配置。

  <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>

配置gpg-key

首先須要使用gpg工具生成一對密鑰,以Windows爲例,若是你安裝了Git(https://git-scm.com/)則已經有了gpg工具,若是沒有能夠單獨下載gpg4win(https://www.gpg4win.org/download.html)。以Git自帶的gpg工具爲例,生成密鑰方式以下:

jiang@windows MINGW64 /d/temp/Java/simian-maven-plugin (master)
$ gpg --gen-key
gpg (GnuPG) 1.4.22; Copyright (C) 2015 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
 
gpg: directory `/c/Users/jiang/.gnupg' created
gpg: new configuration file `/c/Users/jiang/.gnupg/gpg.conf' created
gpg: WARNING: options in `/c/Users/jiang/.gnupg/gpg.conf' are not yet active during this run
gpg: keyring `/c/Users/jiang/.gnupg/secring.gpg' created
gpg: keyring `/c/Users/jiang/.gnupg/pubring.gpg' created
Please select what kind of key you want:
   (1) RSA and RSA (default)
   (2) DSA and Elgamal
   (3) DSA (sign only)
   (4) RSA (sign only)
Your selection?
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048)
Requested keysize is 2048 bits
Please specify how long the key should be valid.
         0 = key does not expire
      <n>  = key expires in n days
      <n>w = key expires in n weeks
      <n>m = key expires in n months
      <n>y = key expires in n years
Key is valid for? (0)
Key does not expire at all
Is this correct? (y/N) y
 
You need a user ID to identify your key; the software constructs the user ID
from the Real Name, Comment and Email Address in this form:
    "Heinrich Heine (Der Dichter) <heinrichh@duesseldorf.de>"
 
Real name: Aloys
Email address: jiangxinnju@163.com
Comment: for sonatype
You selected this USER-ID:
    "Aloys (for sonatype) <jiangxinnju@163.com>"
 
Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? C
Comment: JiangXin
You selected this USER-ID:
    "Aloys (JiangXin) <jiangxinnju@163.com>"
 
Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O
You need a Passphrase to protect your secret key.    **********
 
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
........+++++
...........+++++
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
.+++++
+++++
gpg: /c/Users/jiang/.gnupg/trustdb.gpg: trustdb created
gpg: key 2AB935A0 marked as ultimately trusted
public and secret key created and signed.
 
gpg: checking the trustdb
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
pub   2048R/2AB935A0 2018-10-28
      Key fingerprint = A5C1 8750 A311 0057 671C  D8B7 A14C A7F2 2AB9 35A0
uid                  Aloys (JiangXin) <jiangxinnju@163.com>
sub   2048R/94B6D362 2018-10-28

部署依賴到stagingRepositories

執行以下命令部署插件:

mvn clean deploy -P sonatype-oss-release

若是使用eclipse的mvn插件發佈的話,配置以下: 

 

若是發佈成功,就能夠到stagingRepositories(https://oss.sonatype.org/#stagingRepositories)中查看了。

發佈到中央倉庫

進入stagingRepositories查看發佈好的構件,點擊左側的Staging Repositories,通常最後一個就是剛剛發佈的依賴,此時的構件狀態爲open。 

選中剛纔發佈的構件,並點擊上方的Close–>Confirm,在下邊的Activity選項卡中查看狀態,當狀態變成closed後,執行Release–>Confirm,並在下邊的Activity選項卡中查看狀態,成功後構件自動刪除,一小段時間(約10分鐘)後便可同步到maven的中央倉庫,再過1-2個小時就能夠搜索到該依賴了(https://search.maven.org)。

其它問題

配置pom.xml時,爲何沒有配置倉庫的地址?

爲何我只是在settings.xml中配置了id,又沒有配置這個id對應的服務器地址,Maven是若是找到我想上傳的服務器地址?

還記的以前咱們在pom.xml中配置的<parent>節點麼?這個節點中有以下內容:

    <distributionManagement>
        <snapshotRepository>
            <id>sonatype-nexus-snapshots</id>
            <name>Sonatype Nexus Snapshots</name>
            <url>${sonatypeOssDistMgmtSnapshotsUrl}</url>
        </snapshotRepository>
        <repository>
            <id>sonatype-nexus-staging</id>
            <name>Nexus Release Repository</name>
            <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
        </repository>
    </distributionManagement>

 

咱們的項目至關於繼承了這個配置,因此Maven根據咱們在settings.xml中配置的id找到pom.xml中配置的url來上傳依賴。

其實咱們徹底能夠不配置<parent>節點,而是根據實際須要本身配置對應的內容,只不過麻煩些,對新手不太友好。

我上傳的依賴會自動簽名麼?

會的,別忘了以前配置的<parent>,裏面有以下插件:

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

在執行mvn clean deploy -P sonatype-oss-release時每次須要咱們輸入密鑰的密碼,比較麻煩,你能夠在settings.xml或者pom.xml的作以下配置:

<profile>
      <id>sonatype-oss-release</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <gpg.executable>gpg</gpg.executable>
        <gpg.passphrase>密鑰的密碼</gpg.passphrase>
      </properties>
    </profile>
</profiles>

此時執行mvn clean deploy -P sonatype-oss-release就不用輸入密碼了。可是此時文件中會有密碼存儲,不太安全,不推薦。建議隨便配個字符串,而後在執行命令時覆蓋:

mvn clean deploy -P sonatype-oss-release -Darguments="gpg.passphrase=password"

安全和方便老是不可兼得,各取所需。

如何配置生成javadoc和sources?

只要按照以前的章節配置了<parent>節點,就不須要單獨配置,由於已經繼承了以下內容:

若是不符合想要的效果,能夠在pom.xml中覆蓋修改。

Snapshot/Staging/Release倉庫的地址分別是什麼?

其中sonatype-nexus-snapshots和sonatype-nexus-staging倉庫的地址能夠在<parent>節點中查看:

sonatype-nexus-snapshots: https://oss.sonatype.org/content/repositories/snapshots/

sonatype-nexus-staging: https://oss.sonatype.org/service/local/staging/deploy/maven2/

sonatype-nexus-release: https://repo1.maven.org/maven2

Maven怎麼知道把個人依賴上傳到哪一個倉庫?

maven會根據模塊的版本號中是否帶有-SNAPSHOT來判斷是快照版本仍是正式版本。若是是快照版本,那麼在mvn deploy時會自動發佈到快照版本庫中,而使用快照版本的模塊,在不更改版本號的狀況下,直接編譯打包時,maven會自動從鏡像服務器上下載最新的快照版本。若是是正式發佈版本,那麼在mvn deploy時會自動發佈到正式版本庫中,而使用正式版本的模塊,在不更改版本號的狀況下,編譯打包時若是本地已經存在該版本的模塊則不會主動去鏡像服務器上下載。

因此,咱們在開發階段,能夠將公用庫的版本設置爲快照版本,而被依賴組件則引用快照版本進行開發,在公用庫的快照版本更新後,咱們也不須要修改pom文件提示版本號來下載新的版本,直接mvn執行相關編譯、打包命令便可從新下載最新的快照庫了,從而也方便了咱們進行開發。

每次發佈,若是要切換髮布倉庫都要修改版本號,有沒有辦法簡化?

在pom.xml中作以下配置:

    <groupId>com.github.jiangxincode</groupId>
    <artifactId>simian-maven-plugin</artifactId>
    <version>${project.release.version}</version>
    <packaging>maven-plugin</packaging>
    <name>Simian Maven Plugin</name>
    <url>https://github.com/jiangxincode/simian-maven-plugin</url>
    <properties>
        <project.release.version>0.0.5-SNAPSHOT</project.release.version>
    </properties>
    <profiles>
        <profile>
            <id>sonatype-oss-release</id>
        <properties>
            <project.release.version>0.0.5</project.release.version>
        </properties>
        </profile>
    </profiles>

首先咱們看到pom文件中version的定義是採用佔位符的形式,這樣的好處是能夠根據不一樣的profile來替換版本信息。

若是在發佈時使用mvn deploy -P release 的命令,那麼會自動使用0.0.5做爲發佈版本,那麼根據maven處理snapshot和release的規則,因爲版本號後不帶-SNAPSHOT故當成是正式發佈版本,會被髮布到release倉庫;

若是發佈時使用mvn deploy命令,那麼就會使用默認的版本號0.0.5-SNAPSHOT,此時maven會認爲是快照版本,會自動發佈到快照版本庫。

部署到snapshot倉庫時,jar包會帶上時間戳,怎麼辦?

這不要緊,maven會自動取相應版本最新的jar包。

提交到release倉庫是,報錯怎麼辦?

報錯信息以下:

Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy) on project simian-maven-plugin: Failed to deploy artifacts: Could not transfer artifact...from/to release...

這是由於elease的部署策略是【disable redeploy】,不容許覆蓋更新組件。修改一下版本號,再提交便可。

如何在Github項目中添加maven版本badge?

若是你想達到下圖的效果,只須要在README.md中添加以下內容,注意根據實際狀況修改:

 

[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.jiangxincode/simian-maven-plugin/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.jiangxincode/simian-maven-plugin)

如何上傳非開源Jar包到Maven中央倉庫

假設咱們的groupId爲com.github.jiangxincode,artifact爲simian,版本爲2.5.10。那麼首先咱們會有一個simian-2.5.10.jar,而後新建一個README文件和License文件,內容能夠爲空也能夠根據實際狀況填寫,利用以下命令打包成simian-2.5.10-sources.jar:

jar -cvf simian-2.5.10-javadoc.jar license.pdf README

同理生成simian-2.5.10-sources.jar,那麼咱們如今有以下三個包了:

  • simian-2.5.10.jar
  • simian-2.5.10-sources.jar
  • simian-2.5.10-javadoc.jar

而後咱們須要編輯一個pom文件,名稱爲simian-2.5.10.pom:

<project
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.github.jiangxincode</groupId>
    <artifactId>simian</artifactId>
    <version>2.5.10</version>
    <packaging>jar</packaging>
    <name>Simian</name>
    <description>Simian (Similarity Analyser) identifies duplication in Java, C#, C, C++, COBOL, Ruby, JSP, ASP, HTML, XML, Visual Basic, Groovy source code and even plain text files. In fact, simian can be used on any human readable files such as ini files, deployment descriptors, you name it.</description>
    <url>http://www.harukizaemon.com/simian/index.html</url>
    <licenses>
        <license>
            <name>Simian Software License Agreement</name>
            <url>http://www.harukizaemon.com/simian/license.pdf</url>
        </license>
    </licenses>
    <scm>
        <url>git@harukizaemon.com:simian/simian-git.git</url>
        <connection>scm:git:git@harukizaemon.com:simian/simian-git.git</connection>
        <developerConnection>scm:git:git@harukizaemon.com:simian/simian-git.git</developerConnection>
    </scm>
    <developers>
        <developer>
            <name>haruki_zaemon</name>
            <email>haruki_zaemon@mac.com</email>
        </developer>
    </developers>
</project>

以上pom文件根據實際內容填寫,注意groupId,artifactId,version不能有錯誤,其次是packaging要填jar。scm節點中填寫一個git地址,和代碼不要緊也行。

完成這一步後咱們有四個文件分別爲:

  • simian-2.5.10.jar
  • simian-2.5.10-sources.jar
  • simian-2.5.10-javadoc.jar
  • simian-2.5.10.pom

而後依次運行gpg命令簽名這四個文件,以pom文件爲例:

gpg -ab simian-2.5.10.pom

而後會獲得對應的asc文件,最後文件列表爲

  • simian-2.5.10.jar
  • simian-2.5.10.jar.asc
  • simian-2.5.10-sources.jar
  • simian-2.5.10-sources.jar.asc
  • simian-2.5.10-javadoc.jar
  • simian-2.5.10-javadoc.jar.asc
  • simian-2.5.10.pom
  • simian-2.5.10.pom.asc

接下來須要把八個文件打成一個bundle.jar,命令示例:

jar -cvf bundle.jar simian-2.5.10*

打開並登錄https://oss.sonatype.org/#welcome,點擊Staging Upload選項

點擊Staging Upload後,在Upload Mode後有個下拉選框,選擇Artifact bundle,而後select bundle,找到以前生成的bundle.jar,點擊上傳按鈕。

 

上傳後會在上圖的Staging Repositories中有顯示。以後的步驟就不在贅述了,和上傳開源依賴是徹底同樣的。

參考內容

Github開源Java項目(IJPay)上傳到Maven Central 詳細介紹:https://blog.csdn.net/zyw_java/article/details/77802615

 

歡迎和你們交流技術相關問題:

郵箱: jiangxinnju@163.com

博客園地址: <http://www.cnblogs.com/jiangxinnju>

GitHub地址: <https://github.com/jiangxincode>

知乎地址: <https://www.zhihu.com/people/jiangxinnju>

相關文章
相關標籤/搜索