buildscript {

    // ------------------------------------------------------------------
    // Versions
    // ------------------------------------------------------------------
    
    ext.rxjava3Version = "3.0.0-RC6"
    ext.rxjava3DocVersion = "3.x"
    ext.licenseVersion = "0.15.0"
    ext.animalSnifferVersion = "1.5.0"
    ext.bndVersion = "4.3.1"
    ext.junitVersion = "4.12"
    ext.jmhLibVersion = "1.21"
    ext.jmhGradleVersion = "0.5.0"
    ext.jacocoVersion = "0.8.4"
    ext.pmdVersion = "5.4.2"
    ext.mavenPublishVersion = "0.8.0"
    
    // ------------------------------------------------------------------

    repositories {
        mavenCentral()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }

    dependencies {
        classpath "ru.vyarus:gradle-animalsniffer-plugin:$animalSnifferVersion"
        classpath "gradle.plugin.com.hierynomus.gradle.plugins:license-gradle-plugin:$licenseVersion"
        classpath "me.champeau.gradle:jmh-gradle-plugin:$jmhGradleVersion"
        classpath "biz.aQute.bnd:biz.aQute.bnd.gradle:$bndVersion"
        classpath "com.vanniktech:gradle-maven-publish-plugin:$mavenPublishVersion"
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: "me.champeau.gradle.jmh"
apply plugin: 'pmd'
apply plugin: 'jacoco'
apply plugin: 'ru.vyarus.animalsniffer'
apply plugin: "com.github.hierynomus.license"

sourceCompatibility = '1.8'
targetCompatibility = '1.8'

group = "com.github.akarnokd" 
ext.githubProjectName = 'rxjava3-jdk8-interop'

[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

if (!hasProperty('mainClass')) {
    ext.mainClass = ''
}

repositories {
    mavenCentral()
    maven { url 'https://oss.sonatype.org/content/groups/public' }

    // maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
    // maven { url 'https://oss.jfrog.org/libs-snapshot' }
    // maven { url 'http://repo.spring.io/libs-snapshot' }
}

apply plugin: 'maven'

dependencies {
    signature 'org.codehaus.mojo.signature:java18:1.0@signature'

    compile "io.reactivex.rxjava3:rxjava:$rxjava3Version"

    testCompile group: 'junit', name: 'junit', version: junitVersion
}

apply plugin: 'biz.aQute.bnd.builder'

jar {
    bnd ('Bundle-Name': 'rxjava3-jdk8-interop',
            'Bundle-Vendor': 'RxJava Contributors',
            'Bundle-Description': 'RxJava 3 interop library for supporting Java 8 features such as Optional, Stream and CompletableFuture.',
            'Import-Package': '!org.junit,!junit.framework,!org.mockito.*,!org.testng.*,*',
            'Bundle-DocURL': 'https://github.com/akarnokd/RxJavaJdk8Interop',
            'Eclipse-ExtensibleAPI': 'true',
    )
}

jmh {
    jmhVersion = jmhLibVersion
    humanOutputFile = null
    if (project.hasProperty('jmh')) {
        include = ".*" + project.jmh + ".*"
    } else {
        include = ".*"
    }
}

plugins.withType(EclipsePlugin) {
    project.eclipse.classpath.plusConfigurations += [ configurations.jmh ]
}

javadoc {
    failOnError = false

    options.links(
        "https://docs.oracle.com/javase/8/docs/api/",
        "http://reactivex.io/RxJava/$rxjava3DocVersion/javadoc/"
    )
    
}

test {
    maxHeapSize = "2g"
    testLogging  {
        events "started", "failed" // "skipped", "passed"
    //    showStandardStreams = true
    }
}

license {
    header rootProject.file('HEADER')
    ext.year = Calendar.getInstance().get(Calendar.YEAR)
    skipExistingHeaders true
    ignoreFailures true
    excludes(["**/*.md", "**/*.txt"])
}

jacoco {
    toolVersion = jacocoVersion // See http://www.eclemma.org/jacoco/.
}

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

build.dependsOn jacocoTestReport

check.dependsOn jacocoTestReport

pmd {
    toolVersion = pmdVersion
    ignoreFailures = true
    sourceSets = [sourceSets.main]
    ruleSets = []
    ruleSetFiles = files('pmd.xml')
}

pmdMain {
    reports {
        html.enabled = true
        xml.enabled = true
    }
}

task pmdPrint(dependsOn: 'pmdMain') doLast {
    File file = rootProject.file('build/reports/pmd/main.xml')
    if (file.exists()) {

        println("Listing first 100 PMD violations")

        file.eachLine { line, count ->
            if (count <= 100) {
               println(line)
            }
        }

    } else {
        println("PMD file not found.")
    }
}

build.dependsOn pmdPrint
check.dependsOn pmdPrint

animalsniffer {
    annotation = 'io.reactivex.internal.util.SuppressAnimalSniffer'
}

apply plugin: "com.vanniktech.maven.publish"
