通过AndroidStudio发布 aar库 到MavenCentral

分享
开发者 2024-10-4 14:38:47 26 0 来自 中国
前言:

将 aar库 发布到 MavenCentral有以下几步:


  • 去 sonatype 注册账号并创建题目,最终至题目效果为“已修复”
  • 下载 GnuPG 创建密钥,将密钥生存至本地并将公钥发送至公钥服务器
  • 在AndroidStudio上配置好 sonatype 信息及 GnuPG 签名,提交 aar库 至  nexus 暂存
  • 使用 sonatype 账号登录 nexus 查察上传的aar库,确认无误后完成发布。
  • 发布完成,考核通过后可在 https://search.maven.org/ 搜刮查察。
  • 完结撒花~


  • 接下来将详细阐明每个步调
一:去 sonatype 注册账号并创建题目,最终至题目效果为“已修复”
1.1 第一次必要先注册账号

1.png
2.png 1.2 创建Project工单


4.png

  • 注:对于 Group Id 不清晰怎么配置自己的域名,可以参考这里:https://blog.csdn.net/u012693479/article/details/120508108
1.3 开通堆栈

创建好工单后,根据批评提示在个人堆栈创建一个项目用于校验,比方我的是创建名称为“OSSRH-80195”项目1.4 github 创建<OSSRH-80195>工程


7.png 1.4 以上预备工作都完成后,复兴下批评,待管理员考核

1.5 考核通过后,项目状态会酿成:已办理

二:下载 GnuPG 并创建GPG签名生存至本地待使用
2.1 按照下面指引下载 GnuPG exe文件下载,默认安装即可。

10.png
2.2 安装 gpg 完成之后,可以在cmd 下令行查察是否安装乐成

2.3 创建密钥,通过 cmd 下令行创建并发布公钥至公钥服务器,想通过 gpg exe步调操纵点 这里

注:网上有人是直接通过“Kleopatra.exe”步调界面操纵的,不外我在最后一步“在服务器上发布...”一直发布失败,以是就通过下令行的方式操纵,假如有知道怎样办理的大佬可以批评区留言哈,感谢~。
报错如下:
gpg.exe 的输出为:gpg: sending key 231883C5xxxxxxxx to hkps://keyserver.ubuntu.com gpg: keyserver send failed: Certificate expired gpg: keyserver send failed: Certificate expired

  • 2.3.1 天生密钥
  gpg --gen-key 13.png

  • 2.3.2 查察密钥
  gpg --list-key

  • 2.3.3 导出密钥
假如gradle签名的时间报密钥文件中找不到指定的 key ID的key的错误,此时可以手动导出
  gpg --export-secret-keys > 某盘:\...\gnupg\secring.gpg

  • 2.3.4 公钥发送至公钥服务器
keyserver.ubuntu.com
keys.openpgp.org
pgp.mit.edu
各个公钥服务器之间相互同步必要肯定时间,可以提前发送到这三个服务器,查察最新服务器地点点 这里。
  gpg --keyserver keyserver.ubuntu.com --send-keys keyId  gpg --keyserver keys.openpgp.org --send-keys keyId  gpg --keyserver pgp.mit.edu --send-keys keyId

  • 注:以上三个服务器地点只要包管此中一个发送乐成即可
  • 2.3.6 查询公钥是否上传乐成
  gpg --keyserver hkp://pool.sks-keyservers.net --search-keys keyId  gpg --keyserver hkp://pgp.mit.edu --search-keys keyId  gpg --keyserver hkp://keyserver.ubuntu.com --search-keys keyId
三:在AndroidStudio上配置好 sonatype 信息及 GnuPG 签名,提交 aar库 至  nexus 暂存
3.1 在项目根目次创建一个新的"xxx.gradle"文件,用于maven上传,并添加如下代码。

