buildscript {
    repositories {
        jcenter()
    }
}

plugins {
    id 'net.researchgate.release' version '2.4.0'
}
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'idea'
apply plugin: 'jacoco'
apply plugin: 'maven'
apply plugin: 'signing'

//Only target Java 8
if (!JavaVersion.current().java8Compatible) {
    throw new IllegalStateException("Must be built with Java 8 or higher")
}

//IntelliJ settings
idea {
    project {
        //use JDK 1.8 in idea
        jdkName "1.8"
        languageLevel "1.8"
    }
}

//Dependencies
repositories {
    jcenter()
    mavenLocal()
}

dependencies {
    implementation "io.zipkin.brave:brave-bom:4.18.2"

    implementation "io.ratpack:ratpack-guice:${ratpackVersion}"
    implementation 'org.slf4j:slf4j-api:1.7.13'
    implementation "io.zipkin.brave:brave"
    implementation "io.zipkin.brave:brave-instrumentation-http"
    // compile vs compileOnly for access to all overloaded parameters on Config.spanReporter
    compileOnly 'io.zipkin.reporter:zipkin-reporter:1.1.2'
    testImplementation 'io.zipkin.reporter:zipkin-reporter'
    testImplementation 'junit:junit:4.12'
    testImplementation 'org.assertj:assertj-core:3.4.1'
    testImplementation 'org.codehaus.groovy:groovy-all:2.4.6' // required for coverage
    testImplementation 'org.spockframework:spock-core:1.0-groovy-2.4'
    testImplementation 'cglib:cglib-nodep:3.2.1'
    testImplementation 'org.objenesis:objenesis:2.2'
    testImplementation 'com.nagternal:spock-genesis:0.4.0'
    testImplementation 'org.slf4j:slf4j-nop:1.7.21'
    testImplementation 'io.ratpack:ratpack-groovy-test:1.4.1'
    testImplementation "io.zipkin.brave:brave-instrumentation-http-tests"
    testImplementation 'com.squareup.okhttp3:mockwebserver:3.9.1'
}

//Artifacts
task sourcesJar(type: Jar, dependsOn: classes) {
    classifier 'sources'
    from sourceSets.main.allSource
}

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

compileJava {
    options.deprecation = true
}

artifacts {
    archives sourcesJar
    archives javadocJar
}

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

//Artifact deployment

/**
 * Set the credentials from ENV variables.
 */
def repoUserName = System.getenv("SONATYPE_USER")
def repoPassword = System.getenv("SONATYPE_PASSWORD")

uploadArchives {
    repositories {
        mavenDeployer {
            beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
            repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2") {
                authentication(userName: "${repoUserName}", password: "${repoPassword}")
            }
            snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots") {
                authentication(userName: "${repoUserName}", password: "${repoPassword}")
            }
            //configuration = configurations.deployerJars
            pom.project {
                name 'ratpack-zipkin'
                packaging 'jar'
                description 'Zipkin support for Ratpack'
                url 'https://github.com/hyleung/ratpack-zipkin'
                scm {
                    url "https://github.com/hyleung/ratpack-zipkin.git";
                    connection "scm:git:git://github.com/hyleung/ratpack-zipkin.git";
                    developerConnection "scm:git:git@github.com:hyleung/ratpack-zipkin.git";
                }
                developers {
                    developer {
                        id "hyleung";
                        name "Ho Yan Leung";
                        email "hy.leung@gmail.com";
                    }
                }
                licenses {
                    license {
                        name 'The Apache Software License, Version 2.0'
                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                        distribution 'repo'
                    }
                }
            }
            pom.groupId = 'com.github.hyleung'
            pom.artifactId = 'ratpack-zipkin'
        }
    }
}

install {
    repositories.mavenInstaller {
        pom.groupId = 'com.github.hyleung'
        pom.artifactId = 'ratpack-zipkin'
    }
}

//Coverage
jacocoTestReport {
    reports {
        xml.enabled = true
        html.enabled = true
    }
}
