發佈Android庫至MavenCentral詳解

最近,使用compose編寫了一個類QQ的image picker。完成android library的編寫,在此記錄下發佈這個Library到maven central的流程以及碰到的問題。
maven: //mvnrepository.com/artifact/io.github.huhx/compose-image-picker

github://github.com/huhx/compose_image_picker

Sonatype 賬號

MavenCentral 和 Sonatype 的關係

庫平台 運營商 管理後台
MavenCentral Sonatype oss.sonatype.org

因此我們要發佈Library到Maven Central的話,首先需要Sonatype的賬號以及權限。

申請 Sonatype 賬號

申請地址://issues.sonatype.org/secure/Signup!default.jspa

登錄賬號創建issue

創建issue地址://issues.sonatype.org/secure/ViewProfile.jspa

image-20220805223118398.png

點擊 Create 按鈕, 然後會彈出 Create Issue的窗口:

image-20220805224204197.png

點擊Configure Fields, 選擇 Custom 選項

image-20220805224557793.png

grouId的話最好使用: io.github.github_name, 要不然使用其他的還需要在 DNS 配置中配置一個TXT記錄來驗證域名所有權

填寫完所有的信息點擊創建,一個新的issue就創建成功了,以下就是我創建的issue,附上鏈接://issues.sonatype.org/browse/OSSRH-83290

image-20220805225725812.png

值得注意的是sonatype要求我們創建一個github倉庫來驗證我們的gihu賬號。創建完倉庫之後,我們回復熱心的工作人員,接下來就是等他們的處理結果了。大概30分鐘就能好吧

image-20220805230217988.png
收到這樣的回復,代表一切ready了你可以上傳package到maven central

編寫gradle腳本上傳Lib

這篇文章裏面,我是使用的android library做例子的。如果你想要發佈java的Library,可以參考://docs.gradle.org/current/userguide/publishing_maven.html

In module project, build.gradle file

// add maven-publish and signing gradle plugin
plugins {
    id 'maven-publish'
    id 'signing'
}

// add publish script
publishing {
    publications {
        release(MavenPublication) {
            pom {
                name = 'Image Picker Compose'
                description = 'An Image Picker Library for Jetpack Compose'
                url = '//github.com/huhx/compose_image_picker'

                licenses {
                    license {
                        name = 'The Apache License, Version 2.0'
                        url = '//www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }

                developers {
                    developer {
                        id = 'huhx'
                        name = 'hongxiang'
                        email = '[email protected]'
                    }
                }

                scm {
                    connection = '//github.com/huhx/compose_image_picker.git'
                    developerConnection = '//github.com/huhx/compose_image_picker.git'
                    url = '//github.com/huhx/compose_image_picker'
                }
            }

            groupId "io.github.huhx"
            artifactId "compose-image-picker"
            version "1.0.2"

            afterEvaluate {
                from components.release
            }
        }
    }
    repositories {
        maven {
            url "//s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
            credentials {
                username ossrhUsername // ossrhUsername is your sonatype username
                password ossrhPassword // ossrhUsername is your sonatype password
            }
        }
    }
}

// signing, this need key, secret, we put it into gradle.properties
signing {
    sign publishing.publications.release
}

ossrhUsernameossrhPassword 是我們在第一步註冊的sonatype賬號。用戶名和密碼是敏感信息,所以我們放在gradle.properties並且不會提交到github. 所以在 gradle.properties文件中,我們添加了以下內容:

# signing information
signing.keyId=key
signing.password=password
signing.secretKeyRingFile=file path

# sonatype account
ossrhUsername=username
ossrhPassword=password

其中包含了簽名的三個重要信息,這個我們會在下面詳細講解

創建gpg密鑰

我使用的是mac,這裡就拿mac來說明如何創建gpg密鑰。以下是shell腳本

# 安佳 gpg
> brew install gpg

# 創建gpg key,過程中會提示你輸入密碼。
# 記住這裡要輸入的密碼就是上述提到你需要配置的signing.password
> gpg --full-gen-key

# 切換目錄到~/.gnupg/openpgp-revocs.d, 你會發現有一個 .rev文件。
# 這個文件名稱的末尾8位字符就是上述提到你需要配置的signing.keyId
> cd ~/.gnupg/openpgp-revocs.d && ls

# 創建secretKeyRingFile, 以下命令會創建一個文件secring.gpg
# 然後~/.gnupg/secring.gpg就是上述提到你需要配置的signing.secretKeyRingFile
> cd ~/.gnupg/ && gpg --export-secret-keys -o secring.gpg

把signing相關的信息成功填寫到gradle.properties之後,我們就可以藉助maven-publish插件發佈我們的andoird包到maven的中心倉庫了

maven publish的gradle task

# 這個是發佈到我們的本地,你可以在~/.m2/repository/的目錄找到你發佈的包
> ./gradlew clean publishToMavenLocal

# 這個是發佈到maven的中心倉庫,你可以在//s01.oss.sonatype.org/#stagingRepositories找到
> ./gradlew clean publish

我們執行./gradlew clean publish發佈之後,訪問地址://s01.oss.sonatype.org/#stagingRepositories

image-20220805233310825.png

你會看到你的android包已經在nexus repository了。接下來你要做的兩步就是Close and Release.

檢驗以及發佈

第一步:點擊Close按鈕,它會觸發對你發佈包的檢驗。我在這個過程中碰到一個signature validation失敗的問題。

# 失敗原因:No public key inhkp://keyserver.ubuntu.com:11371,是因為同步key可能會花些時間。這裡我們可以收到發佈我們的key到相應的服務器上
> gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys signing.keyId

第二步:確保你填入的信息是滿足要求之後,Release按鈕就會被激活。點擊Release,接下來就是等待時間了,不出意外的話。30分鐘你可以在nexus repository manager找到,但是在//mvnrepository.com/ 找到的話得花更長的時間。