plugins {
    id "java"
    id "jacoco"
    id "maven-publish"
    id "signing"
    id "com.github.spotbugs" version "4.7.0"
    id "org.sonarqube" version "2.7.1"
}

group = "io.github.ari4java"
version = "0.17.0"

def projectUrl = "http://github.com/ari4java/ari4java"
def build_number = "x"
if (System.getenv("BUILD_NUMBER") != null) {
    build_number = System.getenv("BUILD_NUMBER")
}

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

sourceSets {
    main {
        java {
            srcDir "src/main/generated"
        }
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'com.fasterxml.jackson.core:jackson-core:2.13.1'
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.1'
    implementation 'com.fasterxml.jackson.core:jackson-annotations:2.13.1'
    implementation 'io.netty:netty-all:4.1.72.Final'
    implementation 'javax.xml.bind:jaxb-api:2.3.1'
    implementation 'org.slf4j:slf4j-api:1.7.32'

    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
    testImplementation 'org.mockito:mockito-core:4.2.0'
    testImplementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.17.0'
}

task buildProps(type: WriteProperties) {
    outputFile file("src/main/resources/build.properties")
    property "BUILD_NUMBER", build_number
}

jar {
    manifest {
        attributes "Implementation-Title": project.name,
                   "Implementation-Version": project.version
    }
}

javadoc {
    def inc = [
        "ch/loway/oss/ari4java/generated/AriVersion.java",
        "ch/loway/oss/ari4java/generated/actions/*",
        "ch/loway/oss/ari4java/generated/models/*",
        "ch/loway/oss/ari4java/*",
        "ch/loway/oss/ari4java/tools/*",
        "ch/loway/oss/ari4java/tools/*/*"
    ]
    includes = inc.toSet()
    options.overview = "src/overview.html"
    failOnError = false
}

test {
    useJUnitPlatform()
    jacoco {
        excludes = ["**/generated/**"]
    }
}

jacocoTestReport {
    reports {
        xml.enabled true
        csv.enabled false
        html.enabled true
        xml.destination = file("${buildDir}/reports/jacocoTestReport.xml")
        html.destination = file("${buildDir}/reports/jacocoTestReport/html")
    }
    afterEvaluate {
        classDirectories.setFrom(files(classDirectories.files.collect {
            fileTree(dir: it, exclude: "**/generated/**")
        }))
    }
}

jacocoTestCoverageVerification {
    violationRules {
        rule {
            limit {
                minimum = 0.3
            }
        }
    }
    afterEvaluate {
        classDirectories.setFrom(files(classDirectories.files.collect {
            fileTree(dir: it, exclude: "**/generated/**")
        }))
    }
}

sonarqube {
    properties {
        property "sonar.projectKey", "ari4java"
        property "sonar.organization", "ari4java"
        property "sonar.host.url", "https://sonarcloud.io"
        property "sonar.login", System.getenv("SONAR_LOGIN")
        property "sonar.java.coveragePlugin", "jacoco"
        property "sonar.coverage.jacoco.xmlReportPaths", "${buildDir}/reports/jacocoTestReport.xml"
    }
}

test.finalizedBy jacocoTestReport
check.dependsOn jacocoTestCoverageVerification

publishing {
    publications {
        maven(MavenPublication) {
            from components.java
            pom {
                name = project.name
                description = "Asterisk ARI interface bindings for Java"
                url = projectUrl
                licenses {
                    license {
                        name = "LPGL-3.0"
                    }
                }
                developers {
                    developer {
                        name = "Graham Brown"
                        email = "grahambrown11@gmail.com"
                    }
                    developer {
                        name = "lenz e."
                        email = "nomail@home"
                    }
                }
                scm {
                    url = projectUrl
                }
            }
        }
    }
    repositories {
        maven {
            name = "sonatype"
            url = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
            credentials {
                username = System.getenv("SONATYPE_USER")
                password = System.getenv("SONATYPE_PASS")
            }
        }
    }

}

signing {
    def signingKeyId = System.getenv("PGP_ID")
    def signingKey = System.getenv("PGP_KEY")
    def signingPassword = System.getenv("PGP_PASS")
    useInMemoryPgpKeys(signingKeyId, signingKey, signingPassword)
    sign publishing.publications
}

spotbugsMain {
    reports {
        html {
            enabled = true
        }
        xml {
            enabled = false
        }
    }
}

spotbugsTest {
    ignoreFailures = true
    reports {
        html {
            enabled = true
        }
        xml {
            enabled = false
        }
    }
}
