Gradle 通过 Maven 上传多变体 AAR

Maven的每个工程默认只会处理一个产物,这个从POM文件的结构中也可以看出来。但是我们很多情况下一个工程中是有多个产物的,这种情况就需要去生成多个POM文件,然后显示的声明要上传的每个产物

uploadArchives {
    repositories {
        mavenDeployer {
            //关联一个Maven库
            repository(url: "file://localhost/tmp/myRepo/")
            addFilter('api') {artifact, file ->
                artifact.name == 'api'
            }
            addFilter('service') {artifact, file ->
                artifact.name == 'service'
            }
            pom('api').version = 'mySpecialMavenVersion'
        }
    }
}

if there is only one artifact, usually there is not more to do. If there is more than one artifact you have to decide what to do about this, as a Maven POM can only deal with one artifact. There are two strategies. If you want to deploy only one artifact you have to specify a filter to select this artifact. If you want to deploy more than one artifact, you have to specify filters which select each artifact. Associated with each filter is a separate configurable POM.

POM文件MavenPom

Pom文件的关键属性和默认值

Pom文件最终在/poms中上生成

Android中使用

MavenDeployer 默认只上传Release产物,如果增加了变体则需要在artifacts中声明需要上传的产物,因为有多个产物,需要通过addFiller配置POM去给对应变体显示声明需要上传的产物,比如debug变体只上传debug的产物

apply plugin: 'maven' 

task sourcesJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier = 'sources'
}

afterEvaluate { project ->
    uploadArchives {
        repositories {
            mavenDeployer {

               ...
                //关键代码
                android.libraryVariants.all { variant ->
                    def isFlavor = !variant.flavorName.isEmpty()
                    def variantName = "${variant.name}"

                    artifacts {
                        if ("debug"==variantName) {
                            archives file: file("build/outputs/aar/${project.name}-${variantName}.aar")
                        }
                        archives sourcesJar
                    }

                    addFilter(variantName) { artifact, file ->
                        println file.name + "=$variantName"
                        //选取产物
                        file.name.contains(variantName)
                    }
                    pom(variantName).groupId = GROUP
                    pom(variantName).artifactId = POM_ARTIFACT_ID + "-" + variantName
                    pom(variantName).version = getVersionName()
                    pom(variantName).project {
                        name POM_NAME
                        packaging POM_PACKAGING
                        description POM_DESCRIPTION
                        url POM_URL              
                    }

                    ...

                    }

                }

            }
        }
    }
}

你可能感兴趣的:(Gradle 通过 Maven 上传多变体 AAR)