脚本自动批量上传 .aar 到 maven repo

260 阅读1分钟
plugins {
    id 'com.android.library'
    id 'org.jetbrains.kotlin.android'
    id 'maven-publish'
}

// Load variables from local.properties file
Properties localProperties = new Properties()
localProperties.load(project.rootProject.file('local.properties').newDataInputStream())


android {
    namespace 'com.***'
    compileSdk 33

    defaultConfig {
        minSdk 26
        targetSdk 33

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

ext.aarFiles = []

afterEvaluate {
    publishing {
        publications {
            File libsDir = new File(project.projectDir, "libs")
            int size = libsDir.listFiles().length
            if (size != 12) {// because mac has an external file: .DS_Store
                throw new GradleException("The retrieved .aar files are incomplete,current size is : " + size)
            } else {
                println("libs directory size check passed, the size is: " + size)
            }

            libsDir.eachFile { file ->
                if (file.name.endsWith(".aar")) {
                    String baseName = file.name.substring(0, file.name.lastIndexOf("."))
                    def matcher = (baseName =~ /(.+)-(\d+.\d+.\d+(?:.\d+)?(?:-\w+(?:.\d+)?)?)/)

                    if (matcher.matches()) {
                        String name = matcher[0][1]
                        String version = matcher[0][2]

                        aarFiles.add([name: name, version: version, file: file])
                    }
                }
            }

            aarFiles.each { aar ->
                "${aar.name}"(MavenPublication) {
                    groupId = 'com.***'
                    artifactId = aar.name
                    version = aar.version
                    artifact(aar.file)
                }
            }
        }

        repositories {
            def auth = localProperties.getProperty("maven.auth.publish")
            maven {
                name = "***"
                url = "https://www.myget.org/***"
            }
        }
    }
}