gradle升级之后,早期gradle发布artifact 到maven的脚本提示部分引用找不到,通过gradle 官方给的demo梳理出来 gradle 7.2 以上使用的发布脚本。
新建一个发布脚本文件,功能包含发布Artifact aar 到maven仓库。附带源码方便调试
测试环境:
Gradle 版本:7.4
AGP版本:7.1.3
AS版本:Dolphin 2021.3.1 Patch 1
脚本publish.gradle 文件
apply plugin: 'maven-publish'
apply plugin: 'signing'
ext {
VERSION_CODE = 1
VERSION_NAME = "1.0.0-SNAPSHOT"
GROUP = "com.example.MyLibrary"
POM_NAME = "logview-sdk"
ARTIFACT_ID = "final-debug"
POM_PACKAGING = "aar"
POM_DESCRIPTION = "descript"
POM_URL = ""
POM_SCM_URL = ""
POM_SCM_CONNECTION = ""
POM_SCM_DEV_CONNECTION = ""
POM_LICENCE_NAME = ""
POM_LICENCE_URL = ""
POM_LICENCE_DIST = "repo"
POM_DEVELOPER_ID = "chrisbanes"
POM_DEVELOPER_NAME = "Chris Banes"
NEXUS_USERNAME = "mvn仓库账户"
NEXUS_PASSWORD = "mvn仓库密码"
RELEASE_REPOSITORY_URL = "https://s01.oss.sonatype.org/content/repositories/releases/"
SNAPSHOT_REPOSITORY_URL ="https://s01.oss.sonatype.org/content/repositories/snapshots/"
}
def isReleaseBuild() {
return VERSION_NAME.contains("SNAPSHOT") == false
}
def getReleaseRepositoryUrl() {
return hasProperty('RELEASE_REPOSITORY_URL') ? RELEASE_REPOSITORY_URL
: "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
}
def getSnapshotRepositoryUrl() {
return hasProperty('SNAPSHOT_REPOSITORY_URL') ? SNAPSHOT_REPOSITORY_URL
: "https://oss.sonatype.org/content/repositories/snapshots/"
}
def getRepositoryUsername() {
return hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : ""
}
def getRepositoryPassword() {
return hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : ""
}
def getVersionName(){
return (project.hasProperty('version') && !project.getProperty('version').equals('unspecified'))? project.getProperty('version') : VERSION_NAME
}
def getRepositoryUrl() {
if (isReleaseBuild()) {
return getReleaseRepositoryUrl()
} else {
return getSnapshotRepositoryUrl()
}
}
afterEvaluate {
publishing {
publications {
maven(MavenPublication) {
//如果发布到maven仓库
artifact bundleReleaseAar
artifact sourcesJar
groupId = GROUP
artifactId = ARTIFACT_ID
version = getVersionName()
pom {
name = POM_NAME
packaging = POM_PACKAGING
description = POM_DESCRIPTION
url = POM_URL
licenses {
license {
name = POM_LICENCE_NAME
url = POM_LICENCE_URL
distribution = POM_LICENCE_DIST
}
}
developers {
developer {
id = POM_DEVELOPER_ID
name = POM_DEVELOPER_NAME
}
}
scm {
url = POM_SCM_URL
connection = POM_SCM_CONNECTION
developerConnection = POM_SCM_DEV_CONNECTION
}
}
}
}
repositories {
maven {
allowInsecureProtocol = true
credentials(PasswordCredentials){
username getRepositoryUsername()
password getRepositoryPassword()
}
url getRepositoryUrl()
}
}
}
}
task sourcesJar(type: Jar) {
if (project.hasProperty("kotlin")) {
from android.sourceSets.main.java.getSrcDirs()
} else if (project.hasProperty("android")) {
from android.sourceSets.main.java.sourceFiles
} else {
println project
from sourceSets.main.allSource
}
archiveClassifier='sources'
}
artifacts {
archives sourcesJar
}
//这里的需要配置gpg工具,可以不对artifact进行签名
signing {
required { isReleaseBuild() && gradle.taskGraph.hasTask("publish") }
sign configurations.archives
}
module的build.gradle文件
apply plugin: 'com.android.library'
apply plugin: 'org.jetbrains.kotlin.android'
apply from: 'gradle-mvn-push.gradle'
android {
namespace 'com.codelabs.mylibrary'
compileSdk 32
defaultConfig {
minSdk 25
targetSdk 32
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}
publishing {
// Publishes "fullRelease" build variant with "fullRelease" component created by
// Android Gradle plugin
singleVariant("release")
}
buildTypes {
release {
minifyEnabled false
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
}
tips:
如果配置了sign,需要在gradle.properties文件中配置gpg的
signing.keyId=key
signing.password=密码
signing.secretKeyRingFile=/Users/yangweichao/.gnupg/secring.gpg
gpg插件api说明。https://docs.gradle.org/current/userguide/signing_plugin.html#signing_plugin
使用maven命令发布。(maven下载地址)
首先配置系统环境变量。
mvn deploy:deploy-file
-Durl=http://nexus.xxxxxx.com/nexus/repository/maven-snapshots-android/
-Dfile=/Users/yangweichao/Downloads/unityLibrary-release.aar
-Dpackaging=aar
-DgroupId=com.codeview.miniparty
-DartifactId=unity-sdk
-Dversion=1.0.0-SNAPSHOT
-DrepositoryId=snapshots
配置maven的setting.xml
//全局环境变量
M2_HOME="/Users/yangweichao/Downloads/apache-maven-3.8.6/bin"
<server>
<id>snapshots</id>
<username>账号</username>
<password>密码</password>
</server>
tips:
这里的 snapshots需要与命令行 -DrepositoryId=snapshots 匹配。
异常:
1.未显式声明发布变体
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:dataBindingMergeDependencyArtifactsRelease'.
> Could not resolve all files for configuration ':app:releaseCompileClasspath'.
> Could not resolve com.codeview.miniparty:crashlytics:1.0.0.
Required by:
project :app
> No matching variant of com.codeview.miniparty:crashlytics:1.0.0 was found. The consumer was configured to find an API of a component, preferably optimized f
or Android, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'release', attribute 'com.android.build.api.attributes.AgpVersionAttr'
with value '7.1.3', attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' but:
- Variant 'debugApiElements-published' capability com.codeview.miniparty:crashlytics:1.0.0 declares an API of a component, as well as attribute 'org.jetbr
ains.kotlin.platform.type' with value 'androidJvm':
- Incompatible because this component declares a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug' a
nd the consumer needed a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'release'
- Other compatible attributes:
- Doesn't say anything about com.android.build.api.attributes.AgpVersionAttr (required '7.1.3')
- Doesn't say anything about its target Java environment (preferred optimized for Android)
- Variant 'debugRuntimeElements-published' capability com.codeview.miniparty:crashlytics:1.0.0 declares a runtime of a component, as well as attribute 'or
g.jetbrains.kotlin.platform.type' with value 'androidJvm':
- Incompatible because this component declares a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug' a
nd the consumer needed a component, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'release'
- Other compatible attributes:
- Doesn't say anything about com.android.build.api.attributes.AgpVersionAttr (required '7.1.3')
- Doesn't say anything about its target Java environment (preferred optimized for Android)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
解决方式:参照 LibraryPublishing
2.同时发布多个artifacts 需要指定类别
Maven-plublish: multiple artifacts with the identical extension and classifier
3.发布aar到https://s01.oss.sonatype.org/ 仓库未指定aar
发布到内网maven平台并没有显式指定,如下代码
afterEvaluate {
publishing {
publications {
debug(MavenPublication) {
//如果发布到maven仓库
artifact bundleReleaseAar
...
...
}
}
}
但是发布到maven平台之后,查看pom.xml发现artifact 类型是module,然后无法正常引用通过格式 ,io.github.awhelefall:crashlytics:1.0.0-SNAPSHOT
指导添加上artifact bundleReleaseAar,之后,发布的类别也包含aar了。如下图
4.error Code=400
对于release仓库中同名的资源,并不会覆盖。会返回一个code =400的异常。未发布成功收到400异常,优先检查下发布的资源ARTIFACT_ID 是否已经存在。
待定:
未指定 artifact bundleReleaseAar,无法依赖到maven仓库,发现type类型是module。最后通过设置 artifact bundleReleaseAar,类型更正为aar。通过真实测试,类型为module,是可以依赖到发布到maven仓库的工件。但是是有一定的延时。发上去之后不能里面搜索到
总结:
升级 gradle到7.0+ 之后发现 MavenDeployment 找不到引用,通过gradle官方介绍maven插件升级了,升级之后Api进行调整。本Blog 支持Gradle 7.0+,发布aar到maven仓库。
demo: https://github.com/aWhalefall/AndroidLog
直接进入libcrash module,执行Gradle脚本
引用:
2. LibraryPublishing
3. Using Gradle Plugins
4. Nexus repository manager
5.发布aar包到maven仓库
6.mutipart artifact 需要显示或者隐式声明依赖传递项
7.Maven中央仓库