Android发布aar到Maven仓库
一、配置步骤
方式一
在要发布的子模块的 build.gradle 文件中做如下配置:
- 添加 maven-publish 插件
plugins {
id 'com.android.library'
id 'maven-publish'
}
- 添加 publish 配置
groupId = 'com.example.android'
artifactId = 'demo'
version = '1.0.0'
// 发布源码
task sourcesJar(type: Jar){
from android.sourceSets.main.java.srcDirs
archiveClassifier.set("sources")
}
// 发布文档
task javadocJar(type: Jar){
from android.sourceSets.main.java.srcDirs
archiveClassifier.set("javadoc")
}
afterEvaluate {
publishing {
publications {
release(MavenPublication) {
from components.release
artifact sourcesJar
artifact javadocJar
groupId = groupId
artifactId = artifactId
version = version
}
}
repositories {
maven {
def releaseRepoUrl = "http://192.168.1.110:8081/repository/maven-releases/"
def snapshotsRepoUrl = "http://192.168.1.110:8081/repository/maven-snapshots/"
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releaseRepoUrl
allowInsecureProtocol(true)
credentials {
username = "admin"
password = "admin123"
}
}
}
}
}
方式二(推荐)
-
复制上传模板到项目根目录。
-
修改模板中的group(可选)
-
修改模板中的仓库地址(可选)
-
子模块中的 build.gradle 添加 maven-publish 插件
plugins { id 'com.android.library' id 'maven-publish' }
-
添加版本号
version = '1.0.0'
-
引入模板
apply from: '../uploadArchive.gradle'
二、引用私有仓库的依赖
根目录中的 settings.gradle 添加 maven私有仓库地址
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
maven {
url 'http://192.168.1.XXX:8181/repository/maven-releases/'
allowInsecureProtocol(true)
}
google()
mavenCentral()
}
}
项目中的 build.gradle 添加引用依赖
dependencies {
...
implementation 'com.alibaba:fastjson:2.0.22'
}
三、方式一完整配置示例
//apply plugin: 'maven-publish'
plugins {
id "maven-publish"
}
groupId = 'com.example.android'
artifactId = 'demo'
version = '1.0.0'
// 发布源码
task sourcesJar(type: Jar){
from android.sourceSets.main.java.srcDirs
archiveClassifier.set("sources")
}
// 发布文档
task javadocJar(type: Jar){
from android.sourceSets.main.java.srcDirs
archiveClassifier.set("javadoc")
}
afterEvaluate {
publishing {
publications {
release(MavenPublication) {
from components.release
artifact sourcesJar
artifact javadocJar
groupId = groupId
artifactId = artifactId
version = version
pom {
name = 'My Library'
description = 'A concise description of my library'
url = 'http://www.example.com/library'
properties = [
myProp: "value",
"prop.with.dots": "anotherValue"
]
licenses {
license {
name = 'The Apache License, Version 2.0'
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id = 'dev'
name = 'developer'
email = 'developer@example.com'
}
}
scm {
connection = 'scm:git:git://example.com/my-library.git'
developerConnection = 'scm:git:ssh://example.com/my-library.git'
url = 'http://example.com/my-library/'
}
}
}
}
repositories {
maven {
def releaseRepoUrl = "http://192.168.1.XXX:8181/repository/maven-releases/"
def snapshotsRepoUrl = "http://192.168.1.XXX:8181/repository/maven-snapshots/"
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releaseRepoUrl
allowInsecureProtocol(true)
credentials {
username = "admin"
password = "admin123"
}
}
}
}
}
附录
uploadArchive.gradle 源码:
group = "com.example.android"
def releaseRepoUrl = "http://192.168.1.XXX:8181/repository/maven-releases/"
def snapshotsRepoUrl = "http://192.168.1.XXX:8181/repository/maven-snapshots/"
// 发布源码
task sourcesJar(type: Jar){
from android.sourceSets.main.java.srcDirs
archiveClassifier.set("sources")
}
// 发布文档
task javadocJar(type: Jar){
from android.sourceSets.main.java.srcDirs
archiveClassifier.set("javadoc")
}
afterEvaluate {
publishing {
publications {
release(MavenPublication) {
from components.release
groupId = group
artifactId = project.name
version = version
pom {
name = project.name
description = 'Android SDK Library'
url = 'http://www.myandroid.com/library'
}
}
}
repositories {
maven {
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releaseRepoUrl
allowInsecureProtocol(true)
credentials {
username = "admin"
password = "admin123"
}
}
}
}
}