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

plugins {
    id "org.sonarqube" version "2.8"
    id "jacoco"
}

apply plugin: 'idea'
apply plugin: "jacoco"
apply plugin: "java"
apply plugin: "org.sonarqube"

ext {
    majorVersion = '1'
    minorVersion = '0'
    patchVersion = '0'
    baseVersion = "${majorVersion}.${minorVersion}.${patchVersion}"
}

// import all scripts from ./gradle.d in order of sequence
file('gradle.d').listFiles().sort().each {
    if (it =~ /.*\.gradle$/) {
        apply from: relativePath(it)
    }
}


subprojects {
    apply plugin: "idea"
    apply plugin: "java"
    apply plugin: "org.sonarqube"

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

        dependencies {
            classpath "org.springframework.boot:spring-boot-gradle-plugin:${version_spring_boot}"
            classpath "org.asciidoctor:asciidoctor-gradle-plugin:1.5.3"
            classpath "org.ajoberstar:gradle-git-publish:2.1.2"
            classpath "com.diffplug.spotless:spotless-plugin-gradle:3.26.0"
            classpath "com.github.jacobono:gradle-jaxb-plugin:1.3.6"
            classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4"
        }
    }

    version = project.getBuildVersion(baseVersion)

    sourceCompatibility = 11
    targetCompatibility = 11

    repositories {
        jcenter()
        mavenCentral()
        mavenLocal()
    }

    test {
        useJUnitPlatform()
        testLogging {
            events "passed", "skipped", "failed"
        }
        finalizedBy jacocoTestReport
    }

    jacocoTestReport {
        dependsOn test
    }

    jacocoTestReport {
        reports {
            xml.enabled true
            csv.enabled false
            html.enabled true
        }
    }

    dependencies {
        testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: version_junit
        testCompile group: 'org.mockito', name: 'mockito-junit-jupiter', version: version_mockito_junit
        testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: version_junit
        annotationProcessor("org.projectlombok:lombok:$lombok_version")
        compileOnly("org.projectlombok:lombok:$lombok_version")
        testCompile group: 'org.jacoco', name: 'org.jacoco.ant', version: '0.8.5'
    }


}

idea {
    project {
        vcs = 'Git'
    }
}

sonarqube {
    properties {
        property "sonar.projectKey", "adessoAG_coderadar"
        property "sonar.organization", "adesso-ag"
        property "sonar.host.url", "https://sonarcloud.io"
        property "sonar.sources", "src/main/java, coderadar-ui/src"
        property "sonar.tests", "src/test/java"
        property "sonar.exclusions", "coderadar-app/workdir"
        property 'sonar.core.codeCoveragePlugin', 'jacoco'
        property "sonar.coverage.jacoco.xmlReportPaths", "${project.buildDir}/reports/jacoco/codeCoverageReport/codeCoverageReport.xml"
    }
}

allprojects {
    repositories {
        mavenLocal()
        mavenCentral()
        jcenter()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        testCompile group: 'org.jacoco', name: 'org.jacoco.ant', version: '0.8.5'
    }
}

tasks.register("codeCoverageReport", JacocoReport) {
    // If a subproject applies the 'jacoco' plugin, add the result it to the report
    subprojects { subproject ->
        subproject.plugins.withType(JacocoPlugin).configureEach {
            subproject.tasks.matching({ t -> t.extensions.findByType(JacocoTaskExtension) }).configureEach { testTask ->
                sourceSets subproject.sourceSets.main
                executionData(testTask)
            }

            // To automatically run `test` every time `./gradlew codeCoverageReport` is called,
            // you may want to set up a task dependency between them as shown below.
            // Note that this requires the `test` tasks to be resolved eagerly (see `forEach`) which
            // may have a negative effect on the configuration time of your build.
            subproject.tasks.matching({ t -> t.extensions.findByType(JacocoTaskExtension) }).forEach {
                rootProject.tasks.codeCoverageReport.dependsOn(it)
            }
        }
    }

    // enable the different report types (html, xml, csv)
    reports {
        // xml is usually used to integrate code coverage with
        // other tools like SonarQube, Coveralls or Codecov
        xml.enabled true

        // HTML reports can be used to see code coverage
        // without any external tools
        html.enabled true
    }
}


