美文网首页
android maven发布插件使用指南

android maven发布插件使用指南

作者: 天天听听 | 来源:发表于2020-12-08 20:36 被阅读0次

    前置

    maven发布插件可以发布产物到 Apache Maven 代码库。Android Gradle 插件会为应用或库模块中的每个构建变体工件创建一个组件,您可以使用它来自定义要发布到 Maven 代码库的发布内容
    需要Android Gradle 插件 3.6.0 及更高版本。

    Android Gradle 插件 发布内容工件 组件名称
    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'
    

    参数配置:

    1. 在组件build.gralde的顶级声明版本号,必须使用version属性名,这个一个已经声明的属性,如果自定义,会导致多组件打包时,版本出现未定义的问题:
    version = '1.0.0'
    
    1. 声明组织,一般是包名,最好统一定义下:
    group = 'com.xxx.xxx'
    
    1. java组件,非android组件,声明打包源码,如果不想上传源码,可以不必配置:
    java {
        withSourcesJar()
        //注释支持
        withJavadocJar()
    }
    
    1. android组件,如果想上传源码,需要自定义产物上传任务:
    //生成源码jar包task,type表示继承Jar打包任务。
    task sourcesJar(type: Jar) {
        from android.sourceSets.main.java.srcDirs
        archiveClassifier.set("sources")
    }
    
    1. 发布配置-仓库配置,可以配置多个发布仓库,选择一次性发布到多个仓库:
        repositories {
            maven {
                ////如果不指定名称,默认为maven
                name = "release"
                url = repository_release
                credentials {
                    username = Username
                    password = Password
                }
            }
            maven {
                name = "snapshot"
                url = repository_snapshot
                credentials {
                    username = Username
                    password = Password
                }
            }
        }
    }
    
    1. 发布配置-产物发布器配置:

    在publishing标签内,参数说明

    mavenJava 发布器的名称,可以自定义,如果是android组件,可以使用release,debug等变体
    from components.java 打包来源配置,使用内置的java变体,如果是android,需要使用release,debug等变体
    MavenPublication 使用maven发布器,另一种发布是Ivy,有兴趣可以去了解下
    groupId 组织名,默认key是group,这里自定义了
    pom pom文件配置,看注释了解下
    publications {
        mavenJava(MavenPublication) {
            groupId = groupName
            from components.java
    
            pom {
                name = artifactId
                description = moduleDescription
                url = 'xxx'
                licenses {
                    //证书说明
                    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 {
                        //证书说明
                        license {
                            name = 'xxx'
                            url = 'null'
                        }
                    }
                    developers {
                        developer {
                            id = 'xxx'
                            name = 'GaoBingqiu'
                            email = 'xxx@qq.com'
                        }
                    }
                    scm {
                        connection = 'xxxx'
                        developerConnection = 'xxx'
                        url = 'http://xxxx/_git/'
                    }
                }
            }
    

    组件导入:

    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")
    }
    
    //等待构建完成
    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
                    //上次源码任务
                    artifact(tasks["sourcesJar"])
    
                    pom {
                        name = artifactId
                        description = moduleDescription
                        url = 'xxx'
                        licenses {
                            //证书说明
                            license {
                                name = 'xxx.'
                                url = 'null'
                            }
                        }
                        developers {
                            developer {
                                id = 'xxx'
                                name = 'GaoBingqiu'
                                email = 'xx@qq.com'
                            }
                        }
                        //软件配置管理
                        scm {
                            connection = 'xxx'
                            developerConnection = 'xxx'
                            url = 'xxx'
                        }
                    }
                }
            }
    

    组件导入:

    ext {
        moduleDescription = "A router for module,main sdk"
    }
    
    version = '1.0.0-beta01'
    apply from: rootProject.projectDir.path + '/mavenAndroid.gradle'
    

    三、发布步骤

    完成配置后,会在右边gralde任务列表里面。

    java组件:
    待补充
    android组件:
    待补充
    任务说明:

    1. generateMetadataFileFoxxxPublication:生成组件配置数据,会在 build/publications/$xxx/里面,一般比较少用
    2. generatePomFileForxxxPublication:生成pom文件,在build/publications/$xxx/里面,一般比较少用
    3. publish:遍历所有发布器,发布到所有的仓库,不包括本地仓库。
    4. publishAllPublicationsToxxxRepository:遍历所有发布器,发布到对应的仓库,这里是发布到release仓库里面。
    5. publishxxxPublicationToMavenLocal:将xxx发布器,发布到本地仓库,这里的发布器的mavenJava/release
    6. publishxxxPublicationToxxxRepository:将xxx发布器,发布到xxx仓库,这里的发布器的mavenJava/release,仓库是relase仓库
    7. publishToMavenLocal:遍历所有发布器,发布到本地仓库。

    一般本地开发,只需要发布到本地仓库即可。应用的需要添加本地仓库。

    在应用的根build.gradle中:

    allprojects {
        repositories {
           //需要使用本地仓库,默认地址是:C:\Users\userName\.m2
            mavenLocal()
            xxxx
            }
    

    真正发布的时候,再发布到远程开发。

    四、多渠道sdk

    多渠道sdk,只需要声明多个发布器即可,在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       
                }
            }
        }
    }
    

    发布时,根据需要指定发布器。

    五、发布策略

    推荐使用:只使用release仓库发布,一般一个sdk需要经过alpha,beta几个版本后,才能正式发布。这样的策略,我们只需要配置一个仓库。

    老的策略:一开始使用snapshot版本,经过前端测试,集成测试稳定后才能发布正式版本,snapshot是可以覆盖的,是不稳定的版本,release仓库的sdk版本都是唯一的。

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

    相关文章

      网友评论

          本文标题:android maven发布插件使用指南

          本文链接:https://www.haomeiwen.com/subject/wnmqgktx.html