buildscript {
	ext.licenseVersion = "0.16.1"
	ext.jmhPluginVersion = "0.5.3"
	ext.jmhLibVersion = "1.21"
	ext.jacocoVersion = "0.8.5"
	ext.bndVersion = "6.4.0"
	ext.mavenPublishPluginVersion = "0.20.0"
	ext.rsTckVersion = "1.0.4"

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

    dependencies {
        classpath "me.champeau.gradle:jmh-gradle-plugin:$jmhPluginVersion"
        classpath "biz.aQute.bnd:biz.aQute.bnd.gradle:$bndVersion"
        classpath "com.vanniktech:gradle-maven-publish-plugin:$mavenPublishPluginVersion"
        classpath "gradle.plugin.com.hierynomus.gradle.plugins:license-gradle-plugin:$licenseVersion"
    }
}

apply plugin: 'java-library'
apply plugin: 'eclipse'
apply plugin: "me.champeau.gradle.jmh"
apply plugin: 'pmd'
apply plugin: 'jacoco'
apply plugin: "com.github.hierynomus.license"

sourceCompatibility = '1.9'
targetCompatibility = '1.9'

group = "com.github.akarnokd" 
ext.githubProjectName = 'reactive4javaflow'

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

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

}

repositories {
    mavenCentral()
    maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
    maven { url 'https://oss.sonatype.org/content/groups/public' }
    maven { url 'https://oss.jfrog.org/libs-snapshot' }
    maven { url 'https://repo.spring.io/libs-snapshot' }
}

dependencies {
    testImplementation group: 'junit', name: 'junit', version: '4.13.2'
    testImplementation group: 'org.testng', name: 'testng', version: '7.5'
    testImplementation "org.reactivestreams:reactive-streams-tck-flow:$rsTckVersion"
}

[compileJava, compileTestJava]*.options*.compilerArgs << "-parameters"

tasks.withType(JavaCompile) {
    options.compilerArgs << "-parameters";
}

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

jar {
    bnd ('Bundle-Name': 'reactive4javaflow',
            'Bundle-Vendor': 'akarnokd',
            'Bundle-Description': 'Reactive Programming library based on the Java 9 Flow API and a 4th generation ReactiveX-style architecture.',
            'Import-Package': '!org.junit,!junit.framework,!org.mockito.*,*',
            'Bundle-DocURL': 'https://github.com/akarnokd/Reactive4JavaFlow',
		)
}

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

jmh {
    jmhVersion = "$jmhLibVersion"
    humanOutputFile = null
    includeTests = false

    if (project.hasProperty('jmh')) {
        include = ".*" + project.jmh + ".*"
    }

}

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

javadoc {
    failOnError = false

    exclude "**/impl/**"
    exclude "**/test/**"
    exclude "**/perf/**"
    options {
        windowTitle = "Reactive4JavaFlow Javadoc ${project.version}"
    }
    // Clear the following options to make the docs consistent with the old format
    options.addStringOption('top').value = ''
    options.addStringOption('doctitle').value = ''
    options.addStringOption('header').value = ''
    options.links("https://docs.oracle.com/javase/9/docs/api")
}

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

task testng(type: Test) {
    useTestNG()
    testLogging  {
        events=["skipped", "failed"]
        exceptionFormat="full"

        debug.events = ["skipped", "failed"]
        debug.exceptionFormat="full"

        info.events = ["failed", "skipped"]
        info.exceptionFormat="full"

        warn.events = ["failed", "skipped"]
        warn.exceptionFormat="full"
    }
}

check.dependsOn testng

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 = '6.46.0'
    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


