发布Android库至MavenCentral详解

虚幻大学 虚幻 171℃ 0评论

? 优质资源分享 ?

学习路线指引(点击解锁) 知识定位 人群定位
? Python实战微信订餐小程序 ? 进阶级 本课程是python flask+微信小程序的完美结合,从项目搭建到腾讯云部署上线,打造一个全栈订餐系统。
?Python量化交易实战? 入门级 手把手带你打造一个易扩展、更安全、效率更高的量化交易系统

最近,使用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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gucYspX7-1659760563415)(https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/47137d59e6ba484d8508fcae625c3deb~tplv-k3u1fbpfcp-watermark.image?)]

点击 Create 按钮, 然后会弹出 Create Issue的窗口:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WQezZtkG-1659760563418)(https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/3fe652e230b648aab1dca58e83f77c2e~tplv-k3u1fbpfcp-watermark.image?)]

点击Configure Fields, 选择 Custom 选项

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xUUmjp9Y-1659760563420)(https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/75118262fe6e463fa7b00280adf2a353~tplv-k3u1fbpfcp-watermark.image?)]

grouId的话最好使用: io.github.github_name, 要不然使用其他的还需要在 DNS 配置中配置一个TXT记录来验证域名所有权

填写完所有的信息点击创建,一个新的issue就创建成功了,以下就是我创建的issue,附上链接:https://issues.sonatype.org/browse/OSSRH-83290

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-v58zIRHw-1659760563421)(https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/847df1b5a8e54587a7638f8095dd69eb~tplv-k3u1fbpfcp-watermark.image?)]

值得注意的是sonatype要求我们创建一个github仓库来验证我们的gihu账号。创建完仓库之后,我们回复热心的工作人员,接下来就是等他们的处理结果了。大概30分钟就能好吧

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gJm8ca5I-1659760563422)(https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/2e01216f87ea402cbffa62bf40e4f6c4~tplv-k3u1fbpfcp-watermark.image?)]
收到这样的回复,代表一切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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-x59160g5-1659760563423)(https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/fb8c333515c74b6796223e0d1e48d11c~tplv-k3u1fbpfcp-watermark.image?)]

你会看到你的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/找到的话得花更长的时间。

转载请注明:虚坏叔叔 » 发布Android库至MavenCentral详解

喜欢 (0)

您必须 登录 才能发表评论!