Publish an Android library to Maven with AAR ,android studio maven aar nexus 上传aar android上传aar到Maven Central 2022年最新最全
1. Github
1.1 注册github
点击下面链接进行注册
2. 注册sonatype账号
2.1 注册
输入Email,Full name,Username,Password,输入验证码,最后点击Sign up就可以注册了。
2.2 登录
2.3 新建group id
登录之后点击新建
按照下图中的步骤一步一步来即可,最后别忘记点击新建。
新建完成之后我们需要在github新建一个仓库,证明你是这个github的使用者(例如这个是我的 https://github.com/996dev)
3. GPG
3.1 下载软件
下载对应系统的文件
下载完成之后点击安装,直到完成。
3.2 生成密钥
创建密钥命令,在创建的时候一定要记住设置的密码,后面要用的
gpg --full-generate-key
导出私钥,这个后面会用的到,使用下面命令进行导出
gpg --export-secret-keys -o secring.gpg
3.3 上传公钥到服务器
上传到这个三个地方keyserver.ubuntu.com,pgp.mit.edu,keys.openpgp.org
gpg --keyserver hkp://xxxx.xxx --send-keys YOUR KEY ID
3.3.1上传
使用下面命令进行上传
gpg --keyserver keyserver.ubuntu.com --send-keys YOUR KEY IDgpg --keyserver pgp.mit.edu --send-keys YOUR KEY IDgpg --keyserver keys.openpgp.org --send-keys YOUR KEY ID
keys.openpgp.org 命令行出问题使用下面的网页直接上传https://keys.openpgp.org/uploa
keys.openpgp.org 命令行出问题使用下面的网页直接上传
https://keys.openpgp.org/uploa
3.3.2 验证上传是否成功
使用下面的命令进行验证是否上传成功
gpg --keyserver keyserver.ubuntu.com --recv-keys YOUR KEY ID
gpg --keyserver keys.openpgp.org --recv-keys YOUR KEY ID
gpg --keyserver pgp.mit.edu --recv-keys YOUR KEY ID
4. android gradle
4.1 publish-mavencentral.gradle
新建publish-mavencentral.gradle文件
apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
apply plugin: 'signing'
//生成文档注释
task androidJavadocs(type: Javadoc) {
failOnError = false
source = android.sourceSets.main.java.srcDirs
ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
classpath += files(ext.androidJar)
}
//将文档打包成jar
task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
archiveClassifier.set('javadoc')
from androidJavadocs.destinationDir
}
//将源码打包
task androidSourcesJar(type: Jar) {
archiveClassifier.set('sources')
from android.sourceSets.main.java.srcDirs
}
artifacts {
archives androidSourcesJar
archives androidJavadocsJar
}
//加载资源
Properties properties = new Properties()
InputStream inputStream = project.rootProject.file('gradle.properties').newDataInputStream();
properties.load(inputStream)
// Because the components are created only during the afterEvaluate phase, you must
// configure your publications using the afterEvaluate() lifecycle method.
afterEvaluate {
publishing {
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 = properties.getProperty('GROUP_ID')
artifactId = project.artifactId
version = project.artifactVersion
// Adds Javadocs and Sources as separate jars.
// artifact file("$buildDir/outputs/aar/${project.name}-release.aar")
artifact androidSourcesJar
artifact androidJavadocsJar
pom {
name = project.artifactName
packaging = 'aar'
description = project.artifactDescription
url = project.githubUrl
licenses {
license {
name = 'The Apache License, Version 2.0'
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id = '996dev'
name = '996dev'
email = '996dev@996.dev'
}
}
scm {
url = project.githubUrl
connection = "scm:git@${project.githubHttps}"
developerConnection = "scm:${project.githubSSH}"
}
}
}
}
repositories {
maven {
def releasesRepoUrl = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
def snapshotsRepoUrl = "https://s01.oss.sonatype.org/content/repositories/snapshots/"
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
credentials {
username = properties.getProperty('ossrhUsername')
password = properties.getProperty('ossrhPassword')
}
}
}
}
signing {
sign publishing.publications.release
}
}
4.2 moudle中使用上传gradle
apply from: "${rootProject.projectDir}/publish-mavencentral.gradle"
ext {
artifactId = "toast"
artifactName = 'toast'
artifactDescription = "androidx toast"
artifactVersion = "0.0.1"
githubUrl = "https://github.com/996dev/toast"
githubHttps = "https://github.com/996dev/toast.git"
githubSSH = "git@github.com:996dev/toast.git"
}
5. 配置账号密码
在gradle.propertis 填写下面的信息
signing.password=刚刚生成的密钥时输入的密码
signing.secretKeyRingFile=D\:\\Key\\secring.gpg 上面到处私钥
ossrhUsername=sonatype账号
ossrhPassword=sonatype密码