// ./gradlew clean build generateRelease
apply plugin: 'maven'

def groupId = project.PUBLISH_GROUP_ID
def artifactId = project.PUBLISH_ARTIFACT_ID
def version = project.PUBLISH_VERSION

def localReleaseDest = "${buildDir}/release/${version}"

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

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

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

uploadArchives {
    repositories.mavenDeployer {
        pom.groupId = groupId
        pom.artifactId = artifactId
        pom.version = version
        // Add other pom properties here if you want (developer details / licenses)
        repository(url: "file://${localReleaseDest}")
        // workaround for: https://code.google.com/p/android/issues/detail?id=72807
        pom.whenConfigured { pom ->
            pom.dependencies.findAll { dependency ->
                if (dependency.artifactId == 'appcompat-v7') {
                    dependency.type = 'aar'
                }
            }
        }
    }
}

task zipRelease(type: Zip) {
    from localReleaseDest
    destinationDir buildDir
    archiveName "release-${version}.zip"
}

task generateRelease {
    leftShift {
        println "Release ${version} can be found at ${localReleaseDest}/"
        println "Release ${version} zipped can be found ${buildDir}/release-${version}.zip"
    }
}

generateRelease.dependsOn(uploadArchives)
generateRelease.dependsOn(zipRelease)


artifacts {
    archives androidSourcesJar
    //archives androidJavadocs doesn't work for some reason
}
