项目结构
project
|__ app
|__ build.gradle
|__ lib_aa
|__ build.gradle
|__ lib_bb
|__ build.gradle
|__ build.gradle
|__ gradle.properties
|__ publishmaven.gradle
|__ publishmaven.sh
封装 maven-publish 的 publishmaven.gradle 脚本:
apply plugin: 'maven-publish'
// 后缀日期
def data = new Date().format("yyyyMMdd", TimeZone.getTimeZone("GMT+08:00"))
afterEvaluate { project ->
try {
if (publishProjectName != null
&& !publishProjectName.isEmpty()
&& publishProjectName != project.name) {
return
}
def groupId = project.ext["GROUP_ID"]
def artifactId = project.ext["ARTIFACT_ID"]
def version = "${app.versionName}-$data-SNAPSHOT"
println "配置 ${project.name} 工程 maven 打包插件: { groupId : $groupId, artifactId : $artifactId, version : $version } "
publishing {
publications {
release(MavenPublication) {
// 可根据自己需求打 debug 包还是 release 包
// from project.components.debug
from project.components.release
it.groupId = groupId
it.artifactId = artifactId
it.version = version
}
}
repositories {
maven {
allowInsecureProtocol true
// 自己的 maven 私服仓库地址
url = "http://nexus.devmobile.com/repository/android-snapshots/"
credentials {
// 自己的 maven 私服账号
username = "android"
// 自己的 maven 私服密码
password = "123456"
}
}
}
}
} catch (Exception e) {
// 忽略未配置打包信息的 project
}
}
批量打包上传 publishmaven.sh 脚本:
#!/bin/bash
# 全量打包
#./gradlew publish
./gradlew publish -PpublishProjectName=lib_aa
./gradlew publish -PpublishProjectName=lib_bb
使用
1. 在主工程的 build.gradle 中配置:
allprojects {
repositories {
// maven-publish 下载所需仓库配置
maven { url 'https://repo1.maven.org/maven2/' }
}
}
2. 在主工程 gradle.properties 中配置:
# 单独为模块或库打包上传,不要加引号,不填时表示所有配置的模块或库都会打包上传,例: lib_aa
publishProjectName=
3. 在子工程 build.gradle 中配置:
apply from: '../publishmaven.gradle'
// 配置此模块上传 maven 的 groupId
this.ext.GROUP_ID = 'com.xxxx.lib'
// 配置此模块上传 maven 的 artifactId
this.ext.ARTIFACT_ID = 'aa'
4. 批量打包上传:
./publishmaven.sh