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

ext.isReleaseVersion = !version.endsWith("SNAPSHOT")

task javadoc(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
    android.libraryVariants.all { variant ->
        if (variant.name == 'release') {
            owner.classpath += variant.javaCompileProvider.get().classpath
        }
    }
    exclude '**/R.html', '**/R.*.html', '**/index.html'
}

// build a jar with javadoc
task javadocJar(type: Jar, dependsOn: javadoc) {
    archiveClassifier.set('javadoc')
    from javadoc.destinationDir
}

// build a jar with source
task sourcesJar(type: Jar) {
    archiveClassifier.set('sources')
    from android.sourceSets.main.java.srcDirs
}

artifacts {
    archives javadocJar
    archives sourcesJar
}

afterEvaluate {
    publishing {
        publications {
            maven(MavenPublication) {
                from components.release
                artifact javadocJar
                artifact sourcesJar

                groupId = rootProject.group
                artifactId = project.name
                version = project.version

                apply from: rootProject.file('pom.gradle')
                pom(pomData)
            }
        }
        repositories {
            maven {
                name 'sonatype'
                url "https://oss.sonatype.org/service/local/staging/deploy/maven2"
                credentials {
                    username = findProperty("sonatype.username")
                    password = findProperty("sonatype.password")
                }
            }
        }
    }
    tasks.withType(Sign) {
        onlyIf { isReleaseVersion }
    }
    signing {
        useGpgCmd()
        sign publishing.publications.maven
    }
}
