plugins {
    id "java-library"
    id "maven-publish"
    id "jacoco"
    id "idea"
    id "com.adarshr.test-logger" version "4.0.0"
    id "checkstyle"
    id "com.github.spotbugs" version "6.4.2"
    id "org.jreleaser" version "1.20.0"
}

group "com.github.alexdlaird"
version "2.4.0"

java {
    withJavadocJar()
    withSourcesJar()
}

repositories {
    mavenCentral()
}

dependencies {
    implementation "com.google.code.gson:gson:2.13.2"
    implementation "org.yaml:snakeyaml:2.5"
    implementation "org.slf4j:slf4j-api:2.0.17"
    testImplementation "org.slf4j:slf4j-simple:2.0.17"
    testImplementation "org.junit.jupiter:junit-jupiter-api:5.13.4"
    testImplementation "org.mockito:mockito-core:5.20.0"
    testImplementation "org.hamcrest:hamcrest:3.0"
    testImplementation "org.junit-pioneer:junit-pioneer:2.3.0"
    testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.13.4"
}

tasks.register("createProperties", WriteProperties) {
    outputFile new File(projectDir, "src/main/resources/version.properties")
    property "version", version
    finalizedBy sourcesJar
}
processResources.dependsOn createProperties

tasks.register("printVersion") {
    doLast {
        println(project.findProperty("rootProject").version)
    }
}

test {
    jvmArgs += [
            "--add-opens", "java.base/java.util=ALL-UNNAMED",
            "--add-opens", "java.base/java.lang=ALL-UNNAMED"
    ]

    useJUnitPlatform()
    finalizedBy jacocoTestReport
}

testlogger {
    slowThreshold 2000
}

jacoco {
    toolVersion = "0.8.11"
}

jacocoTestReport {
    reports {
        xml.required = true
        xml.destination file("build/reports/jacoco/report.xml")
    }
}

checkstyle {
    ignoreFailures = false
    maxWarnings = 0
}

spotbugs {
    excludeFilter = file("config/spotbugs/exclude.xml")
}

spotbugsMain {
    reports {
        html {
            required = true
        }
    }
}

spotbugsTest {
    reports {
        html {
            required = true
        }
    }
}

publishing {
    publications {
        javaLibrary(MavenPublication) {
            from components.java
            versionMapping {
                usage("java-api") {
                    fromResolutionOf("runtimeClasspath")
                }
                usage("java-runtime") {
                    fromResolutionResult()
                }
            }
            pom {
                name = Project.getName()
                description = "A Java wrapper for ngrok that manages its own binary, making ngrok available via a convenient Java API"
                url = "https://github.com/alexdlaird/java-ngrok"
                licenses {
                    license {
                        name = "MIT"
                        url = "https://github.com/alexdlaird/java-ngrok/blob/master/LICENSE"
                        distribution = "repo"
                    }
                }
                developers {
                    developer {
                        id = "alexdlaird"
                        name = "Alex Laird"
                        email = "contact@alexlaird.com"
                    }
                }
                scm {
                    connection = "scm:git:git://github.com/alexdlaird/java-ngrok.git"
                    developerConnection = "scm:git:ssh://github.com/alexdlaird/java-ngrok.git"
                    url = "https://github.com/alexdlaird/java-ngrok"
                }
            }
        }
    }

    repositories {
        maven {
            url = layout.buildDirectory.dir("staging-deploy")
        }
    }
}

jreleaser {
    signing {
        active = "ALWAYS"
        armored = true
        verify = true
    }

    deploy {
        maven {
            mavenCentral {
                sonatype {
                    active = "ALWAYS"
                    url = "https://central.sonatype.com/api/v1/publisher"
                    stagingRepository(layout.buildDirectory.dir("staging-deploy").get().toString())
                    retryDelay = 60
                }
            }
        }
    }

    release {
        github {
            skipRelease = true
            skipTag = true
        }
    }
}

javadoc {
    options.overview = "src/main/java/overview.html"
    options.addStringOption("Xdoclint:none", "-quiet")
    options.addBooleanOption("html5", true)
    options.addBooleanOption("-allow-script-in-comments", true)
    options.header(file("src/main/resources/header.html").text)
}
