android maven發(fā)布插件使用指南

前置

maven發(fā)布插件可以發(fā)布產(chǎn)物到 Apache Maven 代碼庫(kù)。Android Gradle 插件會(huì)為應(yīng)用或庫(kù)模塊中的每個(gè)構(gòu)建變體工件創(chuàng)建一個(gè)組件,您可以使用它來(lái)自定義要發(fā)布到 Maven 代碼庫(kù)的發(fā)布內(nèi)容。
需要Android Gradle 插件 3.6.0 及更高版本。

Android Gradle 插件 發(fā)布內(nèi)容工件 組件名稱
com.android.library AAR components.variant
com.android.application APK 和可用的 ProGuard 或 R8 映射文件的 ZIP components.variant_apk
com.android.application Android App Bundle (AAB) components.variant_aab

一、使用

在組件的build.gralde中:
gradle4.0,新的集成方式:

plugins {
    id 'maven-publish'
}

其他:

apply plugin: 'maven-publish'

參數(shù)配置:

  1. 在組件build.gralde的頂級(jí)聲明版本號(hào),必須使用version屬性名,這個(gè)一個(gè)已經(jīng)聲明的屬性,如果自定義,會(huì)導(dǎo)致多組件打包時(shí),版本出現(xiàn)未定義的問(wèn)題:
version = '1.0.0'
  1. 聲明組織,一般是包名,最好統(tǒng)一定義下:
group = 'com.xxx.xxx'
  1. java組件,非android組件,聲明打包源碼,如果不想上傳源碼,可以不必配置:
java {
    withSourcesJar()
    //注釋支持
    withJavadocJar()
}
  1. android組件,如果想上傳源碼,需要自定義產(chǎn)物上傳任務(wù):
//生成源碼jar包task,type表示繼承Jar打包任務(wù)。
task sourcesJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    archiveClassifier.set("sources")
}
  1. 發(fā)布配置-倉(cāng)庫(kù)配置,可以配置多個(gè)發(fā)布倉(cāng)庫(kù),選擇一次性發(fā)布到多個(gè)倉(cāng)庫(kù):
    repositories {
        maven {
            ////如果不指定名稱,默認(rèn)為maven
            name = "release"
            url = repository_release
            credentials {
                username = Username
                password = Password
            }
        }
        maven {
            name = "snapshot"
            url = repository_snapshot
            credentials {
                username = Username
                password = Password
            }
        }
    }
}
  1. 發(fā)布配置-產(chǎn)物發(fā)布器配置:

在publishing標(biāo)簽內(nèi),參數(shù)說(shuō)明

mavenJava 發(fā)布器的名稱,可以自定義,如果是android組件,可以使用release,debug等變體
from components.java 打包來(lái)源配置,使用內(nèi)置的java變體,如果是android,需要使用release,debug等變體
MavenPublication 使用maven發(fā)布器,另一種發(fā)布是Ivy,有興趣可以去了解下
groupId 組織名,默認(rèn)key是group,這里自定義了
pom pom文件配置,看注釋了解下
publications {
    mavenJava(MavenPublication) {
        groupId = groupName
        from components.java

        pom {
            name = artifactId
            description = moduleDescription
            url = 'xxx'
            licenses {
                //證書(shū)說(shuō)明
                license {
                    name = 'xxxx'
                    url = 'xxxx'
                }
            }
            developers {
                developer {
                    id = 'xxx'
                    name = 'xxx'
                    email = 'gaobingqiu@qq.com'
                }
            }
            //軟件配置管理
            scm {
                connection = 'xxx'
                developerConnection = 'xxx'
                url = 'xxx'
            }
        }
    }
}

二、參考配置

java組件的參考:mavenJava.gradle

apply plugin: 'maven-publish'

java {
    withSourcesJar()
    withJavadocJar()
}

