apply plugin: "maven"
apply plugin: "signing"

def isReleaseBuild() {
    return VERSION_NAME.contains("SNAPSHOT") == false
}

def getReleaseRepositoryUrl() {
  return hasProperty("RELEASE_REPOSITORY_URL") ? RELEASE_REPOSITORY_URL
      : "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
}

def getSnapshotRepositoryUrl() {
  return hasProperty("SNAPSHOT_REPOSITORY_URL") ? SNAPSHOT_REPOSITORY_URL
      : "https://oss.sonatype.org/content/repositories/snapshots/"
}

def getRepositoryUsername() {
    return hasProperty("SONATYPE_USERNAME") ? SONATYPE_USERNAME : ""
}

def getRepositoryPassword() {
    return hasProperty("SONATYPE_PASSWORD") ? SONATYPE_PASSWORD : ""
}

afterEvaluate { project ->
    uploadArchives {
        repositories {
            mavenDeployer {
                beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }

                pom.groupId = GROUP
                pom.artifactId = POM_ARTIFACT_ID
                pom.version = VERSION_NAME

                repository(url: getReleaseRepositoryUrl()) {
                    authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
                }
                snapshotRepository(url: getSnapshotRepositoryUrl()) {
                    authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
                }

                pom.project {
                    name POM_NAME
                    packaging POM_PACKAGING
                    description POM_DESCRIPTION
                    url POM_URL

                    scm {
                        url POM_SCM_URL
                        connection POM_SCM_CONNECTION
                        developerConnection POM_SCM_DEV_CONNECTION
                    }

                    licenses {
                        license {
                            name POM_LICENCE_NAME
                            url POM_LICENCE_URL
                            distribution POM_LICENCE_DIST
                        }
                    }

                    developers {
                        developer {
                            id POM_DEVELOPER_ID
                            name POM_DEVELOPER_NAME
                        }
                    }
                }

            }
        }
    }

    signing {
        required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") }
        sign configurations.archives
    }

    task androidJavadocs(type: Javadoc) {
        source = android.sourceSets.main.java.srcDirs
        ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
        classpath += files(ext.androidJar)
    }

    task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
        classifier = "javadoc"
        from androidJavadocs.destinationDir
    }

    task androidSourcesJar(type: Jar) {
        classifier = "sources"
        from android.sourceSets.main.java.sourceFiles
    }

    if (JavaVersion.current().isJava8Compatible()) {
        allprojects {
            tasks.withType(Javadoc) {
                options.addStringOption("Xdoclint:none", "-quiet")
            }
        }
    }

    artifacts {
        archives androidSourcesJar
        archives androidJavadocsJar
    }

    task installArchives(type: Upload) {
        group = "Upload"
        description = "Installs the ':archives' artifacts into the local Maven repository."

        configuration = configurations.archives

        repositories {
            mavenInstaller {
                pom.groupId = GROUP
                pom.artifactId = POM_ARTIFACT_ID
                pom.version = VERSION_NAME
            }
        }
    }
}
