最近有個需要進行開源的類庫,小小的記錄一下過程以及過程中的問題,如果有不對的地方還請指出
首先我這里需要把我們的類庫傳至maven的中央倉庫
這里我們首先需要sonatype
一、Sonatype簡介
Sonatype是一個軟件管理工具提供商,致力於跟蹤和監控各個代碼模塊,Maven的中央倉庫就是由Sonatype出資維護的而不是由Apache進行維護的,所以我們要在Sonatype上進行申請上傳maven的資格來使用Maven倉庫托管服務
Sonatype OSSRH(OSS存儲庫托管)使用 Sonatype Nexus存儲庫管理器 為開源項目二進制文件提供存儲庫托管服務
OSSRH存儲庫的初始設置需要一些手動步驟和人工檢查,然后通常會修改您的部署過程以將組件放入OSSRH,這些都是一次性的步驟
Sonatype 官方介紹:https://www.sonatype.com/company
Sonatype OSSRH官方介紹以及使用說明:https://central.sonatype.org/pages/ossrh-guide.html
二、Sonatype注冊
Sonatype注冊地址(科學瀏覽更快些):https://issues.sonatype.org/secure/Signup!default.jspa
輸入信息即可進行注冊
二、Sonatype登錄
Sonatype登錄地址:https://issues.sonatype.org/login.jsp
三、Sonatype新建issue
點擊新建
填入信息
這里需要注意幾個問題,不然反復審核時間會非常漫長
- When choosing a groupId that reflects your project hosting, in this case, something like io.github.2020 or com.github.2020 would be correct. Also, please create a public repo called OSSRH-6666 to verify github account ownership.
這里是Group Id這個選項需要填寫正確,基本填io.github.2020 或者 com.github.2020 都是沒問題的
- Please create a new, public repository reachable at https://github.com/2020/OSSRH-6666 to prove Github account ownership.
這里是需要在你的github建一個OSSRH-6666(你自己的序號)的一個項目來證明這個項目是你的,建議最好提前在申請的時候就建好,不然再次審核的時間會非常長
四、GPG / PGP簽名文件生成
Sonatype要求所有部署的文件都需要使用GPG / PGP.asc簽名,並且每個文件都必須包含一個包含簽名的文件,所以我們需要生成密鑰對
這里我們用到Gpg4win來生成密鑰
Gpg4win下載地址:https://www.gpg4win.org/download.html
下載需要點擊$0 不給它錢
不過網速很是感人
百度網盤:(失效留言)
鏈接:https://pan.baidu.com/s/1JiUVp0GkB_fSqcRetNkKrA 提取碼:vams
接下來進行安裝,我這里是默認安裝了kleopatra界面管理的
我們這里使用兩種方法來生成密鑰對,一種kleopatra桌面管理的方式,一種cmd命令行的方式來生成
- kleopatra生成
安裝之后啟動kleopatra,點擊新建密鑰對
依次輸入個人信息進行創建
可以看見密鑰已經成功創建,一長串的數字為公鑰id
回到主界面可以看到已經增加了一條,生成的key為密鑰id下方的數字
那我們再說一下cmd命令行生成
gpg --gen-key
也是輸入信息,輸入O進行生成,生成之后也可以在管理端進行查看
五、GPG公鑰上傳至公共密鑰服務器
這里是通過公共的密鑰服務器來驗證jar包
在cmd輸入:gpg --list-key查詢一下公鑰
發布密鑰
gpg --keyserver keyserver.ubuntu.com --send-keys + 公鑰或者key
查詢是否成功
gpg --keyserver keyserver.ubuntu.com --recv-keys + 公鑰或者key
六、配置文件修改
- 先修改maven的setting.xml的配置,在
標簽下增加如下信息
<servers>
<!-- server
| Specifies the authentication information to use when connecting to a particular server, identified by
| a unique name within the system (referred to by the 'id' attribute below).
|
| NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
| used together.
|
-->
<server>
<!-- ID NAME 需要和pom.xml下<snapshotRepository> 標簽下的id要一致-->
<id>ossrh</id>
<!-- 這里填寫Sonatype OSSRH的賬號和密碼-->
<username>username</username>
<password>password</password>
</server>
</servers>
- 再修改一下pom.xml
Sonatype對pom要求還挺多的
官方對於pom的要求解釋,javadoc,簽名規范等等
官方描述:https://central.sonatype.org/pages/requirements.html
官方demo地址:https://bitbucket.org/simpligility/ossrh-pipeline-demo/src/master/pom.xml
這里我們下拆開說幾個部分
6.1 項目命名以及描述
<!-- 項目信息 -->
<!--groupId需要為Sonatype的groupId-->
<groupId> com.github.2020 </groupId>
<artifactId>name</artifactId>
<version>3.0.0</version>
<!--快照版本-->
<version>3.0.0-SNAPSHOT</version>
<!--正式版本-->
<!--<version>3.0.0-RELEASE</version>-->
<packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}</name>
<description>To introduce classes in the Java project POM</description>
<url>https://github.com/2020/2020</url>
6.2 許可證信息
開源選擇的證書,我這里是MIT
<!-- 許可證信息 -->
<licenses>
<!-- Apache許可證 -->
<!-- <license>-->
<!-- <name>The Apache Software License, Version 2.0</name>-->
<!-- <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>-->
<!-- </license>-->
<!-- MIT許可證 -->
<license>
<name>MIT License</name>
<url>http://www.opensource.org/licenses/mit-license.php</url>
</license>
</licenses>
6.3 開發者信息
<!-- 開發者信息 -->
<developers>
<developer>
<name>2020</name>
<email>2020@qq.com</email>
<url>https://github.com/2020</url>
</developer>
</developers>
6.4 scm信息
可以理解為托管信息,我這里是github,所以以此舉例
<!-- SCM信息 -> git在github上托管 -->
<scm>
<connection>scm:git:git://github.com/Ladder2020/JLadder.git</connection>
<developerConnection>scm:git:ssh://github.com/Ladder2020/JLadder.git</developerConnection>
<url>https://github.com/Ladder2020/JLadder.git</url>
</scm>
6.5 插件
使用個人資料:由於生成javadoc和源jar以及使用GPG簽署組件是一個相當耗時的過程,因此這些執行通常與正常的構建配置隔離並移動到配置文件中。然后,在通過激活配置文件執行部署時,將使用此配置文件。
<build>
<plugins>
<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>
<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>
</execution>
</executions>
</plugin>
<!-- 必須配置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>
</plugins>
</build>
6.6 上傳地址以及賬戶信息
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
6.7 注意點
這里有幾個特別需要注意的地方
那么想要正式發布的話需要改成
還有在6.6的地方的id需要和setting.xml的id需要一致不然會出現Authorization failed for 403 Forbidden或者Authorization failed for 401 Forbidden
6.8 完整pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.2020</groupId>-->
<!-- groupId需要為Sonatype的groupId-->
<groupId> com.github.2020 </groupId>
<artifactId>2020</artifactId>
<version>3.0.0</version>
<!-- 快照版本-->
<!-- <version>3.0.0-SNAPSHOT</version>-->
<!-- 正式版本-->
<!-- <version>3.0.0-RELEASE</version>-->
<packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}</name>
<!-- <name>2020</name>-->
<description>To introduce classes in the Java project POM</description>
<url>https://github.com/2020/2020</url>
<!-- 許可證信息 -->
<licenses>
<!-- Apache許可證 -->
<!-- <license>-->
<!-- <name>The Apache Software License, Version 2.0</name>-->
<!-- <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>-->
<!-- </license>-->
<!-- MIT許可證 -->
<license>
<name>MIT License</name>
<url>http://www.opensource.org/licenses/mit-license.php</url>
</license>
</licenses>
<!-- SCM信息 -> -->
<scm>
<connection>scm:git:git://github.com/2020/2020.git</connection>
<developerConnection>scm:git:ssh://github.com/2020/2020.git</developerConnection>
<url>https://github.com/2020/2020.git</url>
</scm>
<!-- 開發者信息 -->
<developers>
<developer>
<name>2020</name>
<email>2020@qq.com</email>
<url>https://github.com/2020</url>
</developer>
</developers>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArguments>
<verbose/>
<bootclasspath>${java.home}/lib/rt.jar${path.separator}${java.home}/lib/jce.jar</bootclasspath>
<extdirs>${project.basedir}/src/main/webapp/WEB-INF/lib</extdirs>
</compilerArguments>
</configuration>
</plugin>
<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>
<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>
</execution>
</executions>
</plugin>
<!-- 必須配置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>
</plugins>
</build>
<repositories>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>public</id>
<name>aliyun nexus</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
</project>
七、上傳jar包到maven中央倉庫
接下來到最后一步,進行Maven的上傳
因為之前也搭建過maven私服,所以這里比較熟悉,
我這里打包上傳演示環境為idea下
右側maven下先進行clean,然后在進行deploy構建上傳
直到顯示BUILD SUCCESS則顯示成功
然后我們去oss上進行查看是否上傳成功https://oss.sonatype.org/#welcome查找一下
接下來點擊左側菜單Staging Repositories進行同步
選中項目點擊close,進行狀態關閉
過了幾分鍾狀態會變成close,之后我們點擊Release,填入信息進行提交
提交成功進行等待,然后去https://search.maven.org或者https://mvnrepository.com等中央倉庫進行查詢你的項目
可以看到已經成功上傳
然后在我們的項目引用下試試
<dependency>
<groupId>com.gitbub.2020</groupId>
<artifactId>2020</artifactId>
<version>3.0.0</version>
<scope>compile</scope>
</dependency>
我這里就完全ok啦
2021.08.13更新,
最近重新弄了一下,有幾個需要注意的點
-
新的groupId 不支持com.github,改變為io.github
-
2021年2月起新申請的均為新的https://s01.oss.sonatype.org/content/repositories/snapshots/
-
javadoc檢驗跳過
<!-- Javadoc -->
<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>
Event: Failed: Javadoc Validation
typeId javadoc-staging
Event: Failed: Signature Validation
typeId signature-staging
Event: Failed: Sources Validation
typeId sources-staging
Event: Close failed
- deploy 命令需要在命令行中執行,在idea執行deploy不會執行gpg輸入passphrase生成asc
- 如有問題可以先檢查官方文檔 https://central.sonatype.org/publish/publish-maven/ 書寫方式