publishing {
    repositories {
        maven {
            name = "release"
            url = repository_release
            credentials {
                 username = Username
                 password = Password
            }
        }
    }

    publications {

        mavenJava(MavenPublication) {
            groupId = groupName
            from components.java

            pom {
                name = artifactId
                description = moduleDescription
                url = 'xxx'
                licenses {
                    //證書(shū)說(shuō)明
                    license {
                        name = 'xxx'
                        url = 'null'
                    }
                }
                developers {
                    developer {
                        id = 'xxx'
                        name = 'GaoBingqiu'
                        email = 'xxx@qq.com'
                    }
                }
                scm {
                    connection = 'xxxx'
                    developerConnection = 'xxx'
                    url = 'http://xxxx/_git/'
                }
            }
        }

組件導(dǎo)入:

ext {
    moduleDescription = "A router for module,the module is only handle annotation,need't packed"
}

version = '1.0.0-beta01'

apply from: rootProject.projectDir.path + '/mavenJava.gradle

android組件的參考:mavenAndroid.gradle

apply plugin: 'maven-publish'

group = groupName

//生成源碼jar包task
task sourcesJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    archiveClassifier.set("sources")
}

//等待構(gòu)建完成
afterEvaluate {
    publishing {
        repositories {
            maven {
                name = "release"
                url = repository_release
                credentials {
                    username = Username
                    password = Password
                }
            }
        }

        publications {
            // Creates a Maven publication called "release".
            release(MavenPublication) {
                // Applies the component for the release build variant.
                from components.release
                //上次源碼任務(wù)
                artifact(tasks["sourcesJar"])

                pom {
                    name = artifactId
                    description = moduleDescription
                    url = 'xxx'
                    licenses {
                        //證書(shū)說(shuō)明
                        license {
                            name = 'xxx.'
                            url = 'null'
                        }
                    }
                    developers {
                        developer {
                            id = 'xxx'
                            name = 'GaoBingqiu'
                            email = 'xx@qq.com'
                        }
                    }
                    //軟件配置管理
                    scm {
                        connection = 'xxx'
                        developerConnection = 'xxx'
                        url = 'xxx'
                    }
                }
            }
        }

組件導(dǎo)入:

ext {
    moduleDescription = "A router for module,main sdk"
}

version = '1.0.0-beta01'
apply from: rootProject.projectDir.path + '/mavenAndroid.gradle'

三、發(fā)布步驟

完成配置后,會(huì)在右邊gralde任務(wù)列表里面。

java組件:
待補(bǔ)充
android組件:
待補(bǔ)充
任務(wù)說(shuō)明:

  1. generateMetadataFileFoxxxPublication:生成組件配置數(shù)據(jù),會(huì)在 build/publications/$xxx/里面,一般比較少用。
  2. generatePomFileForxxxPublication:生成pom文件,在build/publications/$xxx/里面,一般比較少用
  3. publish:遍歷所有發(fā)布器,發(fā)布到所有的倉(cāng)庫(kù),不包括本地倉(cāng)庫(kù)。
  4. publishAllPublicationsToxxxRepository:遍歷所有發(fā)布器,發(fā)布到對(duì)應(yīng)的倉(cāng)庫(kù),這里是發(fā)布到release倉(cāng)庫(kù)里面。
  5. publishxxxPublicationToMavenLocal:將xxx發(fā)布器,發(fā)布到本地倉(cāng)庫(kù),這里的發(fā)布器的mavenJava/release
  6. publishxxxPublicationToxxxRepository:將xxx發(fā)布器,發(fā)布到xxx倉(cāng)庫(kù),這里的發(fā)布器的mavenJava/release,倉(cāng)庫(kù)是relase倉(cāng)庫(kù)
  7. publishToMavenLocal:遍歷所有發(fā)布器,發(fā)布到本地倉(cāng)庫(kù)。

一般本地開(kāi)發(fā),只需要發(fā)布到本地倉(cāng)庫(kù)即可。應(yīng)用的需要添加本地倉(cāng)庫(kù)。

在應(yīng)用的根build.gradle中:

allprojects {
    repositories {
       //需要使用本地倉(cāng)庫(kù),默認(rèn)地址是:C:\Users\userName\.m2
        mavenLocal()
        xxxx
        }

真正發(fā)布的時(shí)候,再發(fā)布到遠(yuǎn)程開(kāi)發(fā)。

四、多渠道sdk

多渠道sdk,只需要聲明多個(gè)發(fā)布器即可,在from中指定變體的類型:

afterEvaluate {
    publishing {
        publications {
            // Creates a Maven publication called "release".
            chinaRelease(MavenPublication) {
                // Applies the component for the release build variant.
                from components.chinaRelease
            }
            // Creates a Maven publication called “debug”.
            chinaDebug(MavenPublication) {
                // Applies the component for the debug build variant.
                from components.chinaDebug       
            }
        }
    }
}

發(fā)布時(shí),根據(jù)需要指定發(fā)布器。

五、發(fā)布策略

推薦使用:只使用release倉(cāng)庫(kù)發(fā)布,一般一個(gè)sdk需要經(jīng)過(guò)alpha,beta幾個(gè)版本后,才能正式發(fā)布。這樣的策略,我們只需要配置一個(gè)倉(cāng)庫(kù)。

老的策略:一開(kāi)始使用snapshot版本,經(jīng)過(guò)前端測(cè)試,集成測(cè)試穩(wěn)定后才能發(fā)布正式版本,snapshot是可以覆蓋的,是不穩(wěn)定的版本,release倉(cāng)庫(kù)的sdk版本都是唯一的。

參考文檔:
https://developer.android.com/studio/build/maven-publish-plugin
https://docs.gradle.org/current/userguide/publishing_maven.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容