我这里取的文件名是:publish-mavencentral.gradle
apply plugin: 'maven-publish'apply plugin: 'signing'task androidSourcesJar(type: Jar) {    classifier = 'sources'    from android.sourceSets.main.java.source    exclude "**/R.class"  //清除`R.class`    exclude "**/BuildConfig.class"  //清除`BuildConfig.class`}//--- 修改配置一 ---ext {path=    PUBLISH_GROUP_ID = '之前注册sonatype时填写的的的groupId域名,假如是github的则是io.github.xxx'    PUBLISH_ARTIFACT_ID = '库的名称'    PUBLISH_VERSION = '库的版本'}//--- 修改配置二 ---ext["signing.keyId"] = ''ext["signing.password"] = ''ext["signing.secretKeyRingFile"] = ''ext["ossrhUsername"] = ''ext["ossrhPassword"] = ''File secretPropsFile = project.rootProject.file('local.properties')if (secretPropsFile.exists()) {    println "Found secret props file, loading props"    Properties p = new Properties()    p.load(new FileInputStream(secretPropsFile))    p.each { name, value ->        ext[name] = value    }} else {    println "No props file, loading env vars"}publishing {    publications {        release(MavenPublication) {            println("publish-maven Log-------> PUBLISH_GROUP_ID: $PUBLISH_GROUP_ID; PUBLISH_ARTIFACT_ID: $PUBLISH_ARTIFACT_ID; PUBLISH_VERSION: $PUBLISH_VERSION")            // The coordinates of the library, being set from variables that            // we'll set up in a moment            //配置一传入的参数            groupId PUBLISH_GROUP_ID            artifactId PUBLISH_ARTIFACT_ID            version PUBLISH_VERSION            // Two artifacts, the `aar` and the sources            artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")            artifact androidSourcesJar            // Self-explanatory metadata for the most part            pom {                //--- 修改配置三 ---                name = PUBLISH_ARTIFACT_ID                description = '上传aar插件至mavencentral,方便使用implementation快速引入' //添加文件形貌                // If your project has a dedicated site, use its URL here                url = 'https://github.com/xxx/xxxx' //项目github链接                licenses {                    license {                        //协议范例,一样平常默认Apache License2.0的话不消改:                        name = 'The Apache License, Version 2.0'                        url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'                    }                }                developers {                    developer {                        //--- 修改配置四 ---                        id = '用户ID'     //你的sonatype用户ID                        name = '用户名'   //你的sonatype用户名                        email = '邮箱'    //你的sonatype注册邮箱                    }                }                // Version control info, if you're using GitHub, follow the format as seen here                scm {                    //--- 修改配置五 ---                    //修改成你的Git地点:                    connection = 'scm:git:github.com/xxx/xxxx.git'                    developerConnection = 'scm:git:ssh://github.com/xxx/xxxx.git'                    //分支地点:                    url = 'https://github.com/xxx/xxxx/tree/master'                }                // A slightly hacky fix so that your POM will include any transitive dependencies                // that your library builds upon                withXml {                    def dependenciesNode = asNode().appendNode('dependencies')                    project.configurations.implementation.allDependencies.each {                        def dependencyNode = dependenciesNode.appendNode('dependency')                        dependencyNode.appendNode('groupId', it.group)                        dependencyNode.appendNode('artifactId', it.name)                        dependencyNode.appendNode('version', it.version)                    }                }            }        }    }    repositories {        // The repository to publish to, Sonatype/MavenCentral        maven {            // This is an arbitrary name, you may also use "mavencentral" or            // any other name that's descriptive for you            //--- 修改配置六 ---            name = "项目名称"            def releasesRepoUrl = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"            def snapshotsRepoUrl = "https://s01.oss.sonatype.org/content/repositories/snapshots/"            // You only need this if you want to publish snapshots, otherwise just set the URL            // to the release repo directly            url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl            // The username and password we've fetched earlier            credentials {                username ossrhUsername                password ossrhPassword            }        }    }}signing {    sign publishing.publications}针对以上代码,有几处必要修改成自己的配置信息


  • 3.1.1  修改配置一:填入必要使用 implementation 引入aar的格式
ext {path=    PUBLISH_GROUP_ID = '之前注册sonatype时填写的的的groupId域名,假如是github的则是io.github.xxx'    PUBLISH_ARTIFACT_ID = '库的名称'    PUBLISH_VERSION = '库的版本'}//以上三个参数分别对应的是 implementation 'io.github.Junkmer:JRouter:0.0.1' 冒号从左往右分割的三部门

  • 3.1.2  修改配置二:在项目的根目次local.properties文件中增加如下配置
signing.keyId=5D31B52D                         #刚才获取的秘钥后8位signing.password=xxxx                          #创建GPG秘钥时设置的暗码signing.secretKeyRingFile=.../xxx.gpg          #天生的.gpg末端的密钥文件目次ossrhUsername=XXX                              #sonatype用户名ossrhPassword=XXX                              #sonatype暗码

  • 3.1.3  修改配置三:配置项目形貌和项目GitHub地点
name = PUBLISH_ARTIFACT_ID  //使用配置一中的"库名称"参数description = '上传aar插件至mavencentral,方便使用implementation快速引入' //添加文件形貌url = 'https://github.com/xxx/xxxx' //项目github链接

  • 3.1.4  修改配置四: 填写在 sonatype 注册的账号信息
id = '用户ID'     //你的sonatype用户IDname = '用户名'   //你的sonatype用户名email = '邮箱'    //你的sonatype注册邮箱

  • 3.1.5  修改配置五:将模板中的"xxx/xxxx"更换成自己的github项目真实地点
//修改成你的Git地点:connection = 'scm:git:github.com/xxx/xxxx.git'developerConnection = 'scm:git:ssh://github.com/xxx/xxxx.git'//分支地点:url = 'https://github.com/xxx/xxxx/tree/master'

  • 3.1.6  修改配置六:配置项目名称和发布地点
name = "项目名称"  //填写在github创建的项目名称def releasesRepoUrl = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"def snapshotsRepoUrl = "https://s01.oss.sonatype.org/content/repositories/snapshots/"注:由于 https://oss.sonatype.org 不再维护,新地点修改成 https://s01.oss.sonatype.org,想查察最新地点点 这里
3.2 实行aar打包和上传


  • 3.2.1 aar怎样打包可以看之前写的 AndroidStudio 打包 Android项目 成 aar 这篇文章
  • 3.2.2 上传 aar 至 MavenCentral
先在必要打包上传的module下的build.gradle,添加以下代码:
apply from: "${rootProject.projectDir}/publish-mavencentral.gradle"

  • 3.2.3 以上都配置好后,在AndroidStudio右侧的gradle tasks中找到要提交的module,点击“4”使命。


  • 3.2.3 上传乐成,控制台会有如下打印,然后就可以在 nexus 上查察了
四:使用 sonatype 账号登录 nexus 查察上传的aar库,确认无误后完成发布。
4.1 登录  nexus

4.2 确认aar库无误后,点击 "release" 按钮提交考核。

五:发布完成,考核通过后可在 https://search.maven.org/ 搜刮查察。
六:完结撒花~
特别感谢:


  • Android:发布aar包到maven堆栈以及 maven插件 和 maven-publish 插件的区别 - 掘金
  • 发布android库AAR至mavenCentral看这篇文章就可以了 - 知乎
  • 项目发布到MavenCentral的流程_天兰之珠的博客-CSDN博客
  • Android库发布至MavenCentral流程详解 - 掘金
  • Android库发布到Maven Central全攻略 - 小专栏
  • 发布Android Lib库(Jar、AAR、SO)到Maven Central_卡卡爾的博客-CSDN博客
  • Android发布库(jar/aar)到MavenCentral_殇神马的博客-CSDN博客_android 发布库
  • Android库发布到Maven Central超详细攻略 - 简书
  • Maven发布 错误集锦_錯過了呗的博客-CSDN博客
您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2024-10-18 16:47, Processed in 0.150896 second(s), 35 queries.© 2003-2025 cbk Team.

快速回复 返回顶部 返回列表