plugins {
    id "signing"
    id "maven-publish"
}

group = "org.hamcrest"
version = "3.0"

subprojects {
    apply plugin: 'checkstyle'
    apply plugin: 'java-library'

    group = rootProject.group
    version = rootProject.version

    java {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    repositories {
        mavenCentral()
    }

    checkstyle {

        project.ext.checkstyleVersion = '9.3'
            //works with a JDK 7 version which is supposed to be supported although
            //deprecated, see https://github.com/hamcrest/JavaHamcrest/pull/211 for
            //the discussion about the support

        sourceSets = [ project.sourceSets.main, project.sourceSets.test ]
        ignoreFailures = false
        configFile = file("${project.rootDir}/checkstyle.xml")

        configurations {
            checkstyle
        }

        dependencies{
            assert project.hasProperty("checkstyleVersion")
            checkstyle "com.puppycrawl.tools:checkstyle:${checkstyleVersion}"
        }
    }

    test {
        testLogging {
            exceptionFormat = 'full'
        }
    }

    jar {
        metaInf {
            from rootProject.file('LICENSE')
        }
    }

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

    task javadocJar(type: Jar) {
        archiveClassifier = 'javadoc'
        from javadoc
    }
}

def pomConfigurationFor(String pomName, String pomDescription) {
    return {
        name = pomName
        description = pomDescription
        url = 'http://hamcrest.org/JavaHamcrest/'

        scm {
            connection = 'scm:git:https://github.com/hamcrest/JavaHamcrest.git'
            developerConnection = 'scm:git:ssh://github.com/hamcrest/JavaHamcrest.git'
            url = 'https://github.com/hamcrest/JavaHamcrest'
        }

        licenses {
            license {
                name = 'BSD-3-Clause'
                url = 'https://raw.githubusercontent.com/hamcrest/JavaHamcrest/master/LICENSE'
            }
        }

        developers {
            developer {
                id = 'joewalnes'
                name = 'Joe Walnes'
            }
            developer {
                id = 'npryce'
                name = 'Nat Pryce'
            }
            developer {
                id = 'sf105'
                name = 'Steve Freeman'
            }
        }
    }
}

def publishToOssrh = project.hasProperty('ossrhUsername') && project.hasProperty('ossrhPassword')

publishing {
    publications {
        def hamcrestProject = project(':hamcrest')
        hamcrest(MavenPublication) {
            from hamcrestProject.components.java
            artifactId hamcrestProject.name
            artifact hamcrestProject.sourcesJar
            artifact hamcrestProject.javadocJar
            pom pomConfigurationFor(
                    'Hamcrest',
                    'Core API and libraries of hamcrest matcher framework.')
        }

        def hamcrestCoreProject = project(':hamcrest-core')
        hamcrestCore(MavenPublication) {
            from hamcrestCoreProject.components.java
            artifactId hamcrestCoreProject.name
            artifact hamcrestCoreProject.sourcesJar
            artifact hamcrestCoreProject.javadocJar
            pom pomConfigurationFor(
                    'Hamcrest Core',
                    'Core Hamcrest API - deprecated, please use "hamcrest" instead')
        }

        def hamcrestLibraryProject = project(':hamcrest-library')
        hamcrestLibrary(MavenPublication) {
            from hamcrestLibraryProject.components.java
            artifactId hamcrestLibraryProject.name
            artifact hamcrestLibraryProject.sourcesJar
            artifact hamcrestLibraryProject.javadocJar
            pom pomConfigurationFor(
                    'Hamcrest Library',
                    'A library of Hamcrest matchers - deprecated, please use "hamcrest" instead')
        }
    }
    repositories {
        if (publishToOssrh) {
            maven {
                def snapshotRepoUrl = "https://oss.sonatype.org/content/repositories/snapshots/"
                def stagingRepoUrl  = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
                url = version.contains('SNAPSHOT') ? snapshotRepoUrl : stagingRepoUrl
                credentials {
                    username = ossrhUsername
                    password = ossrhPassword
                }
            }
        }
    }
}

signing {
    required { publishToOssrh }
    sign publishing.publications.hamcrest
    sign publishing.publications.hamcrestCore
    sign publishing.publications.hamcrestLibrary
}
