已有環(huán)境
- macOS Big Sur 11.2.3
- JDK: 1.8.0_242-release
- Android Studio 開發(fā)環(huán)境
更新日志
2022-04-28 10:45:58
pom 的方式存在依賴丟失問題胧瓜,所以手動打包各個依賴(包括多渠道依賴)到 pom 文件中后續(xù)計劃更新新篇章:Android Gradle 6.0+ 的 GMM 方式
本地Maven私服搭建
下載 Nexus Repository OSS
https://help.sonatype.com/repomanager3/download矢棚,OSX archive
配置
解壓下載文件到某個目錄,cd 到 nexus 的 bin 目錄下(當(dāng)然也可以配置到PATH中)府喳,執(zhí)行 ./nexus run
啟動 nexus
打開Nexus管理后臺頁面:http://localhost:8081/蒲肋,登陸,默認用戶名admin
兜粘,默認密碼保存在 ../nexus-3.30.0-01-mac/sonatype-work/nexus3/admin.password
登陸進來后根據(jù)步驟進行配置,nexus-3.30.0-01 的版本的設(shè)置已經(jīng)做成網(wǎng)頁舱沧,直接在網(wǎng)頁操作便可
倉庫類型:
- maven-central 為代理熟吏,用來代理遠程倉庫,也就是可以緩存遠程倉庫(github上的庫等)
- maven-releases/maven-snapshots 為宿主倉庫牵寺,分別用來部署團隊內(nèi)部的發(fā)布版本組件和快照版本組件(也就是僅在內(nèi)網(wǎng)部可用倉庫)
上傳單個渠道到Maven
在 library 模塊下的 build.gradle 最外層添加
apply plugin: 'maven'
uploadArchives {
repositories.mavenDeployer {
name = 'mavenCentralReleaseDeployer' //名稱固定
repository(url: "http://localhost:8081/repository/maven-releases/") { //倉庫地址
authentication(userName: "admin", password: "admin123") //倉庫用戶名和密碼
}
pom.version = '1.0.1' //版本號
pom.artifactId = "testlibrary" //aar名稱
pom.groupId = "com.lunix.testlibrary" //包名
pom.name = "sdk"
pom.packaging = 'aar' //打包格式,aar固定
}
}
上傳多個渠道
為 lib 配置多渠道和上傳配置
android {
...
flavorDimensions("env")
productFlavors {
cpu {
dimension "env"
}
npu {
dimension "env"
}
}
sourceSets {
main {
java.srcDirs += ["${projectDir}/src/main"]
}
cpu {
java.srcDirs += ["${projectDir}/src/cpu"]
}
npu {
java.srcDirs += ["${projectDir}/src/npu"]
}
}
}
...
import org.gradle.api.internal.artifacts.publish.DefaultPublishArtifact //導(dǎo)入類
//配置要上傳的文件信息
artifacts {
// 執(zhí)行上傳的包和類型
archives new DefaultPublishArtifact("npu", "aar", "aar", null, new Date(), new File("${getBuildDir().absolutePath}/outputs/aar", "testlibrary-cpu-release.aar"))
archives new DefaultPublishArtifact("cpu", "aar", "aar", null, new Date(), new File("${getBuildDir().absolutePath}/outputs/aar", "testlibrary-cpu-release.aar"))
}
uploadArchives {
repositories.mavenDeployer {
name = 'mavenCentralReleaseDeployer' //名稱固定
repository(url: "http://localhost:8081/repository/maven-releases/") {
authentication(userName: "admin", password: "admin123")
}
addFilter('npu') { artifact, file ->
artifact.name.contains("npu")
}
addFilter('cpu') { artifact, file ->
artifact.name.contains("cpu")
}
pom('npu').groupId = "com.lunix.testlibrary" //包名
pom('npu').artifactId = "testlibrary" //aar名稱
pom('npu').version = '1.0.3-npu' //版本號
// 2022-04-28 10:40:02 更新帽氓,pom 的方式只能提交一個包含依賴庫的aar包俩块,這里需要單獨處理
// dependences 的依賴問題黎休,即手動打包各個依賴到 pom 中
pom('npu').withXml {
def dependenciesNode = asNode().appendNode('dependencies')
configurations.implementation.allDependencies.each { dependency ->
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', dependency.group)
dependencyNode.appendNode('artifactId', dependency.name)
dependencyNode.appendNode('version', dependency.version)
dependencyNode.appendNode('scope', 'implementation')
}
// just for: npu,如果有 npuImplementation 依賴方式的庫
// configurations.npuImplementation.allDependencies.each { dependency ->
// def dependencyNode = dependenciesNode.appendNode('dependency')
// dependencyNode.appendNode('groupId', dependency.group)
// dependencyNode.appendNode('artifactId', dependency.name)
// dependencyNode.appendNode('version', dependency.version)
// dependencyNode.appendNode('scope', 'compile')
// }
}
pom('cpu').groupId = "com.lunix.testlibrary" //包名
pom('cpu').artifactId = "testlibrary" //aar名稱
pom('cpu').version = '1.0.3-cpu' //版本號
// 2022-04-28 10:40:02 更新玉凯,pom 的方式只能提交一個包含依賴庫的aar包势腮,這里需要單獨處理
// dependences 的依賴問題,即手動打包各個依賴到 pom 中
pom('cpu').withXml {
def dependenciesNode = asNode().appendNode('dependencies')
configurations.implementation.allDependencies.each { dependency ->
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', dependency.group)
dependencyNode.appendNode('artifactId', dependency.name)
dependencyNode.appendNode('version', dependency.version)
dependencyNode.appendNode('scope', 'implementation')
}
// just for: cpu漫仆,如果有 cpuImplementation 依賴方式的庫
// configurations.cpuImplementation.allDependencies.each { dependency ->
// def dependencyNode = dependenciesNode.appendNode('dependency')
// dependencyNode.appendNode('groupId', dependency.group)
// dependencyNode.appendNode('artifactId', dependency.name)
// dependencyNode.appendNode('version', dependency.version)
// dependencyNode.appendNode('scope', 'compile')
// }
}
}
}
然后執(zhí)行編譯和上傳
# 項目目錄終端
$ ./gradlew :testlibrary:aR # 編譯
$ ./gradlew :testlibrary:uploadArchives # 上傳
補充:另外一個寫法
apply plugin: 'maven-publish'
publishing {
publications {
npu(MavenPublication) {
groupId = "com.lunix.testlibrary" //包名
artifactId = "testlibrary" //aar名稱
version = '1.0.3-npu' //版本號
artifact "${getBuildDir().absolutePath}/outputs/aar/testlibrary-npu-release.aar"
}
cpu(MavenPublication) {
groupId = 'com.lunix.testlibrary' //包名
artifactId = "testlibrary" //aar名稱
version = '1.0.3-cpu' //版本號
artifact "${getBuildDir().absolutePath}/outputs/aar/testlibrary-cpu-release.aar"
}
}
repositories {
maven {
url = 'http://localhost:8081/repository/maven-releases/'
credentials {
username 'admin'
password 'admin123'
}
}
}
}
編譯上傳
# 項目目錄終端
$ ./gradlew :testlibrary:aR # 編譯
$ ./gradlew :testlibrary:publish # 上傳
后臺管理頁面
單獨上傳某個庫的捎拯,注釋掉其中一個 archives
庫導(dǎo)入/使用
- 配置私服地址,根目錄 build.gradle 在 repositories 節(jié)點增加
allprojects {
repositories {
...
maven { url = 'http://localhost:8081/repository/maven-releases/' }
}
}
- 需要使用到庫的模塊的 build.gradle 配置
implementation 'com.lunix.testlibrary:testlibrary:1.0.3-cpu'