参考:
https://blog.csdn.net/lkl22/article/details/122905477
https://central.sonatype.org/publish/publish-gradle/#performing-a-release-deployment
https://juejin.cn/post/6953598441817636900
https://cloud.tencent.com/developer/article/1188461
https://docs.gradle.org/current/userguide/publishing_maven.html
https://developer.android.google.cn/studio/build/maven-publish-plugin?hl=en#groovy
https://juejin.cn/post/6963633839860088846
官网:
https://issues.sonatype.org/
https://s01.oss.sonatype.org/
成功后查询:
https://search.maven.org/
https://repo1.maven.org/maven2/
0. gradle
基于 gradle 版本 7.2
插件的变化参考:
https://docs.gradle.org/current/userguide/publishing_maven.html#publishing_maven
- 7.0 之后的 gradle 在项目级 build.gradle 内 删除了 allprojects 配置,自定义 maven 需要到 项目级的 settings.gradle 的 dependencyResolutionManagement -> repositories 内添加,示例:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven { allowInsecureProtocol = true
url 'http://127.0.0.1:8081/repository/maven-releases/' }
}
}
1. 备案
https://issues.sonatype.org/projects/OSSRH/summary
在网站内新建问题,新建的问题会填写 url/groupID 等信息
然后会有回复,然后在新建问题内的 host ,备案写入 dns txt
写入成功后,等待审核就好了。
示例:
username -> zhangsan
password -> 123456
审核成功了,账号就能登陆到 nexus 官网了
2. gpg 生成密钥
https://central.sonatype.org/publish/requirements/gpg/
- "gpg --gen-key" 命令生成密钥,会填入对应用户名、邮箱、密钥(示例:111111)
- 将公钥上传到服务器
gpg --keyserver keyserver.ubuntu.com --send-keys CFFCEFB88FC0B9AF6D4AA9195B325FB0C6A4EBE9
gpg --keyserver keys.openpgp.org --send-keys CFFCEFB88FC0B9AF6D4AA9195B325FB0C6A4EBE9
gpg --keyserver pgp.mit.edu --send-keys CFFCEFB88FC0B9AF6D4AA9195B325FB0C6A4EBE9
- 将密钥导出到本地文件夹
gpg --output native.gpg --export-secret-keys C6A4EBE9
3. AndroidStudio 项目内 gradle 配置
1. gradle.properties
在这里配置本地 gpg 文件信息,ossrh 账户信息
signing.keyId=C6A4EBE9
signing.password=111111
signing.secretKeyRingFile= D:\\本地路径\\native.gpg
ossrhUsername=zhangsan
ossrhPassword=123456
2. 模块级 build.gradle
plugins {
id 'com.android.library'
id 'maven-publish'
id 'signing'
}
android {
compileSdk 32
defaultConfig {
minSdk 21
targetSdk 32
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
project.afterEvaluate {
publishing {
repositories {
maven {
name 'test'
url 'http://127.0.0.1:8081/repository/maven-releases/'
credentials {
username = 'admin'
password = '123456'
}
}
maven {
name 'maven'
url 'https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/'
credentials {
username = 'zhangsan'
password = '123456'
}
}
}
publications {
maven(MavenPublication) {
groupId = 'com.baidu.module'
artifactId = "chat"
version = "1.0"
artifact bundleReleaseAar
//artifact sourcesJar
//artifact generateSourcesJar
//artifact javadocJar
//from components.release
artifacts {}
pom {
name = 'My Library'
description = 'A concise description of my library'
url = 'http://www.baidu.com/library'
inceptionYear = '2023'
licenses {
license {
name = 'The Apache License, Version 2.0'
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id = 'baidu'
name = 'zhangsan'
email = 'zhangsan@baidu.ccom'
}
}
scm {
connection = 'scm:git:git://baidu.com/my-library.git'
developerConnection = 'scm:git:ssh://baidu.com/my-library.git'
url = 'http://baidu.com/my-library/'
}
}
}
}
}
}
signing {
sign publishing.publications
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
账号等信息都填写正确,然后点击右侧 publishMavenPublicationToMavenRepository 发布
4. 最终发布
登录 nuxus 后,在 Staging Repositories 选中项目,然后点 close ,close 没问题的话,最后点 release 就发布了。
5. 本地 nexus 仓库
nexus 安装:
https://www.sonatype.com/products/repository-oss-download
启动命令
.\nexus.exe /install
.\nexus.exe /start
.\nexus.exe /run
.\nexus.exe /uninstall
.\nexus.exe /stop
本地访问 http://127.0.0.1:8081/#browse/browse
账号和密码在安装时可见,admin、admin.password
问题总结
- 网上大部分 gradle 7.0 之前的版本示例代码,7.0 后做了一点改变,按上面示例就行
- sonatype 内建立项目是通过提出问题,然后审核 才通过的
网友评论