
/*
 * Based on Stripe's java deployment, which is in turn
 * based on the example at Chris Banes's repository that allows signing
 * without manually creating a maven file.
 *
 * The original can be found at
 * https://raw.github.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle
 */

apply plugin: "signing"
apply plugin: "maven-publish"
apply plugin: "io.github.gradle-nexus.publish-plugin"

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

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

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

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

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

nexusPublishing {
    packageGroup = GROUP
    repositories {
        sonatype {
            nexusUrl.set(uri(getReleaseRepositoryUrl()))
            snapshotRepositoryUrl.set(uri(getSnapshotRepositoryUrl()))
            
            username = getRepositoryUsername()
            password = getRepositoryPassword()
        }
    }
}

task sourcesJar(type: Jar) {
    classifier = "sources"
    from sourceSets.main.allSource
}

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

artifacts {
    archives sourcesJar
    archives javadocJar
}

publishing {
    publications {
        maven(MavenPublication) {
            groupId = GROUP
            version = VERSION_NAME

            from components.java
            artifact sourcesJar
            artifact javadocJar
            pom {
                name = POM_NAME
                description = POM_DESCRIPTION
                url = POM_URL

                groupId = GROUP
                artifactId = POM_ARTIFACT_ID
                version = VERSION_NAME

                licenses {
                    license {
                        name = POM_LICENCE_NAME
                        url = POM_LICENCE_URL
                        distribution = POM_LICENCE_DIST
                    }
                }
                developers {
                    developer {
                        id = POM_DEVELOPER_ID
                        name = POM_DEVELOPER_NAME
                        email = POM_DEVELOPER_EMAIL
                    }
                }
                scm {
                    connection = POM_SCM_CONNECTION
                    developerConnection = POM_SCM_DEV_CONNECTION
                    url = POM_SCM_URL
                }
                organization {
                    name = POM_DEVELOPER_NAME
                    url = POM_ORGANIZATION_URL
                }
            }
        }
    }
}

signing {
    required { !project.version.endsWith("-SNAPSHOT") && !project.hasProperty("skipSigning") }
    useGpgCmd()
    sign publishing.publications.maven
}
