部署到 sonatype 很簡單!只需按照這些簡單步驟操作
在 Sonatype 上設定和發佈的參考程序在其 OSSRH 指南中描述。簡而言之,您需要兩個公開可用的 URL
OSSRH 指南會逐步引導您完成在 Sonatype 上設定帳戶的必要程序。這就如同 建立 Sonatype 的 JIRA 帳戶,然後建立一個 新的專案票證一樣簡單。在建立帳戶時,請嘗試在您的電子郵件地址中使用專案所在的相同網域。這可讓 Sonatype 更輕鬆地驗證與票證中要求的 groupId 的關係,但這並非確認所有權的唯一方法。
建立新的專案票證就如同以下操作一樣簡單
在 JIRA 上建立您的 Sonatype 帳戶之後,您可以使用相同的憑證登入 Nexus 儲存庫管理員,雖然本指南中並未要求這樣做,但稍後檢查發佈的成品可能會有所幫助。
注意: Sonatype 建議回應新的專案票證可能需要最多兩個工作天,但在我的情況下,僅需幾分鐘。
為了符合 Sonatype 對發佈到中央儲存庫的 要求並簡化發佈程序,您可以使用兩個社群外掛。sbt-pgp 外掛可以使用 GPG/PGP 來簽署檔案。(選擇性地,sbt-sonatype 可以更友善地發佈到 Sonatype 儲存庫。)
請遵循使用 PGP 簽名。
首先,您應該安裝 GnuPG,並驗證版本
$ gpg --version
gpg (GnuPG/MacGPG2) 2.2.8
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>
接下來,產生金鑰
$ gpg --gen-key
列出金鑰
$ gpg --list-keys
/home/foo/.gnupg/pubring.gpg
------------------------------
pub rsa4096 2018-08-22 [SC]
1234517530FB96F147C6A146A326F592D39AAAAA
uid [ultimate] your name <[email protected]>
sub rsa4096 2018-08-22 [E]
發佈金鑰
$ gpg --keyserver keyserver.ubuntu.com --send-keys 1234517530FB96F147C6A146A326F592D39AAAAA
使用您想要使用的 PGP 金鑰,您可以使用 sbt-pgp 外掛簽署您想要發佈到 Sonatype 儲存庫的成品。請遵循外掛的指示,您很快就會擁有 PGP 簽署的成品。
簡而言之,將下列程式碼行新增到您的 ~/.sbt/1.0/plugins/gpg.sbt
檔案中,以便為 SBT 專案全域啟用它
addSbtPlugin("com.github.sbt" % "sbt-pgp" % "2.1.2")
注意:此外掛是用於簽署成品的解決方案。它適用於 GPG 命令列工具。
請確保 gpg
命令位於 sbt 可用的 PATH 中。
您的 Sonatype OSSRH 帳戶憑證需要儲存在安全的地方 (例如,不要儲存在儲存庫中)。常見的慣例是 $HOME/.sbt/1.0/sonatype.sbt
檔案,其中包含以下內容
credentials += Credentials(Path.userHome / ".sbt" / "sonatype_credentials")
接下來,建立一個檔案 ~/.sbt/sonatype_credentials
realm=Sonatype Nexus Repository Manager
host=oss.sonatype.org
user=<your username>
password=<your password>
注意:前兩個字串必須是
"Sonatype Nexus Repository Manager"
和"oss.sonatype.org"
,Coursier 才能使用這些憑證。如果您使用的是在 2021 年 2 月之後建立的新 OSSRH 帳戶,請使用"s01.oss.sonatype.org"
取代"oss.sonatype.org"
若要發佈到 Maven 儲存庫,您需要設定一些設定,以便產生正確的中繼資料。
將這些設定新增到 build.sbt
的結尾或單獨的 publish.sbt
ThisBuild / organization := "com.example.project2"
ThisBuild / organizationName := "example"
ThisBuild / organizationHomepage := Some(url("http://example.com/"))
ThisBuild / scmInfo := Some(
ScmInfo(
url("https://github.com/your-account/your-project"),
"scm:[email protected]:your-account/your-project.git"
)
)
ThisBuild / developers := List(
Developer(
id = "Your identifier",
name = "Your Name",
email = "your@email",
url = url("http://your.url")
)
)
ThisBuild / description := "Some description about your project."
ThisBuild / licenses := List(
"Apache 2" -> new URL("http://www.apache.org/licenses/LICENSE-2.0.txt")
)
ThisBuild / homepage := Some(url("https://github.com/example/project"))
// Remove all additional repository other than Maven Central from POM
ThisBuild / pomIncludeRepository := { _ => false }
ThisBuild / publishTo := {
// For accounts created after Feb 2021:
// val nexus = "https://s01.oss.sonatype.org/"
val nexus = "https://oss.sonatype.org/"
if (isSnapshot.value) Some("snapshots" at nexus + "content/repositories/snapshots")
else Some("releases" at nexus + "service/local/staging/deploy/maven2")
}
ThisBuild / publishMavenStyle := true
pom.xml
(Maven 使用的專案組態的最終產品) 檔案的完整格式 在此處概述。您可以使用 build.sbt
中的 pomExtra
選項將更多資料新增至其中。
從 sbt Shell 執行
> publishSigned
在 Nexus 儲存庫管理員中檢查已發佈的成品 (與 Sonatype 的 Jira 帳戶相同的登入)。
按一下「關閉」按鈕,然後按一下「發佈」按鈕,以關閉暫存儲存庫並將發佈版本升級至中央。
注意:sbt-sonatype 是第三方外掛,這表示它不在 Lightbend 訂閱的範圍內。
若要簡化 Sonatype 的 Nexus 的使用方式,請將下列程式碼行新增到 project/plugins.sbt
,以便將 sbt-sonatype 外掛匯入您的專案
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.9.13")
此外掛將有助於發佈程序,但簡而言之,這些是將函式庫發佈到儲存庫的主要步驟
sonatypeOpen "您的 groupId" "一些暫存名稱"
publishSigned
sonatypeRelease
以下是一些在使用此外掛時需要注意的重要金鑰。請在此處閱讀以取得更多資訊。
// This becomes a simplified version of the above key.
publishTo := sonatypePublishToBundle.value
// Set this to the same value set as your credential files host.
sonatypeCredentialHost := "oss.sonatype.org"
// Set this to the repository to publish to using `s01.oss.sonatype.org`
// for accounts created after Feb. 2021.
sonatypeRepository := "https://oss.sonatype.org/service/local"
發佈之後,您必須遵循 Nexus 的發佈工作流程。
注意:sbt-sonatype 外掛也可以用來發佈到其他非 Sonatype 儲存庫
使用階段式發佈,以便在推送完整專案之前,測試獨立發佈的大型專案。
注意:錯誤訊息
PGPException: checksum mismatch at 0 of 20
表示您輸入的密碼錯誤。我們至少在 OS X 上發現,7 位元 ASCII 範圍之外的字元 (例如,母音變音) 可能會出現問題。如果您絕對確定您輸入的密碼正確且錯誤並未消失,請嘗試變更密碼。
注意:如果您使用的是在 2021 年 2 月之後建立的新 OSSRH 帳戶,請使用
"s01.oss.sonatype.org"
取代"oss.sonatype.org"
注意:sbt-release 是第三方外掛,這表示它不在 Lightbend 訂閱的範圍內。
為了使用 sbt-release 外掛 自動化上述發佈流程,您只需將發佈指令作為步驟加入到 releaseProcess
任務中即可
...
releaseStepCommand("sonatypeOpen \"your groupId\" \"Some staging name\""),
...
releaseStepCommand("publishSigned"),
...
releaseStepCommand("sonatypeRelease"),
...