
plugins {
    id "org.sonarqube" version "2.8"
    id "io.codearte.nexus-staging" version "0.21.1"
    id "com.github.spotbugs" version "2.0.1"
    id "org.xbib.gradle.plugin.asciidoctor" version "1.5.6.0.1"
}

subprojects {

    apply plugin: 'java'
    apply plugin: 'maven'
    apply plugin: 'pmd'
    apply plugin: 'checkstyle'
    apply plugin: 'com.github.spotbugs'
    apply plugin: 'org.xbib.gradle.plugin.asciidoctor'

    repositories {
        mavenCentral()
    }

    configurations {
        asciidoclet
    }

    dependencies {
        testCompile "org.junit.jupiter:junit-jupiter-api:${project.property('junit.version')}"
        testCompile "org.junit.jupiter:junit-jupiter-params:${project.property('junit.version')}"
        testCompile "org.junit.jupiter:junit-jupiter-engine:${project.property('junit.version')}"
        testCompile "org.junit.vintage:junit-vintage-engine:${project.property('junit.version')}"
        testCompile "junit:junit:${project.property('junit4.version')}"
        asciidoclet "org.asciidoctor:asciidoclet:${project.property('asciidoclet.version')}"
    }

    compileJava {
        sourceCompatibility = JavaVersion.VERSION_11
        targetCompatibility = JavaVersion.VERSION_11
    }

    tasks.withType(JavaCompile) {
        options.compilerArgs << "-Xlint:all"
    }

    jar {
        manifest {
            attributes('Implementation-Version': project.version)
        }
    }

    test {
        useJUnitPlatform()
        failFast = false
        testLogging {
            events 'STARTED', 'PASSED', 'FAILED', 'SKIPPED'
        }
        afterSuite { desc, result ->
            if (!desc.parent) {
                println "\nTest result: ${result.resultType}"
                println "Test summary: ${result.testCount} tests, " +
                        "${result.successfulTestCount} succeeded, " +
                        "${result.failedTestCount} failed, " +
                        "${result.skippedTestCount} skipped"
            }
        }
    }

    clean {
        delete 'out'
    }

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

    task javadocJar(type: Jar, dependsOn: javadoc) {
        classifier 'javadoc'
    }

    artifacts {
        archives sourcesJar, javadocJar
    }

    tasks.withType(Checkstyle) {
        ignoreFailures = true
        reports {
            xml.enabled = true
            html.enabled = true
        }
    }
    spotbugs {
        effort = "max"
        reportLevel = "low"
        //includeFilter = file("findbugs-exclude.xml")
    }

    tasks.withType(com.github.spotbugs.SpotBugsTask) {
        ignoreFailures = true
        reports {
            xml.enabled = false
            html.enabled = true
        }
    }

    tasks.withType(Pmd) {
        ignoreFailures = true
        reports {
            xml.enabled = true
            html.enabled = true
        }
    }

    checkstyle {
        configFile = rootProject.file('config/checkstyle/checkstyle.xml')
        ignoreFailures = true
        showViolations = true
    }

    sonarqube {
        properties {
            property "sonar.projectName", "${project.group} ${project.name}"
            property "sonar.sourceEncoding", "UTF-8"
            property "sonar.tests", "src/test/java"
            property "sonar.scm.provider", "git"
            property "sonar.junit.reportsPath", "build/test-results/test/"
        }
    }

    ext {
        projectDescription = 'Content processing library for Java'
        scmUrl = 'https://github.com/xbib/content'
        scmConnection = 'scm:git:git://github.com/xbib/content.git'
        scmDeveloperConnection = 'scm:git:git://github.com/xbib/content.git'
    }

    task sonatypeUpload(type: Upload, dependsOn: build) {
        group = 'publish'
        configuration = configurations.archives
        uploadDescriptor = true
        repositories {
            if (project.hasProperty('ossrhUsername')) {
                mavenDeployer {
                    beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
                    repository(url: uri(ossrhReleaseUrl)) {
                        authentication(userName: ossrhUsername, password: ossrhPassword)
                    }
                    snapshotRepository(url: uri(ossrhSnapshotUrl)) {
                        authentication(userName: ossrhUsername, password: ossrhPassword)
                    }
                    pom.project {
                        groupId project.group
                        artifactId project.name
                        version project.version
                        name project.name
                        description projectDescription
                        packaging 'jar'
                        inceptionYear '2016'
                        url scmUrl
                        organization {
                            name 'xbib'
                            url 'http://xbib.org'
                        }
                        developers {
                            developer {
                                id 'xbib'
                                name 'Jörg Prante'
                                email 'joergprante@gmail.com'
                                url 'https://github.com/jprante'
                            }
                        }
                        scm {
                            url scmUrl
                            connection scmConnection
                            developerConnection scmDeveloperConnection
                        }
                        licenses {
                            license {
                                name 'The Apache License, Version 2.0'
                                url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                            }
                        }
                    }
                }
            }
        }
    }
}

nexusStaging {
    packageGroup = "org.xbib"
    //stagingProfileId = "org.xbib"
}
