前言
本文用于記錄如何將自己的庫(kù)上傳到maven中心倉(cāng)庫(kù),
- 首先我們需要注冊(cè)sonatype的jira賬號(hào)装蓬,然后申請(qǐng)創(chuàng)建自己的repo蹈丸,等待官方審核通過(guò)之后即可擁有自己的空間壁涎;
- 我們使用gradle的maven-publish和signing插件來(lái)簡(jiǎn)化打包上傳的操作坏瘩,通過(guò)配置之后,即可通過(guò)gradle任務(wù)來(lái)上傳到maven倉(cāng)庫(kù)润脸;
- 上傳時(shí)可以選擇上傳到snapshot存儲(chǔ)區(qū)或者staging存儲(chǔ)區(qū)柬脸,這兩個(gè)存儲(chǔ)去上傳之后即可立刻訪問(wèn),snapshot區(qū)可公開(kāi)訪問(wèn)毙驯,而staging只能供自己或有權(quán)限的人使用倒堕,需要驗(yàn)證用戶名密碼;
- 如需要將staging區(qū)的版本公開(kāi)給所有人使用爆价,可通過(guò)sonatype網(wǎng)站上的release操作來(lái)公開(kāi)垦巴;
創(chuàng)建sonatype賬號(hào)及group
參考: OSSRH Guide - The Central Repository Documentation (sonatype.org)
創(chuàng)建issues: 創(chuàng)建問(wèn)題 - Sonatype JIRA
創(chuàng)建issues并且經(jīng)管理員同意后媳搪,才可以上傳倉(cāng)庫(kù),創(chuàng)建之后相當(dāng)于擁有了一個(gè)group骤宣,之后可以往這個(gè)group上傳其他的項(xiàng)目而無(wú)需再次建立issues秦爆;
新建issues之后,系統(tǒng)會(huì)回復(fù)憔披,根據(jù)回復(fù)的要求進(jìn)行處理即可等限,通過(guò)后會(huì)有類似的回復(fù):
com.github.hanlyjiang has been prepared, now user(s) hanlyjiang can:
Deploy snapshot artifacts into repository https://oss.sonatype.org/content/repositories/snapshots
Deploy release artifacts into the staging repository https://oss.sonatype.org/service/local/staging/deploy/maven2
Release staged artifacts into repository 'Releases'
please comment on this ticket when you promoted your first release, thanks
即表明我們擁有了自己的group;
生成簽名key
所有上傳到倉(cāng)庫(kù)中的文件必須進(jìn)行簽名芬膝,否則會(huì)無(wú)法發(fā)布望门,所以我們需要生成簽名用的key,同時(shí)還需要將key推送到公共的key服務(wù)器锰霜,然sonatype服務(wù)器可以訪問(wèn)到筹误,以進(jìn)行驗(yàn)證;
生成key
過(guò)程中需要輸入密碼癣缅,==輸入后請(qǐng)記住密碼==
gpg --gen-key
gpg (GnuPG) 2.2.22; Copyright (C) 2020 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
gpg: 鑰匙箱‘/Users/hanlyjiang/.gnupg/pubring.kbx’已創(chuàng)建
注意:使用 “gpg --full-generate-key” 以獲得一個(gè)功能完整的密鑰產(chǎn)生對(duì)話框厨剪。
GnuPG 需要構(gòu)建用戶標(biāo)識(shí)以辨認(rèn)您的密鑰。
真實(shí)姓名: hanlyjiang
電子郵件地址: hanlyjiang@outlook.com
您選定了此用戶標(biāo)識(shí):
“hanlyjiang <hanlyjiang@outlook.com>”
更改姓名(N)所灸、注釋(C)丽惶、電子郵件地址(E)或確定(O)/退出(Q)炫七? o
我們需要生成大量的隨機(jī)字節(jié)爬立。在質(zhì)數(shù)生成期間做些其他操作(敲打鍵盤(pán)
、移動(dòng)鼠標(biāo)万哪、讀寫(xiě)硬盤(pán)之類的)將會(huì)是一個(gè)不錯(cuò)的主意侠驯;這會(huì)讓隨機(jī)數(shù)
發(fā)生器有更好的機(jī)會(huì)獲得足夠的熵。
我們需要生成大量的隨機(jī)字節(jié)奕巍。在質(zhì)數(shù)生成期間做些其他操作(敲打鍵盤(pán)
吟策、移動(dòng)鼠標(biāo)、讀寫(xiě)硬盤(pán)之類的)將會(huì)是一個(gè)不錯(cuò)的主意的止;這會(huì)讓隨機(jī)數(shù)
發(fā)生器有更好的機(jī)會(huì)獲得足夠的熵檩坚。
gpg: 密鑰 E8A99FE282B70849 被標(biāo)記為絕對(duì)信任
gpg: 吊銷證書(shū)已被存儲(chǔ)為‘/Users/hanlyjiang/.gnupg/openpgp-revocs.d/0B372361CC1A9AE2452D43FDE8A99FE282B70849.rev’
公鑰和私鑰已經(jīng)生成并被簽名。
pub rsa3072 2021-05-24 [SC] [有效至:2023-05-24]
0B372361CC1A9AE2452D43FDE8A99FE282B70849
uid hanlyjiang <hanlyjiang@outlook.com>
sub rsa3072 2021-05-24 [E] [有效至:2023-05-24]
列出key
gpg --list-keys
gpg: 正在檢查信任度數(shù)據(jù)庫(kù)
gpg: 絕對(duì)信任密鑰 8F5EC255E5A0D063 的公鑰未找到
gpg: 絕對(duì)信任密鑰 4150E419D483B9A6 的公鑰未找到
gpg: marginals needed: 3 completes needed: 1 trust model: pgp
gpg: 深度:0 有效性: 3 已簽名: 0 信任度:0-诅福,0q匾委,0n,0m氓润,0f赂乐,3u
gpg: 下次信任度數(shù)據(jù)庫(kù)檢查將于 2023-05-24 進(jìn)行
/Users/hanlyjiang/.gnupg/pubring.kbx
------------------------------------
pub rsa3072 2021-05-24 [SC] [有效至:2023-05-24]
0B372361CC1A9AE2452D43FDE8A99FE282B70849
uid [ 絕對(duì) ] hanlyjiang <hanlyjiang@outlook.com>
sub rsa3072 2021-05-24 [E] [有效至:2023-05-24]
發(fā)送key到服務(wù)器
提示:
如果一個(gè)key服務(wù)器不通的話,可以換一個(gè)重新來(lái)一遍咖气,只要上傳成功一個(gè)即可挨措。
key需要發(fā)送到服務(wù)器上挖滤,以便sonatype獲取并校驗(yàn)簽名,通過(guò)如下命令上傳:
設(shè)置key的信息:
KEY_SERVER=hkp://pool.sks-keyservers.net
KEY_ID=0B372361CC1A9AE2452D43FDE8A99FE282B70849
上傳:
$ gpg --keyserver $KEY_SERVER --send-keys $KEY_ID
gpg: 正在發(fā)送密鑰 E8A99FE282B70849 到 hkp://pool.sks-keyservers.net
查看是否成功:
KEY_SERVER=hkp://pool.sks-keyservers.net
gpg --keyserver $KEY_SERVER --recv-keys $KEY_ID
可用的key-server:
hkp://keyserver.ubuntu.com
hkp://pool.sks-keyservers.net
hkp://keys.openpgp.org
hkp://keys.gnupg.net
hkp://keys.openpgp.org
導(dǎo)出key
導(dǎo)出公鑰:
gpg -a -o ~/.gnupg/maven-pub.key --export $KEY_ID
導(dǎo)出私鑰:(需要輸入密碼)
gpg -a -o ~/.gnupg/maven-prv.key --export-secret-keys $KEY_ID
導(dǎo)出gpgkey:
gpg --keyring secring.gpg --export-secret-keys > ~/.gnupg/secring.gpg
gradle配置
官方關(guān)于Gradle下上傳的說(shuō)明在這里 Gradle - The Central Repository Documentation (sonatype.org)浅役,這個(gè)文章中使用的是maven插件斩松,另外還有一個(gè)maven-publish插件,我們使用maven-publish這個(gè)插件觉既;
gradle.properties
修改gradle配置:
# ~/.gradle/gradle.properties 寫(xiě)入如下內(nèi)容:
ossrhUsername=hanlyjiang # jira的用戶名
ossrhPassword=#jira的密碼
# 公鑰ID的后8位 0B372361CC1A9AE2452D43FDE8A99FE282B70849
signing.keyId=82B70849
signing.password=生成key時(shí)的密碼
signing.secretKeyRingFile=/Users/hanlyjiang/.gnupg/secring.gpg
用戶名和密碼可以隨意命名砸民,只要自己在build.gradle對(duì)應(yīng)上就可以
而 signing 的配置則需要保持名稱一致。
build.gradle:
參考:
- [使用 Maven Publish 插件 | Android 開(kāi)發(fā)者 | Android Developers (google.cn)](https://developer.android.google.cn/studio/build/maven-publish-plugin?hl=zh-cn#:~:text=使用 Maven Publish 插件 Android Gradle 插件 3.6.0,Gradle 插件會(huì)為應(yīng)用或庫(kù)模塊中的每個(gè)構(gòu)建變體工件創(chuàng)建一個(gè) 組件 奋救,您可以使用它來(lái)自定義要發(fā)布到 Maven 代碼庫(kù)的 發(fā)布內(nèi)容 岭参。)
- Maven Publish Plugin (gradle.org)
我們?cè)谛枰蟼鞯捻?xiàng)目中配置,注意pom中的信息也需要補(bǔ)全尝艘,否則上傳之后無(wú)法通過(guò)sonatype的檢查演侯,無(wú)法發(fā)布;
??注意: 有的人的上傳地址可能是 https://s01.oss.sonatype.org 的域名背亥,如:
https://s01.oss.sonatype.org/content/repositories/snapshots
https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/
plugins {
id 'com.android.library'
id 'signing'
id 'maven-publish'
}
def VERSION="1.0.1"
android {
defaultConfig {
minSdkVersion 22
targetSdkVersion 30
versionCode 1
versionName VERSION
}
}
// Because the components are created only during the afterEvaluate phase, you must
// configure your publications using the afterEvaluate() lifecycle method.
afterEvaluate {
publishing {
repositories {
maven {
name "local"
// change to point to your repo, e.g. http://my.org/repo
url = "$buildDir/repo"
}
maven {
name "sonartype-Staging"
// change to point to your repo, e.g. http://my.org/repo
url = "https://oss.sonatype.org/service/local/staging/deploy/maven2"
// https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/
credentials {
username = ossrhUsername
password = ossrhPassword
}
}
// 定義snapshot倉(cāng)庫(kù)
maven {
name "sonatype-Snapshots"
// change to point to your repo, e.g. http://my.org/repo
url = "https://oss.sonatype.org/content/repositories/snapshots/"
credentials {
username = ossrhUsername
password = ossrhPassword
}
}
}
publications {
// Creates a Maven publication called "release".
release(MavenPublication) {
// Applies the component for the release build variant.
from components.release
// You can then customize attributes of the publication as shown below.
groupId = 'com.github.hanlyjiang'
artifactId = 'apf_library'
version = VERSION
pom {
name = 'HJ Android Plugin Framework'
description = 'A Android Plugin Framework'
url = 'https://github.com/hanlyjiang/apf-library'
licenses {
license {
name='The Apache Software License, Version 2.0'
url='http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id = 'hanlyjiang'
name = 'hanly jiang'
email = 'hanlyjiang@outlook.com'
}
}
scm {
connection = 'https://github.com/hanlyjiang/apf-library'
developerConnection = 'https://github.com/hanlyjiang/apf-library.git'
url = 'https://github.com/hanlyjiang/apf-library'
}
}
}
// Creates a Maven publication called “debug”.
debug(MavenPublication) {
// Applies the component for the debug build variant.
from components.debug
groupId = 'com.github.hanlyjiang'
artifactId = 'apf_library-debug'
version = String.format("%s-SNAPSHOT",VERSION)
pom {
name = 'HJ Android Plugin Framework'
description = 'A Android Plugin Framework'
url = 'https://github.com/hanlyjiang/apf-library'
licenses {
license {
name='The Apache Software License, Version 2.0'
url='http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id = 'hanlyjiang'
name = 'hanly jiang'
email = 'hanlyjiang@outlook.com'
}
}
scm {
connection = 'https://github.com/hanlyjiang/apf-library'
developerConnection = 'https://github.com/hanlyjiang/apf-library.git'
url = 'https://github.com/hanlyjiang/apf-library'
}
}
}
}
signing {
sign publishing.publications.release , publishing.publications.debug
}
}
}
發(fā)布腳本配置中有以下需要注意:
- groupId:需要配置自己申請(qǐng)的 groupId秒际;
- artifactId:需要修改為自己項(xiàng)目的 artifactId;
- pom 中的文件描述需要修改為自己項(xiàng)目的描述狡汉;
- repositories 部分配置了遠(yuǎn)程倉(cāng)庫(kù)對(duì)應(yīng)的用戶名和密碼娄徊,發(fā)布地址需要根據(jù)是否是新項(xiàng)目進(jìn)行修改,舊項(xiàng)目域名是 oss.sonatype.org盾戴,新項(xiàng)目域名是:s01.oss.sonatype.org
- signing 簽名部分需要配置對(duì)應(yīng)的 gpg 密鑰和賬戶信息
關(guān)于maven 倉(cāng)庫(kù)的注意點(diǎn):
- snapshots倉(cāng)庫(kù)上傳的庫(kù)寄锐,其版本號(hào)需要以
-SNAPSHOT
結(jié)尾,否則可能出現(xiàn)400錯(cuò)誤尖啡;
執(zhí)行上傳任務(wù)
執(zhí)行g(shù)radle 對(duì)應(yīng)的任務(wù)即可上傳
$ module=apf-library; ./gradlew "$module":publishReleasePublicationToCenterRepository
具體生成的任務(wù)可以在AndroidStudio 的Gradle工具窗口中查看publishing分組的任務(wù)橄仆;或者通過(guò)如下命令查看
$ module=apf-library; ./gradlew "$module":tasks| grep -E "publish|generate" generateMetadataFileForDebugPublication - Generates the Gradle metadata file for publication 'debug'. generateMetadataFileForReleasePublication - Generates the Gradle metadata file for publication 'release'. generatePomFileForDebugPublication - Generates the Maven POM file for publication 'debug'. generatePomFileForReleasePublication - Generates the Maven POM file for publication 'release'. publish - Publishes all publications produced by this project. publishAllPublicationsToCenterRepository - Publishes all Maven publications produced by this project to the center repository. publishAllPublicationsToLocalRepository - Publishes all Maven publications produced by this project to the local repository. publishDebugPublicationToCenterRepository - Publishes Maven publication 'debug' to Maven repository 'center'. publishDebugPublicationToLocalRepository - Publishes Maven publication 'debug' to Maven repository 'local'. publishDebugPublicationToMavenLocal - Publishes Maven publication 'debug' to the local Maven repository. publishReleasePublicationToCenterRepository - Publishes Maven publication 'release' to Maven repository 'center'. publishReleasePublicationToLocalRepository - Publishes Maven publication 'release' to Maven repository 'local'. publishReleasePublicationToMavenLocal - Publishes Maven publication 'release' to the local Maven repository. publishToMavenLocal - Publishes all Maven publications produced by this project to the local Maven cache.
發(fā)布
- close
- release
參考:
首先需要解釋下這里的發(fā)布指的什么意思:我們的倉(cāng)庫(kù)上傳之后,實(shí)際上是存儲(chǔ)與一個(gè)臨時(shí)的獨(dú)立與公有倉(cāng)庫(kù)的地方衅斩,這個(gè)只能我們自己訪問(wèn)盆顾,如果需要將倉(cāng)庫(kù)提供給其他人訪問(wèn),就需要發(fā)布畏梆;發(fā)布的過(guò)程可以手動(dòng)在web頁(yè)面上操作您宪,也可以通過(guò)命令行來(lái)進(jìn)行;
發(fā)布需要取sonatype的網(wǎng)站上操作奠涌,以下為操作步驟:
-
然后登錄宪巨,登錄之后可以看到
Build Promotion
的菜單,然后打開(kāi)Staging Repository
铣猩,其中會(huì)顯示已經(jīng)上傳的倉(cāng)庫(kù): -
選中一個(gè)staging repo揖铜,然后點(diǎn)擊
Close
,并進(jìn)行確認(rèn) -
結(jié)果可以在Activity中查看
上面的錯(cuò)誤是找不到key达皿,我們重新將key上傳到ubuntu:
gpg --keyserver $KEY_SERVER --send-keys 0B372361CC1A9AE2452D43FDE8A99FE282B70849
gpg --keyserver $KEY_SERVER --recv-keys 0B372361CC1A9AE2452D43FDE8A99FE282B70849
然后再次close
-
然后再進(jìn)行release
-
完成后天吓,會(huì)發(fā)一個(gè)郵件通知贿肩,然后會(huì)更新jira上創(chuàng)建項(xiàng)目的issues:
引入并使用
引入snapshot或staging版本
可通過(guò)如下路徑確認(rèn)自己的庫(kù)是否上傳成功(注意將后面的路徑替換為自己的):
- Snapshot: https://oss.sonatype.org/content/repositories/snapshots/com/github/hanlyjiang/
- Release: https://repo1.maven.org/maven2/com/github/hanlyjiang/
snapshot和staging的倉(cāng)庫(kù)中的版本可在推送后立即訪問(wèn),不過(guò)只能自己訪問(wèn)龄寞,需要驗(yàn)證用戶名和密碼汰规。
添加以下repo配置:
allprojects {
repositories {
maven {
name = "Sonatype-Snapshots"
setUrl("https://oss.sonatype.org/content/repositories/snapshots")
// setUrl("https://s01.oss.sonatype.org/content/repositories/snapshots")
credentials(PasswordCredentials::class.java) {
username = property("ossrhUsername").toString()
password = property("ossrhPassword").toString()
}
}
maven {
name = "Sonatype-Staging"
setUrl("https://oss.sonatype.org/service/local/staging/deploy/maven2/")
// setUrl("https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/")
credentials(PasswordCredentials::class.java) {
username = property("ossrhUsername").toString()
password = property("ossrhPassword").toString()
}
}
google()
jcenter()
mavenCentral()
}
}
引入release版本
如果需要公開(kāi)發(fā)布給自己或其他人使用,則需要release物邑,release操作之后距離可以訪問(wèn)到有一定的時(shí)間周期溜哮,下面是一次release后收到的官方的郵件:
Central sync is activated for com.github.hanlyjiang. After you successfully release, your component will be published to Central https://repo1.maven.org/maven2/, typically within 10 minutes, though updates to https://search.maven.org can take up to two hours.
也就是說(shuō)從maven中心倉(cāng)庫(kù)中查詢需要10分鐘左右,從網(wǎng)頁(yè)搜索則需要2個(gè)小時(shí)色解,可以訪問(wèn):https://search.maven.org 來(lái)搜索茂嗓,可以通過(guò)訪問(wèn) https://repo1.maven.org/maven2/ 來(lái)確認(rèn)是否被索引了,如果被索引科阎,則可以引入到項(xiàng)目之中述吸;
在gradle中使用則只需要導(dǎo)入mavenCenter() 即可;
rootProject build.gradle:
allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}
app build.gradle:
implementation("com.github.hanlyjiang:apf_library:1.0")
kotlin中使用
配置
import org.gradle.api.publish.maven.MavenPom
plugins {
id("com.android.library")
id("signing")
`maven-publish`
// kotlin("android")
// kotlin("android.extensions")
}
android {
// 省略android配置
}
fun getMyPom(): Action<in MavenPom> {
return Action<MavenPom> {
name.set("Android Common Utils Lib")
description.set("Android Common Utils Library For HJ")
url.set("https://github.com/hanlyjiang/lib_common_utils")
properties.set(mapOf(
"myProp" to "value",
"prop.with.dots" to "anotherValue"
))
licenses {
license {
name.set("The Apache License, Version 2.0")
url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
}
}
developers {
developer {
id.set("hanlyjiang")
name.set("Hanly Jiang")
email.set("hanlyjiang@outlook.com")
}
}
scm {
connection.set("scm:git:git://github.com/hanlyjiang/lib_common_utils.git")
developerConnection.set("scm:git:ssh://github.com/hanlyjiang/lib_common_utils.git")
url.set("https://github.com/hanlyjiang/lib_common_utils")
}
}
}
afterEvaluate {
publishing {
publications {
create<MavenPublication>("release") {
from(components.getByName("release"))
groupId = "com.github.hanlyjiang"
artifactId = "android_common_utils"
version = android.defaultConfig.versionName
pom(getMyPom())
}
}
repositories {
val ossrhCredentials = Action<PasswordCredentials> {
username = properties["ossrhUsername"].toString()
password = properties["ossrhPassword"].toString()
}
// sonar的倉(cāng)庫(kù)锣笨,地址根據(jù)項(xiàng)目的版本號(hào)來(lái)確定是snapshot還是正式倉(cāng)庫(kù)
maven {
name = "Sonartype"
val releasesRepoUrl = uri("https://oss.sonatype.org/service/local/staging/deploy/maven2")
val snapshotsRepoUrl = uri("https://oss.sonatype.org/content/repositories/snapshots/")
url = if (android.defaultConfig.versionName.toString().endsWith("SNAPSHOT")) snapshotsRepoUrl else releasesRepoUrl
credentials(ossrhCredentials)
// snapshot的地址:
// https://oss.sonatype.org/content/repositories/snapshots/com/github/hanlyjiang/android_common_utils/
}
// 項(xiàng)目本地的倉(cāng)庫(kù)
maven {
name = "ProjectLocal"
val releasesRepoUrl = uri(layout.buildDirectory.dir("repos/releases"))
val snapshotsRepoUrl = uri(layout.buildDirectory.dir("repos/snapshots"))
url = if (android.defaultConfig.versionName.toString().endsWith("SNAPSHOT")) snapshotsRepoUrl else releasesRepoUrl
}
}
}
// https://stackoverflow.com/questions/54654376/why-is-publishing-function-not-being-found-in-my-custom-gradle-kts-file-within
signing {
sign(publishing.publications.getByName("release"))
}
}
問(wèn)題:
A problem occurred configuring project ':lib_common_utils'.
> SoftwareComponentInternal with name 'java' not found.
Maven Publish Plugin (gradle.org)
上傳javadoc和source
添加javadoc和jarsource的任務(wù)
tasks.register("javadoc", Javadoc::class.java) {
group = "publishing"
dependsOn("assemble")
source = android.sourceSets["main"].java.getSourceFiles()
classpath += project.files(android.bootClasspath + File.pathSeparator)
if (JavaVersion.current().isJava9Compatible) {
(options as StandardJavadocDocletOptions).addBooleanOption("html5", true)
}
android.libraryVariants.forEach { libraryVariant ->
classpath += libraryVariant.javaCompileProvider.get().classpath
}
options.apply {
encoding("UTF-8")
charset("UTF-8")
isFailOnError = false
(this as StandardJavadocDocletOptions).apply {
addStringOption("Xdoclint:none")
links?.add("https://developer.android.google.cn/reference/")
links?.add("http://docs.oracle.com/javase/8/docs/api/")
}
}
}
tasks.register("jarSource", Jar::class.java) {
group = "publishing"
from(android.sourceSets["main"].java.srcDirs)
archiveClassifier.set("sources")
}
tasks.register("jarJavadoc", Jar::class.java) {
group = "publishing"
dependsOn("javadoc")
val javadoc: Javadoc = tasks.getByName("javadoc") as Javadoc
from(javadoc.destinationDir)
archiveClassifier.set("javadoc")
}
publish中使用
afterEvaluate {
publishing {
publications {
create<MavenPublication>("release") {
from(components.getByName("release"))
groupId = "com.github.hanlyjiang"
artifactId = "android_common_utils"
version = android.defaultConfig.versionName
pom(getMyPom())
// 添加javadoc
artifact(tasks.getByName("jarJavadoc") as Jar)
// 添加source
artifact(tasks.getByName("jarSource") as Jar)
}
}
repositories {
val ossrhCredentials = Action<PasswordCredentials> {
username = properties["ossrhUsername"].toString()
password = properties["ossrhPassword"].toString()
}
// sonar的倉(cāng)庫(kù)蝌矛,地址根據(jù)項(xiàng)目的版本號(hào)來(lái)確定是snapshot還是正式倉(cāng)庫(kù)
maven {
name = "Sonartype"
val releasesRepoUrl = uri("https://oss.sonatype.org/service/local/staging/deploy/maven2")
val snapshotsRepoUrl = uri("https://oss.sonatype.org/content/repositories/snapshots/")
url = if (android.defaultConfig.versionName.toString().endsWith("SNAPSHOT")) snapshotsRepoUrl else releasesRepoUrl
credentials(ossrhCredentials)
// snapshot的地址:
// https://oss.sonatype.org/content/repositories/snapshots/com/github/hanlyjiang/android_common_utils/
}
// 項(xiàng)目本地的倉(cāng)庫(kù)
maven {
name = "ProjectLocal"
val releasesRepoUrl = uri(layout.buildDirectory.dir("repos/releases"))
val snapshotsRepoUrl = uri(layout.buildDirectory.dir("repos/snapshots"))
url = if (android.defaultConfig.versionName.toString().endsWith("SNAPSHOT")) snapshotsRepoUrl else releasesRepoUrl
}
}
}
// https://stackoverflow.com/questions/54654376/why-is-publishing-function-not-being-found-in-my-custom-gradle-kts-file-within
signing {
sign(publishing.publications.getByName("release"))
}
}
? 錯(cuò)誤記錄
400 錯(cuò)誤
maven publish Received status code 400 from server
可能的原因: maven 倉(cāng)庫(kù)分兩個(gè),一個(gè)是snapshot倉(cāng)庫(kù)错英,一個(gè)是release 倉(cāng)庫(kù)入撒,如果將snapshot版本(版本號(hào)帶SNAPSHOT)的包上傳到Release倉(cāng)庫(kù)的地址,則會(huì)報(bào)錯(cuò)椭岩。
參考文章
- Android maven倉(cāng)庫(kù)和jcenter上傳 (shimo.im)
- [OSSRH-55238] Create a open source project for android - Sonatype JIRA
- Gradle - The Central Repository Documentation (sonatype.org)
- Jcenter 停止服務(wù)茅逮,說(shuō)一說(shuō)我們的遷移方案 - InfoQ 寫(xiě)作平臺(tái)
- [使用 Maven Publish 插件 | Android 開(kāi)發(fā)者 | Android Developers (google.cn)](https://developer.android.google.cn/studio/build/maven-publish-plugin?hl=zh-cn#:~:text=使用 Maven Publish 插件 Android Gradle 插件 3.6.0,Gradle 插件會(huì)為應(yīng)用或庫(kù)模塊中的每個(gè)構(gòu)建變體工件創(chuàng)建一個(gè) 組件 ,您可以使用它來(lái)自定義要發(fā)布到 Maven 代碼庫(kù)的 發(fā)布內(nèi)容 簿煌。)