最近項目中有個需求,其中關鍵一環是發佈不一樣Module變體到Maven庫。一開始沒意識到這點,經過經常使用的apply plugin: 'maven'
插件配置寫法,發現執行uploadArchives
任務成功,但實際上並無產物生成。且uploadArchives
所依賴各task也並無執行,期間無其餘有效提示日誌信息。後來終於想到應該是構建變體緣由,經與同事交流,最終發現能夠使用maven-publish
插件。html
實際上Android官網上已經有針對不一樣Module變體到Maven庫的指引文檔,且所使用的正是maven-publish
插件。java
Android Gradle 插件 3.6.0 及更高版本支持 Maven Publish Gradle 插件,可以讓您將構建工件發佈到 Apache Maven 代碼庫。Android Gradle 插件會爲應用或庫模塊中的每一個構建變體工件建立一個組件,您能夠使用它來自定義要發佈到 Maven 代碼庫的發佈內容。android
maven-publish
插件功能至關強大,不只能夠區分變體發佈模塊AAR,同時還支持發佈AAB甚至APK文件。markdown
具體文檔地址:
developer.android.com/studio/buil…
docs.gradle.org/current/use…app
1,官網發佈AAR示例:maven
// 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 = 'com.example.MyLibrary'
artifactId = 'final'
version = '1.0'
}
// Creates a Maven publication called 「debug」.
debug(MavenPublication) {
// Applies the component for the debug build variant.
from components.debug
groupId = 'com.example.MyLibrary'
artifactId = 'final-debug'
version = '1.0'
}
}
}
複製代碼
2,項目中使用的配置,其中部分腳本示例:ide
apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
task androidJavadocs(type: Javadoc) {
// 設置源碼所在的位置
source = android.sourceSets.main.java.srcDirs
exclude '**/R.html', '**/R.*.html', '**/index.html'
}
// 生成javadoc.jar
task androidJavadocsJar(type: Jar) {
// 指定文檔名稱
archiveClassifier.set('javadoc')
from androidJavadocs.destinationDir
}
// 生成sources.jar
task androidSourcesJar(type: Jar) {
archiveClassifier.set('sources')
from android.sourceSets.main.java.sourceFiles
}
afterEvaluate {
publishing {
publications {
thGoogleSdk(MavenPublication) {
groupId = 'com.corn.pay'
artifactId = project.getName()
version = '1.1.3-SNAPSHOT'
pom.withXml{
def dependenciesNode = asNode().appendNode("dependencies")
configurations.implementation.allDependencies.forEach(){
Dependency dependency ->
if (dependency.version != "unspecified" && dependency.name != "unspecified"){
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', dependency.group)
dependencyNode.appendNode('artifactId', dependency.name)
dependencyNode.appendNode('version', dependency.version)
}
}
}
artifact androidJavadocsJar
artifact androidSourcesJar
artifact(tasks.getByName("bundleThGoogleSdkReleaseAar"))
}
}
repositories {
maven {
def devUrl = "https://maven.xxxx.com/nexus"
def user = 'xxxx'
def pass = 'yyyy'
String target = pom_target
if (target == "release") {
url = "${devUrl}/content/repositories/releases/"
credentials {
username = user
password = pass
}
} else if (target == "local") {
// test, upload local maven repository
url = "file:" + new File(project.rootProject.rootDir, "mavenlocal").path
} else {
url = "${devUrl}/content/repositories/snapshots"
credentials {
username = user
password = pass
}
}
}
}
}
}
複製代碼