plugins {
    id 'java-library'
    id 'signing'
    id 'maven-publish'
    id 'io.freefair.lombok' version "8.6"
    id 'jacoco'
    id 'io.github.gradle-nexus.publish-plugin' version "1.1.0"
}

group 'it.firegloves'
version '1.10.1'


java {
    withJavadocJar()
    withSourcesJar()
    sourceCompatibility = JavaVersion.VERSION_1_8
}

def poiVersion = '5.2.5'
def comonsLang3Version = '3.14.0'
def comonsMath3Version = '3.6.1'
def comonsCollectionsVersion = '4.4'
def slf4jVersion = '2.0.13'

repositories {
    mavenCentral()
}

dependencies {

    // https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml
    implementation (group: 'org.apache.poi', name: 'poi-ooxml', version: poiVersion) {
        exclude group: 'org.apache.commons', module: 'commons-compress'
    }
    // TODO remove when poi-ooxml will update its dependency
    implementation group: 'org.apache.commons', name: 'commons-compress', version: '1.26.2'

    // https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
    implementation group: 'org.apache.commons', name: 'commons-lang3', version: comonsLang3Version

    // https://mvnrepository.com/artifact/org.apache.commons/commons-math3
    implementation group: 'org.apache.commons', name: 'commons-math3', version: comonsMath3Version

    // https://mvnrepository.com/artifact/org.apache.commons/commons-collections4
    implementation group: 'org.apache.commons', name: 'commons-collections4', version: comonsCollectionsVersion

    // https://mvnrepository.com/artifact/org.slf4j/slf4j-api
    implementation group: 'org.slf4j', name: 'slf4j-api', version: slf4jVersion


    ////////// TEST DEPENDENCIES

    // junit
    testImplementation group: 'junit', name: 'junit', version: '4.13.2'

    // https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client
    testImplementation group: 'org.mariadb.jdbc', name: 'mariadb-java-client', version: '3.4.0'

    // https://mvnrepository.com/artifact/org.mockito/mockito-core
    testImplementation group: 'org.mockito', name: 'mockito-core', version: '4.11.0'

    // https://mvnrepository.com/artifact/org.hamcrest/hamcrest-all
    testImplementation group: 'org.hamcrest', name: 'hamcrest-all', version: '1.3'

    // https://mvnrepository.com/artifact/mysql/mysql-connector-java
    testImplementation group: 'com.mysql', name: 'mysql-connector-j', version: '8.4.0'
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            pom {
                name = 'MemPOI'
                description = 'A library to simplify export from database to Excel files using Apache POI'
                url = 'https://github.com/firegloves/MemPOI'
                inceptionYear = '2019'

                scm {
                    url = 'https://github.com/firegloves/MemPOI.git'
                    connection = 'scm:git:git://github.com/firegloves/MemPOI.git'
                    developerConnection = 'cm:git:ssh://github.com/firegloves/MemPOI.git'
                }

                licenses {
                    license {
                        name = 'MIT License'
                        url = 'http://www.opensource.org/licenses/mit-license.php'
                    }
                }

                developers {
                    developer {
                        id = 'firegloves'
                        name = 'Luca Corsetti'
                        email = 'lucorset@gmail.com'
                    }
                }
            }
            from(components.java)
        }
    }
}

nexusPublishing {
    repositories {
        sonatype {
            username = System.getenv("OSSRH_USERNAME")
            password = System.getenv("OSSRH_TOKEN")
        }
    }
}

signing {
    sign publishing.publications.mavenJava
}


javadoc {
    if(JavaVersion.current().isJava9Compatible()) {
        options.addBooleanOption('html5', true)
    }
}


// JACOCO START
test {
    finalizedBy jacocoTestReport // report is always generated after tests run
}

jacoco {
    toolVersion = "0.8.11"
    reportsDirectory = layout.buildDirectory.dir("reports/jacoco")
}

jacocoTestReport {
    dependsOn test // tests are required to run before generating the report
    reports {
        xml.required.set(true)
        csv.required.set(false)
        html.required.set(false)
    }
}

jacocoTestCoverageVerification {
    violationRules {
        rule {
            limit {
                includes = [ 'it.firegloves.mempoi.*' ]
                minimum = 0.7
            }
        }
    }

    dependsOn jacocoTestReport
}

check.dependsOn jacocoTestCoverageVerification
// JACOCO END
