使用Maven-Publish发布多渠道aar包

使用Maven-Publish发布多渠道aar包_第1张图片

  1. 在module中配置多个渠道
android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        
    //配置多个渠道
    productFlavors {
        cqtest {
            dimension = "full"
        }
        pre {
            dimension = "full"

        }
        dev {
            dimension = "full"
        }
    }
}
  1. 在module的目录下新建一个gradle文件(这里我将其命名为mavenpublish.gradle)(可以通过复制build.gradle文件的方式来创建一个gradle文件),该文件与module中的build.gradle文件同级。如图所示:使用Maven-Publish发布多渠道aar包_第2张图片

  2. 在新建的gradle文件中添加如下脚本

apply plugin: 'maven-publish'

publishing {
    publications {
        //重庆测试环境
        cqtest(MavenPublication) {//cqtest即为渠道的名称,可以随意取
            groupId = 'cn.companyname'//公司域名
            artifactId = 'lib-mycar-cqtest'//该aar包的名称
            version = '1.0.6'//版本号
            def projectName = project.getName()
            artifact "build/outputs/aar/mycar-cqtest-release.aar"//对应该渠道打包出来的aar所在的目录(如何打包请往下看)
            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)
                        }
                }
            }
        }

        //开发环境
        dev(MavenPublication) {
            groupId = 'cn.companyname'
            artifactId = 'lib-mycar-dev'
            version = '1.0.6'
            def projectName = project.getName()
            artifact "build/outputs/aar/mycar-dev-release.aar"
            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)
                        }
                }
            }
        }

        //预发布环境
        pre(MavenPublication) {
            groupId = 'cn.companyname'
            artifactId = 'lib-mycar-pre'
            version = '1.0.6'
            def projectName = project.getName()
            artifact "build/outputs/aar/mycar-pre-release.aar"
            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)
                        }
                }
            }
        }
    }
    repositories {
        maven {
        	//maven仓库的地址
			url = "http://nexus.isuanyun.com/repository/maven-public/"
			//仓库的用户名及密码
              credentials {
                  username ''
                 password ''
              }
             //这是一个本地仓库地址即D盘的maventestrepository文件夹,可以用来测试发布aar包,发布之后的aar将存放于该地址
            //url = "file://d:/maventestrepository"
        }
    }
}

  1. 在module的build.gradle中添加如下脚本
  • 在build.gradle的最顶部添加 apply from: ‘mavenpublish.gradle’
//把mavenpublish换成自己的文件名
apply from: 'mavenpublish.gradle'
  • 添加task,与android{}同级
    注意:generatePomFileForCqtestPublication,其中Cqtest即为渠道名,必须和mavenpublish.gradle文件中定义的名称完全一致。
android{
//~~~~~~
}
//编译发布到maven
task deploy{
    dependsOn tasks.clean
    dependsOn tasks.build
    dependsOn tasks.publish
    tasks.preBuild.mustRunAfter(tasks.clean)
    //
    tasks.generatePomFileForCqtestPublication.mustRunAfter(tasks.build)
    tasks.generatePomFileForDevPublication.mustRunAfter(tasks.build)
    tasks.generatePomFileForPrePublication.mustRunAfter(tasks.build)
}
  1. 打包各个渠道的aar包
    可以通过gradle打包,如图所示:
    使用Maven-Publish发布多渠道aar包_第3张图片
    打包后的文件存放路径为:module—build—outputs—aar,如图所示:
    使用Maven-Publish发布多渠道aar包_第4张图片

  2. 发布各个渠道的aar包到maven仓库
    也是通过gradle来发布,如图所示:
    使用Maven-Publish发布多渠道aar包_第5张图片

  3. 引用远程依赖

  • 发布成功之后,在需要依赖该aar包的项目build.gradle中添加maven仓库地址即在mavenpublish.gradle文件中配置的maven仓库地址。
  • 使用Maven-Publish发布多渠道aar包_第6张图片
allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }
		//添加aar包的maven仓库地址
        maven {
            url = "file://d:/maventestrepository"
        }
    }
}
  • 在需要依赖该aar包的module的build.gradle中添加依赖
implementation 'cn.companyname:lib-mycar-cqtest:1.0.6'

你可能感兴趣的:(使用Maven-Publish发布多渠道aar包)