草庐IT

通过AndroidStudio发布 aar库 到MavenCentral

安然罓安好 2023-10-12 原文

前言:

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

  1. sonatype 注册账号并创建问题,最终至问题结果为“已修复”
  2. 下载 GnuPG 创建密钥,将密钥保存至本地并将公钥发送至公钥服务器
  3. 在AndroidStudio上配置好 sonatype 信息及 GnuPG 签名,提交 aar库 至 nexus 暂存
  4. 使用 sonatype 账号登录 nexus 查看上传的aar库,确认无误后完成发布。
  5. 发布完成,审核通过后可在 https://search.maven.org/ 搜索查看。
  6. 完结撒花~
  • 接下来将详细说明每个步骤

一:去 sonatype 注册账号并创建问题,最终至问题结果为“已修复”

1.1 第一次需要先注册账号

1.2 创建Project工单

1.3 开通仓库
 创建好工单后,根据评论提示在个人仓库创建一个项目用于校验,例如我的是创建名称为“OSSRH-80195”项目
1.4 github 创建<OSSRH-80195>工程

1.4 以上准备工作都完成后,回复下评论,待管理员审核

1.5 审核通过后,项目状态会变成:已解决

二:下载 GnuPG 并创建GPG签名保存至本地待使用

2.1 按照下面指引下载 GnuPG exe文件下载,默认安装即可。

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
  • 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用户ID
name = '用户名'   //你的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打包和上传

先在需要打包上传的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/ 搜索查看。

六:完结撒花~

特别感谢:

有关通过AndroidStudio发布 aar库 到MavenCentral的更多相关文章

  1. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  2. ruby - 通过 erb 模板输出 ruby​​ 数组 - 2

    我正在使用puppet为ruby​​程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby​​不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这

  3. ruby - 通过 ruby​​ 进程共享变量 - 2

    我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是

  4. ruby - 通过 RVM (OSX Mountain Lion) 安装 Ruby 2.0.0-p247 时遇到问题 - 2

    我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search

  5. ruby-on-rails - Enumerator.new 如何处理已通过的 block ? - 2

    我在理解Enumerator.new方法的工作原理时遇到了一些困难。假设文档中的示例:fib=Enumerator.newdo|y|a=b=1loopdoy[1,1,2,3,5,8,13,21,34,55]循环中断条件在哪里,它如何知道循环应该迭代多少次(因为它没有任何明确的中断条件并且看起来像无限循环)? 最佳答案 Enumerator使用Fibers在内部。您的示例等效于:require'fiber'fiber=Fiber.newdoa=b=1loopdoFiber.yieldaa,b=b,a+bendend10.times.m

  6. ruby - 寻找通过阅读代码确定编程语言的ruby gem? - 2

    几个月前,我读了一篇关于ruby​​gem的博客文章,它可以通过阅读代码本身来确定编程语言。对于我的生活,我不记得博客或gem的名称。谷歌搜索“ruby编程语言猜测”及其变体也无济于事。有人碰巧知道相关gem的名称吗? 最佳答案 是这个吗:http://github.com/chrislo/sourceclassifier/tree/master 关于ruby-寻找通过阅读代码确定编程语言的rubygem?,我们在StackOverflow上找到一个类似的问题:

  7. 通过 MacPorts 的 RubyGems 是个好主意吗? - 2

    从MB升级到新的MBP后,Apple的迁移助手没有移动我的gem。我这次是通过macports安装ruby​​gems,希望在下次升级时避免这种情况。有什么我应该注意的陷阱吗? 最佳答案 如果你想把你的gems安装在你的主目录中(在传输过程中应该复制过来,作为一个附带的好处,会让你以你自己的身份运行geminstall,而不是root),将gemhome:键设置为您在~/.gemrc中的主目录中的路径. 关于通过MacPorts的RubyGems是个好主意吗?,我们在StackOverf

  8. ruby - 通过 RVM 安装 Ruby 1.9.2 永远行不通! - 2

    当我执行>rvminstall1.9.2时一切顺利。然后我做>rvmuse1.9.2也很顺利。但是当涉及到ruby​​-v时..sam@sjones:~$rvminstall1.9.2/home/sam/.rvm/rubies/ruby-1.9.2-p136,thismaytakeawhiledependingonyourcpu(s)...ruby-1.9.2-p136-#fetchingruby-1.9.2-p136-#downloadingruby-1.9.2-p136,thismaytakeawhiledependingonyourconnection...%Total%Rece

  9. ruby - 可以通过多少种方法将方法添加到 ruby​​ 对象? - 2

    当谈到运行时自省(introspection)和动态代码生成时,我认为ruby​​没有任何竞争对手,可能除了一些lisp方言。前几天,我正在做一些代码练习来探索ruby​​的动态功能,我开始想知道如何向现有对象添加方法。以下是我能想到的3种方法:obj=Object.new#addamethoddirectlydefobj.new_method...end#addamethodindirectlywiththesingletonclassclass这只是冰山一角,因为我还没有探索instance_eval、module_eval和define_method的各种组合。是否有在线/离线资

  10. ruby - 检查是否通过 require 执行或导入了 Ruby 程序 - 2

    如何检查Ruby文件是否是通过“require”或“load”导入的,而不是简单地从命令行执行的?例如:foo.rb的内容:puts"Hello"bar.rb的内容require'foo'输出:$./foo.rbHello$./bar.rbHello基本上,我想调用bar.rb以不执行puts调用。 最佳答案 将foo.rb改为:if__FILE__==$0puts"Hello"end检查__FILE__-当前ruby​​文件的名称-与$0-正在运行的脚本的名称。 关于ruby-检查是否

随机推荐