apply plugin: 'maven'

def mavenRepositoryUrl = "http://nexus.yidianling.com/repository/AndroidReleases/"
def mavenRepositoryUrl2 = "http://nexus.yidianling.com/repository/AndroidSnapshots/"

def getVersionName() {
    def publishVersion = rootProject.ext.ydl2PublishVersion
    return publishVersion
}
def isLocal(){
    Properties properties = new Properties()
    InputStream inputStream = project.rootProject.file('local.properties').newDataInputStream()
    properties.load(inputStream)
    def local = properties.getProperty("packageLocal")
    return local
}
task sourceJar(type:Jar){
    classifier = 'source'
    from android.sourceSets.main.java.srcDirs
}
artifacts {
    archives sourceJar
}

afterEvaluate { project ->
    uploadArchives {
        repositories {
            mavenDeployer {
                pom.artifactId = project.getName().replace('_', "-")
                pom.groupId = "com.ydl"
                pom.version = getVersionName()
                def local = isLocal()
                if (local == "true") {
                    repository(url: mavenLocal().url)
                } else if (local == "false") {
                    def snapshot = getVersionName().endsWith("-SNAPSHOT")
                    repository(url: snapshot ? mavenRepositoryUrl2 : mavenRepositoryUrl) {
                        authentication(userName: "admin", password: "fjoi#1+#@")
                    }
                } else {
                    println("请在local.properties中指定packageLocal的值[true|false]")
                }
            }
        }
    }

    uploadArchives.doFirst {
        // log
        println String.format(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n>>>>> [%s] 正在将[%s]版本的[%s]包上传到Maven的[%s]仓库 >>>>>\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
                , project.getName()
                , getVersionName()
                , "aar"
                , "packageLocal:" + isLocal()
        )
    }
//    rootProject.getTasksByName("aa",false).forEach{println(it)}
    //pay依赖platform,platform依赖utils和net
    //confide依赖audioim
    if (project.name.startsWith("ydl")) {
        if (project.name == "ydl-utils" || project.name == "ydl-net") {
            rootProject.getTasksByName("packYdl1", false).forEach { it.dependsOn(uploadArchives) }
        }
        if (project.name == "ydl-platform") {
            rootProject.getTasksByName("packYdl2", false).forEach { it.dependsOn(uploadArchives) }
        }
        rootProject.getTasksByName("packYdl", false).forEach { it.dependsOn(uploadArchives) }
    } else {
        if (project.name != "m-audioim") {
            rootProject.getTasksByName("packM", false).forEach { it.dependsOn(uploadArchives) }
        }
    }
}