plugins {
    id "jacoco"
    id "java-library"
    id "maven-publish"

    id "org.sonarqube" version "6.3.1.5724"
    id "com.diffplug.spotless" version "6.19.0"
    id "io.github.gradle-nexus.publish-plugin" version "2.0.0"
}

repositories {
    mavenLocal()
    mavenCentral()
}

group = groupId
var ver = System.getenv().getOrDefault("RELEASE_VERSION", artifactVersion)
version = ver.startsWith("v") ? ver.substring(1) : ver

sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

dependencies {
    compileOnly "org.jetbrains:annotations:24.0.1"
    implementation "io.goodforgod:gson-configuration:2.0.0"

    testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.11.4"
    testImplementation "org.junit.jupiter:junit-jupiter-api:5.11.4"
    testImplementation "org.junit.jupiter:junit-jupiter-params:5.11.4"
}

test {
    failFast(false)
    useJUnitPlatform()
    testLogging {
        events("passed", "skipped", "failed")
        exceptionFormat("full")
        showStandardStreams(false)
    }

    reports {
        html.required = false
        junitXml.required = true
    }

    environment([
            "": "",
    ])
}

spotless {
    java {
        encoding("UTF-8")
        importOrder()
        removeUnusedImports()
        eclipse("4.21").configFile("${rootDir}/config/codestyle.xml")
    }
}

sonarqube {
    properties {
        property "sonar.host.url", "https://sonarcloud.io"
        property "sonar.organization", "goodforgod"
        property "sonar.projectKey", "GoodforGod_$artifactId"
    }
}

nexusPublishing {
    packageGroup = groupId
    repositories {
        sonatype {
            username = System.getenv("OSS_USERNAME")
            password = System.getenv("OSS_PASSWORD")
            nexusUrl.set(uri("https://ossrh-staging-api.central.sonatype.com/service/local/"))
            snapshotRepositoryUrl.set(uri("https://central.sonatype.com/repository/maven-snapshots/"))
        }
    }
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java

            pom {
                name = "Java Etherscan API"
                url = "https://github.com/GoodforGod/$artifactId"
                description = "Library is a wrapper for EtherScan API."

                license {
                    name = "MIT License"
                    url = "https://github.com/GoodforGod/$artifactId/blob/master/LICENSE"
                    distribution = "repo"
                }

                developer {
                    id = "GoodforGod"
                    name = "Anton Kurako"
                    email = "goodforgod.dev@gmail.com"
                    url = "https://github.com/GoodforGod"
                }

                scm {
                    connection = "scm:git:git://github.com/GoodforGod/${artifactId}.git"
                    developerConnection = "scm:git:ssh://GoodforGod/${artifactId}.git"
                    url = "https://github.com/GoodforGod/$artifactId/tree/master"
                }
            }
        }
    }
    repositories {
        maven {
            def releasesRepoUrl = "https://ossrh-staging-api.central.sonatype.com/service/local/"
            def snapshotsRepoUrl = "https://central.sonatype.com/repository/maven-snapshots/"
            url = version.endsWith("SNAPSHOT") ? snapshotsRepoUrl : releasesRepoUrl
            credentials {
                username System.getenv("OSS_USERNAME")
                password System.getenv("OSS_PASSWORD")
            }
        }
        if (!version.endsWith("SNAPSHOT")) {
            maven {
                name = "GitHubPackages"
                url = "https://maven.pkg.github.com/GoodforGod/$artifactId"
                credentials {
                    username = System.getenv("GITHUB_ACTOR")
                    password = System.getenv("GITHUB_TOKEN")
                }
            }
        }
    }
}

java {
    withJavadocJar()
    withSourcesJar()
}

tasks.withType(JavaCompile) {
    options.encoding("UTF-8")
    options.incremental(true)
    options.fork = true
}

check.dependsOn jacocoTestReport
jacocoTestReport {
    reports {
        xml.required = true
        html.destination file("${buildDir}/jacocoHtml")
    }
}

javadoc {
    options.encoding = "UTF-8"
    if (JavaVersion.current().isJava9Compatible()) {
        options.addBooleanOption("html5", true)
    }
}

if (project.hasProperty("signingKey")) {
    apply plugin: "signing"
    signing {
        def signingKey = findProperty("signingKey")
        def signingPassword = findProperty("signingPassword")
        useInMemoryPgpKeys(signingKey, signingPassword)
        sign publishing.publications.mavenJava
    }
}
