apply(plugin: "maven-publish")
apply(plugin: "signing")

tasks.register("sourcesJar", Jar) {
    archiveClassifier.set("sources")
    from(android.sourceSets.getByName("main").java.srcDirs)
}

tasks.register("javadoc", Javadoc) {
    source = android.sourceSets.getByName("main").java.srcDirs
    setDestinationDir(file("../javadoc/"))
    failOnError false
}

tasks.register("javadocJar", Jar) {
    dependsOn(javadoc)
    archiveClassifier.set("javadoc")
    from(javadoc)
}

afterEvaluate {
    tasks.javadoc.classpath += files(project.android.getBootClasspath())
}

publishing {
    publications {
        release(MavenPublication) {
            groupId PUBLISH_GROUP_ID
            artifactId PUBLISH_ARTIFACT_ID
            version PUBLISH_VERSION

            artifacts {
                artifact(sourcesJar)
                artifact(javadocJar)
                artifact("$buildDir/outputs/aar/${project.name}-release.aar")
            }

            pom {
                name = PUBLISH_ARTIFACT_ID
                description = POM_DESCRIPTION
                url = POM_URL

                licenses {
                    license {
                        name = POM_LICENSE_NAME
                        url = POM_LICENSE_URL
                    }
                }

                developers {
                    developer {
                        id = POM_DEVELOPER_ID
                        name = POM_DEVELOPER_NAME
                        email = POM_DEVELOPER_EMAIL
                    }
                }

                scm {
                    connection = GITHUB_SCM_CONNECTION
                    url = POM_SCM_URL
                }

                withXml {
                    def dependencies = asNode().appendNode("dependencies")

                    configurations
                            .getByName("releaseCompileClasspath")
                            .resolvedConfiguration
                            .firstLevelModuleDependencies
                            .forEach {
                        def dependency = dependencies.appendNode("dependency")
                        dependency.appendNode("groupId", it.moduleGroup)
                        dependency.appendNode("artifactId", it.moduleName)
                        dependency.appendNode("version", it.moduleVersion)
                    }
                }
            }
        }
    }
}

ext["signing.keyId"] = rootProject.ext["signing.keyId"]
ext["signing.password"] = rootProject.ext["signing.password"]
ext["signing.secretKeyRingFile"] = rootProject.ext["signing.secretKeyRingFile"]

signing {
    sign publishing.publications
}