apply plugin: 'maven'
apply plugin: 'signing'

def isLocalBuild() {
    return getProperty('VERSION_NAME').toString().contains("LOCAL")
}

def isReleaseBuild() {
    return getProperty('VERSION_NAME').toString().contains("SNAPSHOT")
}

def getLocalRepositoryUrl() {
    return getProperty('LOCAL_REPOSITORY_URL')
}

def getSnapshotRepositoryUrl() {
    return getProperty('SNAPSHOT_REPOSITORY_URL')
}

def getDebugRepositoryUrl() {
    return isLocalBuild() ? getLocalRepositoryUrl() : getSnapshotRepositoryUrl()
}

def getReleaseRepositoryUrl() {
    return getProperty('RELEASE_REPOSITORY_URL')
}

def getRepositoryUsername() {
    return hasProperty('MAVEN_USERNAME') ? getProperty('MAVEN_USERNAME') : ""
}

def getRepositoryPassword() {
    return hasProperty('MAVEN_PASSWORD') ? getProperty('MAVEN_PASSWORD') : ""
}

uploadArchives {
    repositories {
        mavenDeployer {
            configurePOM(pom)

            beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }

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

//compile **.jar/**-javadoc.jar/**-sources.jar
task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from 'build/docs/javadoc'
}

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

artifacts {
    archives jar
    archives javadocJar
    archives sourcesJar
}

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

private configurePOM(def pom) {
    pom.project {
        groupId getProperty('POM_GROUP_ID')
        artifactId getProperty('POM_ARTIFACT_ID')
        version getProperty('VERSION_NAME')
        name getProperty('POM_NAME')
        packaging getProperty('POM_PACKAGING')
        description getProperty('POM_DESCRIPTION')
        url getProperty('POM_URL')
        inceptionYear getProperty('POM_INCEPTION_YEAR')

        scm {
            url getProperty('POM_SCM_URL')
            connection getProperty('POM_SCM_CONNECTION')
            developerConnection getProperty('POM_SCM_DEV_CONNECTION')
        }

        licenses {
            license {
                name getProperty('POM_LICENCE_NAME')
                url getProperty('POM_LICENCE_URL')
                distribution getProperty('POM_LICENCE_DIST')
                comments getProperty('POM_LICENCE_COMMENTS')
            }
        }

        developers {
            developer {
                id getProperty('POM_DEVELOPER_ID')
                name getProperty('POM_DEVELOPER_NAME')
                email getProperty('POM_DEVELOPER_EMAIL')
                url getProperty('POM_DEVELOPER_URL')
            }
        }

        issueManagement {
            system getProperty('POM_ISSUE_MANAGEMENT_SYSTEM')
            url getProperty('POM_ISSUE_MANAGEMENT_URL')
        }
    }
}
