:notebook: 本文已歸檔到:「blog」php
爲了不重複造輪子,相信每一個 Java 程序員都想打造本身的腳手架或工具包(本身定製的每每纔是最適合本身的)。那麼如何將本身的腳手架發佈到中央倉庫呢?下面咱們將一步步來實現。html
(1)註冊 Sonatype 帳號java
發佈 Java 包到 Maven 中央倉庫,首先須要在 Sonatype 網站建立一個工單(Issues)。git
第一次使用這個網站的時候須要註冊本身的賬號(這個賬號和密碼須要記住,後面會用到)。程序員
(2)建立 Issuegithub
註冊帳號成功後,根據你 Java 包的功能分別寫上Summary
、Description
、Group Id
、SCM url
以及Project URL
等必要信息,能夠參見我以前建立的 Issue:OSSRH-36187。redis
建立完以後須要等待 Sonatype 的工做人員審覈處理,審覈時間仍是很快的,個人審覈差很少等待了兩小時。當 Issue 的 Status 變爲RESOLVED
後,就能夠進行下一步操做了。apache
說明:若是你的 Group Id 填寫的是本身的網站(個人就是這種狀況),Sonatype 的工做人員會詢問你那個 Group Id 是否是你的域名,你只須要在上面回答是就行,而後就會經過審覈。瀏覽器
(1)安裝 Gpg4winbash
Windows 系統,能夠下載 Gpg4win 軟件來生成密鑰對。
安裝後,執行命令 gpg --version 檢查是否安裝成功。
C:\Program Files (x86)\GnuPG\bin>gpg --version
gpg (GnuPG) 2.2.10
libgcrypt 1.8.3
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the exdunwu permitted by law.
Home: C:/Users/Administrator/AppData/Roaming/gnupg
Supported algorithms:
Pubkey: RSA, ELG, DSA, ECDH, ECDSA, EDDSA
Cipher: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH,
CAMELLIA128, CAMELLIA192, CAMELLIA256
Hash: SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224
Compression: Uncompressed, ZIP, ZLIB, BZIP2
複製代碼
(2)生成密鑰對
執行命令 gpg --gen-key
C:\Program Files (x86)\GnuPG\bin>gpg --gen-key
gpg (GnuPG) 2.2.10; Copyright (C) 2018 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the exdunwu permitted by law.
Note: Use "gpg --full-generate-key" for a full featured key generation dialog.
GnuPG needs to construct a user ID to identify your key.
Real name: Zhang Peng
Email address: forbreak@163.com
You selected this USER-ID:
"Zhang Peng <forbreak@163.com>"
Change (N)ame, (E)mail, or (O)kay/(Q)uit? O
複製代碼
說明:按照提示,依次輸入用戶名、郵箱。
(3)查看公鑰
C:\Program Files (x86)\GnuPG\bin>gpg --list-keys gpg: checking the trustdb gpg: marginals needed: 3 completes needed: 1 trust model: pgp gpg: depth: 0 valid: 2 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 2u gpg: next trustdb check due at 2020-11-05 C:/Users/Administrator/AppData/Roaming/gnupg/pubring.kbx -------------------------------------------------------- pub rsa2048 2018-11-06 [SC] [expires: 2020-11-06] E4CE537A3803D49C35332221A306519BAFF57F60 uid [ultimate] forbreak <forbreak@163.com> sub rsa2048 2018-11-06 [E] [expires: 2020-11-06] 複製代碼
說明:其中,E4CE537A3803D49C35332221A306519BAFF57F60 就是公鑰
(4)將公鑰發佈到 PGP 密鑰服務器
執行 gpg --keyserver hkp://pool.sks-keyservers.net --send-keys
發佈公鑰:
C:\Program Files (x86)\GnuPG\bin>gpg --keyserver hkp://pool.sks-keyservers.net --send-keys E4CE537A3803D49C35332221A306519BAFF57F60
gpg: sending key A306519BAFF57F60 to hkp://pool.sks-keyservers.net
複製代碼
注意:有可能出現 gpg: keyserver receive failed: No dat 錯誤,等大約 30 分鐘後再執行就不會報錯了。
(5)查看公鑰是否發佈成功
執行 gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys
查看公鑰是否發佈成功。
C:\Program Files (x86)\GnuPG\bin>gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys E4CE537A3803D49C35332221A306519BAFF57F60
gpg: key A306519BAFF57F60: "forbreak <forbreak@163.com>" not changed
gpg: Total number processed: 1
gpg: unchanged: 1
複製代碼
完成了前兩個章節的準備工做,就能夠將 jar 包上傳到中央倉庫了。固然了,咱們還要對 maven 作一些配置。
一份完整的 settings.xml 配置示例以下:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<pluginGroups>
<pluginGroup>org.sonatype.plugins</pluginGroup>
</pluginGroups>
<!-- 用戶名、密碼就是 Sonatype 帳號、密碼 -->
<servers>
<server>
<id>sonatype-snapshots</id>
<username>xxxxxx</username>
<password>xxxxxx</password>
</server>
<server>
<id>sonatype-staging</id>
<username>xxxxxx</username>
<password>xxxxxx</password>
</server>
</servers>
<!-- 使用 aliyun maven 倉庫加速下載 -->
<mirrors>
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Aliyun</name>
<url>http://maven.aliyun.com/nexus/condunwu/groups/public</url>
</mirror>
</mirrors>
<!-- gpg 的密碼,注意,這裏不是指公鑰 -->
<profiles>
<profile>
<id>sonatype</id>
<properties>
<gpg.executable>C:/Program Files (x86)/GnuPG/bin/gpg.exe</gpg.executable>
<gpg.passphrase>xxxxxx</gpg.passphrase>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>sonatype</activeProfile>
</activeProfiles>
</settings>
複製代碼
(1)添加 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>
<developers>
<developer>
<name>xxxxxx</name>
<email>forbreak@163.com</email>
<url>https://github.com/dunwu</url>
</developer>
</developers>
<scm>
<url>https://github.com/dunwu/dunwu</url>
<connection>git@github.com:dunwu/dunwu.git</connection>
<developerConnection>https://github.com/dunwu</developerConnection>
</scm>
複製代碼
(2)添加 distributionManagement 配置
<distributionManagement>
<snapshotRepository>
<id>sonatype-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>sonatype-staging</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
</repository>
</distributionManagement>
複製代碼
說明:
<snapshotRepository>
指定的是 snapshot 倉庫地址;<repository>
指定的是 staging (正式版)倉庫地址。須要留意的是,這裏的 id 須要和 settings.xml 中的<server>
的 id 保持一致。
(3)添加 profiles 配置
<profiles>
<profile>
<id>sonatype</id>
<build>
<plugins>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.7</version>
<extensions>true</extensions>
<configuration>
<serverId>sonatype-snapshots</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<failOnError>false</failOnError>
<quiet>true</quiet>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
複製代碼
按照上面的步驟配置完後,一切都已經 OK。
此時,使用 mvn clean deploy -P sonatype
命令就能夠發佈 jar 包到中央倉庫了:
說明:-P 參數後面的 sonatype 須要和 pom.xml 中
<profile>
的 id 保持一致,才能激活 profile。
工做中,Java 程序員開發的商用 Java 項目,通常不想發佈到中央倉庫,使得人人盡知。這時,咱們就須要搭建私服,將 maven 服務器部署在公司內部網絡,從而避免 jar 包流傳出去。怎麼作呢,讓咱們來一步步學習吧。
進入官方下載地址,選擇合適版本下載。
本人但願將 Nexus 部署在 Linux 機器,因此選用的是 Unix 版本。
這裏,若是想經過命令方式直接下載(好比用腳本安裝),能夠在官方歷史發佈版本頁面中找到合適版本,而後執行如下命令:
wget -O /opt/maven/nexus-unix.tar.gz http://download.sonatype.com/nexus/3/nexus-3.13.0-01-unix.tar.gz
tar -zxf nexus-unix.tar.gz
複製代碼
解壓後,有兩個目錄:
進入 nexus-3.13.0-01/bin 目錄,有一個可執行腳本 nexus。
執行 ./nexus
,能夠查看容許執行的參數,以下所示,含義可謂一目瞭然:
$ ./nexus
Usage: ./nexus {start|stop|run|run-redirect|status|restart|force-reload}
複製代碼
./nexus start
啓動成功後,在瀏覽器中訪問 http://<ip>:8081
,歡迎頁面以下圖所示:
點擊右上角 Sign in 登陸,默認用戶名/密碼爲:admin/admin123。
有必要提一下的是,在 Nexus 的 Repositories 管理頁面,展現了可用的 maven 倉庫,以下圖所示:
說明:
- maven-central - maven 中央庫(若是沒有配置 mirror,默認就從這裏下載 jar 包),從 repo1.maven.org/maven2/ 獲取資源
- maven-releases - 存儲私有倉庫的發行版 jar 包
- maven-snapshots - 存儲私有倉庫的快照版(調試版本) jar 包
- maven-public - 私有倉庫的公共空間,把上面三個倉庫組合在一塊兒對外提供服務,在本地 maven 基礎配置 settings.xml 中使用。
若是要使用 Nexus,還必須在 settings.xml 和 pom.xml 中配置認證信息。
一份完整的 settings.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<pluginGroups>
<pluginGroup>org.sonatype.plugins</pluginGroup>
</pluginGroups>
<!-- Maven 私服帳號信息 -->
<servers>
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
<!-- jar 包下載地址 -->
<mirrors>
<mirror>
<id>public</id>
<mirrorOf>*</mirrorOf>
<url>http://10.255.255.224:8081/repository/maven-public/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>zp</id>
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>zp</activeProfile>
</activeProfiles>
</settings>
複製代碼
在 pom.xml 中添加以下配置:
<distributionManagement>
<repository>
<id>releases</id>
<name>Releases</name>
<url>http://10.255.255.224:8081/repository/maven-releases</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Snapshot</name>
<url>http://10.255.255.224:8081/repository/maven-snapshots</url>
</snapshotRepository>
</distributionManagement>
複製代碼
注意:
<repository>
和<snapshotRepository>
的 id 必須和settings.xml
配置文件中的<server>
標籤中的 id 匹配。<url>
標籤的地址須要和 maven 私服的地址匹配。
若是要使用 settings.xml 中的私服配置,必須經過指定 -P zp
來激活 profile。
示例:
## 編譯並打包 maven 項目
$ mvn clean package -Dmaven.skip.test=true -P zp
## 編譯並上傳 maven 交付件(jar 包)
$ mvn clean deploy -Dmaven.skip.test=true -P zp
複製代碼