咱們常常會在公司內部寫一些 Java 組件/工具,經過安裝到本地或者發佈到公司的 maven 私服,其餘同事就能夠在 pom.xml
文件引入你的組件了。 可是若是你想把這個組件(工具)開源,給更多的小夥伴使用,那你就得把你的構件發佈到 Maven 中央倉庫。html
本文詳細介紹如何發佈本身的 maven 構件到中央倉庫。java
文章導讀git
maven 中央倉庫是有一個叫作 Sonatype 的公司在維護的,在發佈構件以前須要 註冊一個帳號, 記住本身的用戶名和密碼,之後要用。github
註冊地址:https://issues.sonatype.org/secure/Signup!default.jsparedis
同時,還要記住一個地址,未來在查詢本身所發佈構件狀態和進行一些操做的時候要使用spring
https://oss.sonatype.org/apache
提交發布申請提交申請,在這裏是建立一個issue的形式,建立地址:ubuntu
https://issues.sonatype.org/secure/CreateIssue.jspa?issuetype=21&pid=10134服務器
在填寫issue信息的時候,有一些須要注意的地方:mybatis
提交以後須要等工做人員離開確認。若是你填寫的是本身的域名,工做人員會問你是否是真的是本身的域名,你須要向他證實你確實擁有這個域名。 通常就是讓你作個 TXT
解析或者作個重定向到你的 github Pages
頁面。
下面是個人 issue 地址,不知道怎麼填的同窗請直接參考個人:
https://issues.sonatype.org/browse/O***H-49428
須要說明的是,因爲我用是 .org
域名,在國內自 2018年起,org
域名沒法實名認證,也不能備案,致使個人域名沒法解析,因此我不得不提供 域名證書,解析證實這些,工做人員纔給我審覈經過。
因此,若是你也是 org 域名,議你先把域名從阿里雲這些國內運營商轉出到國外的域名運營商。不然你也會沒法解析
審覈經過之後,你會收到以下回復:
上傳構件的時候須要 GPG 祕鑰進行簽名,因此咱們須要先生成 GPG 祕鑰:
gpg --gen-key gpg (GnuPG) 2.2.4; Copyright (C) 2017 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. 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: yangjian Email address: rock@qq.com You selected this USER-ID: "yangjian <rock@qq.com>" Change (N)ame, (E)mail, or (O)kay/(Q)uit? O generator a better chance to gain enough entropy. gpg: key 9E81A728737D0E7F marked as ultimately trusted gpg: directory '/home/rock/.gnupg/openpgp-revocs.d' created gpg: revocation certificate stored as '/home/yangjian/.gnupg/openpgp-revocs.d/52433C774B2B9FFEFB722F269E81A728737D0E7F.rev' public and secret key created and signed. pub rsa3072 2019-06-16 [SC] [expires: 2021-06-15] 52433C774B2B9FFEFB722F269E81A728737D0E7F uid yangjian <rock@qq.com> sub rsa3072 2019-06-16 [E] [expires: 2021-06-15]
須要輸入姓名、郵箱等字段,其它字段可以使用默認值,此外,還須要輸入一個 Passphase,至關於一個密鑰庫的密碼, 必定不要忘了,也不要告訴別人, 最好記下來,由於後面發佈構件的時候會用到。
Note: 生成祕鑰後你須要把你公鑰發佈到公鑰服務器託管,
sonatype
支持不少服務器,如ubuntu
,keyservers
等。
gpg --keyserver hkp://keyserver.ubuntu.com --send-keys 9E81A728737D0E7F
發佈以後你可使用下面的命令驗證是否發佈成功
gpg --keyserver hkp://keyserver.ubuntu.com --recv-keys 9E81A728737D0E7F
若是看到相似下面的輸出,則說明已經發布成功了
gpg: key 9E81A728737D0E7F: "RockYang <yangjian102621@gmail.com>" not changed gpg: Total number processed: 1 gpg: unchanged: 1
找你所使用的 maven 的配置文件(通常在 ~/.m2/settings.xml
),在配置文件中找到節點, 這個節點默認是註釋了的, 咱們就在這個註釋的外邊增長一個的配置以下:
<servers> <server> <id>oss</id> <username>用戶名</username> <password>密碼</password> </server> </servers>
這裏的 id 是未來要在 pom.xml 裏面使用的,每一個 server 的 id 都不一樣,用來區分你是要把構建發佈到哪一個倉庫, 用戶名和密碼就是在 Sonatype
上面註冊的用戶名和密碼。
爲了把構件發佈到中央倉庫, 咱們必須在 pom.xml 中配置一些關鍵信息,不然會影響發佈審覈。
這些信息包括:name,description,url,licenses,developers,scm 等基本信息。
下面貼上個人配置示例:
<groupId>org.rockyang</groupId> <artifactId>mybatis-kits</artifactId> <version>1.5.1</version> <packaging>pom</packaging> <name>mybatis-kits</name> <description>MyBatis 加強工具包,實現了便捷的單表 CRUD,各類自定義條件查詢,以及物理分頁查詢</description> <url>https://gitee.com/blackfox/mybats-kits</url> <modules> <module>mybatis-kits-core</module> <module>mybatis-kits-spring-boot</module> <module>demos</module> </modules> <licenses> <license> <name>The Apache Software License, Version 2.0</name> <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> </license> </licenses> <developers> <developer> <name>RockYang</name> <email>yangjian102621@gmail.com</email> </developer> </developers> <scm> <connection>scm:https://gitee.com/blackfox/mybats-kits.git</connection> <developerConnection>scm:https://gitee.com/blackfox/mybats-kits.git</developerConnection> <url>https://gitee.com/blackfox/mybats-kits.git</url> </scm>
而後須要在 <build>
裏面增長 java-docs-plugin
和 maven-sourece-plugin
,這個是必需要增長的,分別用來爲你的代碼生成 Java Docs 和打包源碼。
<build> <plugins> <!-- packaging source code --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>${maven-source-plugin.version}</version> <executions> <execution> <id>attach-sources</id> <phase>verify</phase> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin> <!-- Javadoc --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>${maven-javadoc-plugin.version}</version> <!-- skip javadoc error --> <configuration> <failOnError>false</failOnError> <doclint>none</doclint> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
再增長一個 profile,名爲 oss,下面貼上個人,我這裏配置了兩個 profile, 一個是用來發布到公司內部 Nexus maven 私服的。
<profiles> <!-- local nexus repository for test --> <profile> <id>dev</id> <properties> <profiles.active>dev</profiles.active> </properties> <!-- configures for artifact deployment --> <distributionManagement> <repository> <id>releases</id> <name>Release Repository</name> <url>http://192.168.0.135:8081/repository/maven-releases</url> </repository> <snapshotRepository> <id>snapshots</id> <name>Snapshot Repository</name> <url>http://192.168.0.135:8081/repository/maven-snapshots/</url> </snapshotRepository> </distributionManagement> <activation> <!-- default active --> <activeByDefault>true</activeByDefault> </activation> </profile> <!-- Central maven repository --> <profile> <id>oss</id> <properties> <profiles.active>release</profiles.active> </properties> <distributionManagement> <snapshotRepository> <id>oss</id> <url>https://oss.sonatype.org/content/repositories/snapshots/</url> </snapshotRepository> <repository> <id>oss</id> <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url> </repository> </distributionManagement> <build> <plugins> <!-- Gpg Signature --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-gpg-plugin</artifactId> <version>1.6</version> <executions> <execution> <id>oss</id> <phase>verify</phase> <goals> <goal>sign</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles>
上傳構件到 OSS我這裏是把
dev
配置成默認發佈倉庫,因此給dev
profile 添加了<activeByDefault>true</activeByDefault>
配置。 並且你可能也注意到了,oss
profile 裏面我配置了maven-gpg-plugin
,這個是發佈到中央倉庫必須配置的,用來簽名,不然發佈會失敗。 而個人本地倉庫是設置爲不須要簽名的,因此發佈到本地倉庫的dev
profile 就不須要配置gpg
插件。
編輯完 pom.xml,咱們就能夠進行上傳、發佈了。在命令行進入項目pom.xml所在路徑,執行:
# 發佈到本地倉庫 mvn clean deploy # 發佈到中央倉庫 mvn clean deploy -P oss
在編譯打包完成以後,會彈出對話框,要你輸入 gpg
密鑰庫的密碼,這個密碼就是剛剛你建立 gpg
祕鑰時候輸入的密碼,輸入便可完成上傳。 固然有時候不會彈出輸入密碼的輸入框,只是提示須要輸入密碼,根據gpg插件的官網解釋,須要加上密碼做爲參數執行命令,即:
mvn clean deploy -P oss -Dgpg.passphrase=密碼
構建上傳以後須要在 OSS
系統中對操做進行確認,將構件發佈,進入 https://oss.sonatype.org/ 使用你的用戶名和密碼登錄以後:
Staging Repositories
,點擊,在右邊上面一點有一個輸入搜索框輸入你的 groupid 進行快速定位, 能夠發現這時你的構件狀態是 open
。close
按鈕,在彈出框中 confirm
, 這裏又須要校驗一次,稍後結果會經過郵箱通知。注意,你執行 release
成功以後,OSS
裏面就沒有這個構件了,緣由是被同步到 maven 中央倉庫以後,這條記錄就被從 OSS
刪除了。
回到 issue 系統,找到你的那個申請發佈構件的 issue,在下面回覆工做人員,說明構件已經發布,待工做人員確認後,會關閉這個issue。
一切完成後並不能夠立刻就使用你所發佈的構件,得等系統將你的構件同步到中央倉庫以後纔可使用, 這個時間至少要2個小時, 而後就能夠在中央倉庫的搜索頁面 http://search.maven.org/ 搜到你的構件了。
你可能要問,要是每次發佈都這麼麻煩,豈不是要被折騰死,Sonatype 的審覈工做人員也受不了。
答案是:完成第一次的發佈以後,之後的版本發佈就會相對簡單多了。
之後的發佈流程:
添加自動發佈構建 maven 插件這裏須要注意幾點:
- 第一次成功以後,之後就可使用你的 groupid 發佈任何的構件了,只須要你的 groupid 沒有改變。
- 同一個構件同一個版本只能發佈一次,也就是說你能不覆蓋某個版本,另外,版本發佈以後就也能刪除。
儘管第一次之後的發佈流程相對於第一次發佈來講,已經大大簡化了,可是我可能仍是會以爲太麻煩了,有沒有一個工具能在我上傳以後自動給我發佈, 也省去了我每次去登陸 https://oss.sonatype.org/ 手動發佈了(講真,https://oss.sonatype.org/ 打開真不算快)。
答案是:還真有這麼一個插件,它就是: nexus-staging-maven-plugin
只須要在你的 pom.xml 的 build 配置中加上下面的配置:
<plugin> <groupId>org.sonatype.plugins</groupId> <artifactId>nexus-staging-maven-plugin</artifactId> <version>${nexus-staging.version}</version> <extensions>true</extensions> <configuration> <serverId>oss</serverId> <nexusUrl>https://oss.sonatype.org/</nexusUrl> <autoReleaseAfterClose>true</autoReleaseAfterClose> </configuration> </plugin>
一切問題迎刃而解了,之後你想要把你的構建發佈到中央倉庫,惟一要作的就是執行:
mvn clean deploy -P oss
以上就是關於發佈構件到 Maven 中央倉庫的全部操做流程,完整的 pom.xml 配置請參考:
參考連接
原文連接:https://www.r9it.com/20190701/maven-artifact-deploy.html