发布Android库至MavenCentral详解

最近,使用compose编写了一个类QQ的image picker。完成android library的编写,在此记录下发布这个Library到maven central的流程以及碰到的问题。
maven: https://mvnrepository.com/artifact/io.github.huhx/compose-image-picker

github:https://github.com/huhx/compose_image_picker

Sonatype 账号

MavenCentral 和 Sonatype 的关系

库平台 运营商 管理后台
MavenCentral Sonatype s01.oss.sonatype.org

因此我们要发布Library到Maven Central的话,首先需要Sonatype的账号以及权限。

申请 Sonatype 账号

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

登录账号创建issue

创建issue地址:https://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,附上链接:https://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,可以参考:https://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 = 'https://github.com/huhx/compose_image_picker'                  licenses {                     license {                         name = 'The Apache License, Version 2.0'                         url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'                     }                 }                  developers {                     developer {                         id = 'huhx'                         name = 'hongxiang'                         email = 'gohuhx@gmail.com'                     }                 }                  scm {                     connection = 'https://github.com/huhx/compose_image_picker.git'                     developerConnection = 'https://github.com/huhx/compose_image_picker.git'                     url = 'https://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 "https://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的中心仓库,你可以在https://s01.oss.sonatype.org/#stagingRepositories找到 > ./gradlew clean publish 

我们执行./gradlew clean publish发布之后,访问地址:https://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找到,但是在https://mvnrepository.com/找到的话得花更长的时间。

商匡云商
Logo
注册新帐户
对比商品
  • 合计 (0)
对比
0
购物车