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

group = "org.hamcrest"
version = "2.1"

subprojects {
    apply plugin: 'java-library'

    sourceCompatibility = JavaVersion.VERSION_1_7
    targetCompatibility = JavaVersion.VERSION_1_7

    group = rootProject.group
    version = rootProject.version

    repositories {
        mavenCentral()
    }

    test {
        testLogging {
            exceptionFormat = 'full'
        }
    }

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

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

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

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

        licenses {
            license {
                name = 'BSD Licence 3'
                url = 'http://opensource.org/licenses/BSD-3-Clause'
            }
        }

        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
}
