美文网首页Android深入
Android Gradle 发布aar包到maven 分deb

Android Gradle 发布aar包到maven 分deb

作者: 想看烟花么 | 来源:发表于2022-08-17 13:40 被阅读0次

    publishing.gradle

    //[Publishing a project as module (gradle.org)](https://docs.gradle.org/current/userguide/publishing_setup.html)
    apply plugin: 'maven-publish'
    
    def libraryName = "moduleName"
    //task sourceJar(type: Jar) {
    //    from android.sourceSets.main.java.srcDirs
    //    classifier 'sources'
    //}
    
    task mappingZipTask(type: Zip) {
        archiveFileName = "mapping.zip"
        destinationDirectory = file("$buildDir/outputs/mapping/")
        from "$buildDir/outputs/mapping/release/"
    }
    
    afterEvaluate {
        publishAarDebugPublicationToLocalMavenRepository.dependsOn(":$libraryName:assembleDebug")
        publishAarDebugPublicationToLocalMavenRepository.mustRunAfter(":$libraryName:assembleDebug")
        mappingZipTask.dependsOn(":$libraryName:assembleRelease")
        mappingZipTask.mustRunAfter(":$libraryName:assembleRelease")
        publishAarReleasePublicationToLocalMavenRepository.dependsOn(mappingZipTask)
        publishAarReleasePublicationToLocalMavenRepository.mustRunAfter(mappingZipTask)
    }
    
    publishing {
        publications {
            aarDebug(MavenPublication) {
                startPublish(it, "debug")
            }
            aarRelease(MavenPublication) {
                startPublish(it, "release")
            }
        }
        repositories {
            maven {
                name "localMaven"
                url "file://$buildDir/outputs/xxxAndroidProject/Local"
            }
            maven {
                name "debugMaven"
                url "${getArtifactoryRepo()}"
                credentials {
                    username "${getCredentialsUserName()}"
                    password "${getCredentialsPassword()}"
                }
            }
            maven {
                name "releaseMaven"
                url "${getArtifactoryRepo()}"
                credentials {
                    username "${getCredentialsUserName()}"
                    password "${getCredentialsPassword()}"
                }
            }
        }
    }
    
    def startPublish(MavenPublication mavenPublication, String buildType) {
        def versionName = rootProject.ext.versionName
        def publishNumber = getBuildNumber()
        def buildName = buildAARName(versionName, buildType)
        mavenPublication.groupId 'com.company'
        mavenPublication.artifactId "xxxsdk"
        mavenPublication.version "$versionName-$publishNumber"
        //mavenPublication.artifact(sourceJar)
        println "publishingName=$buildName"
        mavenPublication.artifact("$buildDir/outputs/aar/$buildName")
     /*   def mappingDir = new File("$buildDir/outputs/mapping")
        if (buildType == "release") {
            //Only valid when actively calling publishing task.(means that it is not generating 'xxx-mapping.zip' in localMavenRepository when call assembleRelease task only.)
            mavenPublication.artifact("${mappingDir}/mapping.zip")
        }*/
        def subModules = [":conversationSDK:baseUI", ":conversationSDK:chatUIkit", ":conversationSDK:agent", ":conversationSDK:imagePicker",":conversationSDK:basicBuilt"]
        def set = new HashSet<>()
        mavenPublication.pom.withXml {
            def dependenciesNode = asNode().appendNode('dependencies')
            subModules.each {
                project(it).configurations.implementation.allDependencies.each {
                    if (it.group != null && (it.name != null && "unspecified" != it.name) && (it.version != null && "unspecified" != it.version)) {
                        String willAddDependency = "${it.group}${it.name}${it.version}"
                        if (!set.contains(willAddDependency)) {
                            set.add(willAddDependency)
                            def dependencyNode = dependenciesNode.appendNode('dependency')
                            dependencyNode.appendNode('groupId', it.group)
                            dependencyNode.appendNode('artifactId', it.name)
                            dependencyNode.appendNode('version', it.version)
                        }
                    }
                }
            }
        }
    }
    
    // build an aar name which will be in output folder
    // Format will be conversationSDK-v1.0.0-50-debug.aar
    def buildAARName(String versionName, String buildType) {
        def name = "conversationSDK"
        // add version name from defaultConfig
        name += "-v" + versionName
    
        // add version code from defaultConfig
        name += "-" + getBuildNumber()
    
        name += "-" + buildType + ".aar"
        return name
    }
    
    String getBuildNumber() {
        ext.env = System.getenv()
        def number = env.BUILD_NUMBER
        if (number == null) {
            // It's a developer build, the number can be int or String here
            number = rootProject.ext.defaultBuildNumber
        }
        return number
    }
    
    String getCredentialsUserName() {
        ext.env = System.getenv()
        def userName = env.REPO_USERNAME
        if (userName == null) {
            userName = "nullName"
        }
        return userName
    }
    
    String getCredentialsPassword() {
        ext.env = System.getenv()
        def password = env.REPO_PASSWORD
        if (password == null) {
            password = "nullPassword"
        }
        return password
    }
    
    String getArtifactoryRepo() {
        ext.env = System.getenv()
        def repo = env.REPO_PATH
        if (repo == null) {
            repo = "nullRepo"
        }
        return repo
    }
    
    /*publishing {
        publications {
            aar(MavenPublication) {
                def runTasks = gradle.startParameter.taskNames
                println "runTasks $runTasks"
                if (runTasks != null && runTasks.size > 0) {
                    for (i in 0..<runTasks.size) {
                        def task = runTasks[i]
                        if (task.contains("Debug")) {
                            println "runTasks into path1"
                            startPublish(it, "debug")
                            break
                        } else if (task.contains("Release")) {
                            println "runTasks into path2"
                            startPublish(it, "release")
                            break
                        }
                    }
                    println "runTasks into path3"
                }
            }
        }
    }*/
    
    -------------------感谢您的耐心阅读,感谢支持与点赞-------------------

    相关文章

      网友评论

        本文标题:Android Gradle 发布aar包到maven 分deb

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