apply plugin: 'maven-publish'
apply plugin: 'signing'


Properties properties = new Properties()
File projectPropertiesFile = project.rootProject.file("project.properties");
if (projectPropertiesFile.exists()) {
    properties.load(projectPropertiesFile.newDataInputStream())
}

// read properties
def projectName = properties.getProperty("project.name")
def projectGroupId = properties.getProperty("project.groupId")
def projectArtifactId = properties.getProperty("project.artifactId")
def projectVersionName = properties.getProperty("project.version")

def javadocName = properties.getProperty("javadoc.name")

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

task javadoc(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
    failOnError false
}

task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}

// javadoc configuration
javadoc {
    options {
        encoding "UTF-8"
        charSet 'UTF-8'
        author true
        version projectVersionName
        links "http://docs.oracle.com/javase/7/docs/api"
        title javadocName + " " + projectVersionName + " API"
    }
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            groupId projectGroupId
            artifactId projectArtifactId
            version projectVersionName

            artifact("$buildDir/outputs/aar/oss-android-sdk-release.aar")
            artifact(sourcesJar)
            artifact(javadocJar)

            pom {
                name = projectName
                description = 'Aliyun Open Services SDK for Android\n' +
                        '      Copyright (C) Alibaba Cloud Computing\n' +
                        '      All rights reserved.\n' +
                        '      版权所有 （C）阿里云计算有限公司\n' +
                        '      http://www.aliyun.com'
                url = "https://github.com/aliyun/aliyun-oss-android-sdk"
                licenses {
                    license {
                        name = "The Apache License, Version 2.0"
                        url = "http://www.apache.org/licenses/LICENSE-2.0.txt"
                    }
                }
                developers {
                    developer {
                        id = "aliyunproducts"
                        name = "Aliyun SDK"
                        email = "aliyunsdk@aliyun.com"
                    }
                }
                scm {
                    connection = "scm:git:git://github.com/aliyun/aliyun-oss-android-sdk.git"
                    developerConnection = "scm:git:ssh://github.com/aliyun/aliyun-oss-android-sdk.git"
                    url = "https://github.com/aliyun/aliyun-oss-android-sdk"
                }


                withXml {
                    def dependenciesNode = asNode().appendNode('dependencies')
                    project.configurations.all { configuration ->
                        def name = configuration.name
                        if (name != "implementation" && name != "compile" && name != "api") {
                            return
                        }
                        println(configuration)
                        configuration.dependencies.each {
                            println(it)
                            if (it.name == "unspecified" || it.version == 'unspecified') {
                                // 忽略无法识别的
                                return
                            }
                            def dependencyNode = dependenciesNode.appendNode('dependency')
                            dependencyNode.appendNode('groupId', it.group)
                            dependencyNode.appendNode('artifactId', it.name)
                            dependencyNode.appendNode('version', it.version)
                            if (name == "api" || name == "compile") {
                                dependencyNode.appendNode("scope", "compile")
                            } else { // implementation
                                dependencyNode.appendNode("scope", "runtime")
                            }
                        }
                    }
                }

            }
        }
    }
}

//签名
signing {
    sign publishing.publications.mavenJava
}
