plugins {
    id "java"
    id "java-library"
    id "maven-publish"
    id "signing"
    id "checkstyle"
}

repositories {
    mavenCentral()
}

group = rootProject.group
version = rootProject.version
description = "Embulk: Core dependencies"

configurations {
    compileClasspath.resolutionStrategy.activateDependencyLocking()
    runtimeClasspath.resolutionStrategy.activateDependencyLocking()
}

tasks.withType(JavaCompile) {
    options.compilerArgs << "-Xlint:deprecation" << "-Xlint:unchecked"
    options.encoding = "UTF-8"
}

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(8)
    }

    withJavadocJar()
    withSourcesJar()
}

dependencies {
    compileOnly project(":embulk-core")
    compileOnly "org.slf4j:slf4j-api:2.0.13"
    compileOnly "org.msgpack:msgpack-core:0.8.24"

    // Use the same "jcl-over-slf4j" version with slf4f-api.
    // "maven-resolver-transport-http(:1.9.20)" needs "org.slf4j:jcl-over-slf4j(:1.7.36)".
    api("org.slf4j:jcl-over-slf4j:2.0.13") {
        exclude group: "org.slf4j", module: "slf4j-api"
    }

    // Buffer
    api "io.netty:netty-buffer:4.1.109.Final"

    // CLI
    api "commons-cli:commons-cli:1.7.0"

    // Config
    api "org.yaml:snakeyaml:2.2"

    // Jackson & Config
    api "com.fasterxml.jackson.core:jackson-annotations:2.16.2"
    api "com.fasterxml.jackson.core:jackson-core:2.16.2"
    api "com.fasterxml.jackson.core:jackson-databind:2.16.2"
    api "com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.16.2"

    // Apache Commons Lang 3 is required from Apache Maven.
    api "org.apache.commons:commons-lang3:3.14.0"

    // Maven
    api "org.apache.maven:maven-artifact:3.9.6"
    api("org.apache.maven:maven-resolver-provider:3.9.6") {
        exclude group: "org.slf4j", module: "slf4j-api"
    }
    api "org.apache.maven.resolver:maven-resolver-api:1.9.20"
    api "org.apache.maven.resolver:maven-resolver-spi:1.9.20"
    api "org.apache.maven.resolver:maven-resolver-util:1.9.20"
    api("org.apache.maven.resolver:maven-resolver-impl:1.9.20") {
        exclude group: "org.slf4j", module: "slf4j-api"
    }
    api("org.apache.maven.resolver:maven-resolver-supplier:1.9.20") {
        exclude group: "org.slf4j", module: "slf4j-api"
    }
    api("org.apache.maven.resolver:maven-resolver-connector-basic:1.9.20") {
        exclude group: "org.slf4j", module: "slf4j-api"
    }
    api("org.apache.maven.resolver:maven-resolver-transport-file:1.9.20") {
        exclude group: "org.slf4j", module: "slf4j-api"
    }
    api("org.apache.maven.resolver:maven-resolver-transport-http:1.9.20") {
        exclude group: "org.slf4j", module: "slf4j-api"
        exclude group: "org.slf4j", module: "jcl-over-slf4j"
    }

    // Apache Maven (maven-resolver-transport-http) depends on commons-codec,
    // but they do not always catch up with the latest commons-codec.
    api "commons-codec:commons-codec:1.17.0"

    // Timestamp
    api "org.embulk:embulk-util-timestamp:0.3.0"

    testImplementation "org.embulk:embulk-spi:0.11"
    testImplementation project(":embulk-core")
    testImplementation project(":embulk-junit4")
    testImplementation "junit:junit:4.13.2"
    testImplementation "org.slf4j:slf4j-simple:2.0.13"
}

javadoc {
    options {
        locale = "en_US"
        encoding = "UTF-8"
        overview = "src/main/html/overview.html"
        links "https://docs.oracle.com/javase/8/docs/api/"
    }
}

jar {
    metaInf {
        from rootProject.file("LICENSE")
    }

    manifest {
        attributes "Implementation-Title": project.name,
                   "Implementation-Version": project.version,
                   "Implementation-Vendor-Id": project.group,
                   "Specification-Title": project.name,
                   "Specification-Version": project.version
    }
}

sourcesJar {
    metaInf {
        from rootProject.file("LICENSE")
    }
}

javadocJar {
    metaInf {
        from rootProject.file("LICENSE")
    }
}

// It should not publish a `.module` file in Maven Central.
// https://docs.gradle.org/current/userguide/publishing_gradle_module_metadata.html#sub:disabling-gmm-publication
tasks.withType(GenerateModuleMetadata) {
    enabled = false
}

publishing {
    publications {
        maven(MavenPublication) {
            groupId = project.group
            artifactId = project.name

            from components.java
            // javadocJar and sourcesJar are added by java.withJavadocJar() and java.withSourcesJar() above.
            // See: https://docs.gradle.org/current/javadoc/org/gradle/api/plugins/JavaPluginExtension.html

            pom {  // https://central.sonatype.org/pages/requirements.html
                name = project.name
                afterEvaluate { project ->
                    // "description" is declared in subproject's build.gradle. It needs to be configured after evaluation.
                    description = project.description
                }
                url = "https://www.embulk.org/"

                licenses {
                    license {
                        // http://central.sonatype.org/pages/requirements.html#license-information
                        name = "The Apache License, Version 2.0"
                        url = "https://www.apache.org/licenses/LICENSE-2.0.txt"
                        distribution = "repo"
                    }
                }

                developers {
                    developer {
                        name = "Sadayuki Furuhashi"
                        email = "frsyuki@gmail.com"
                    }
                    developer {
                        name = "Muga Nishizawa"
                        email = "muga.nishizawa@gmail.com"
                    }
                    developer {
                        name = "Satoshi Akama"
                        email = "satoshiakama@gmail.com"
                    }
                    developer {
                        name = "Dai MIKURUBE"
                        email = "dmikurube@acm.org"
                    }
                    developer {
                        name = "Shinichi Ishimura"
                        email = "shiketaudonko41@gmail.com"
                    }
                    developer {
                        name = "Roman Shtykh"
                        email = "rshtykh@yahoo.com"
                    }
                }

                scm {
                    connection = "scm:git:git://github.com/embulk/embulk.git"
                    developerConnection = "scm:git:git@github.com:embulk/embulk.git"
                    url = "https://github.com/embulk/embulk"
                }

                withXml {
                    project.configurations.compileOnly.allDependencies.each { dependency ->
                        asNode().dependencies[0].appendNode("dependency").with {
                            it.appendNode("groupId", dependency.group)
                            it.appendNode("artifactId", dependency.name)
                            it.appendNode("version", dependency.version)
                            it.appendNode("scope", "provided")
                        }
                    }
                }
            }
        }
    }

    repositories {
        maven {  // publishMavenPublicationToMavenCentralRepository
            name = "mavenCentral"
            if (project.version.endsWith("-SNAPSHOT")) {
                url "https://oss.sonatype.org/content/repositories/snapshots"
            } else {
                url "https://oss.sonatype.org/service/local/staging/deploy/maven2"
            }
            credentials {
                username = project.hasProperty("ossrhUsername") ? ossrhUsername : ""
                password = project.hasProperty("ossrhPassword") ? ossrhPassword : ""
            }
        }
    }
}

signing {
    if (project.hasProperty("signingKey") && project.hasProperty("signingPassword")) {
        logger.lifecycle("Signing with an in-memory key.")
        useInMemoryPgpKeys(signingKey, signingPassword)
    }
    sign publishing.publications.maven
}

tasks.withType(Test) {
    systemProperties System.properties.findAll { it.key.startsWith("org.embulk") }
    testLogging {
        events "passed", "skipped", "failed", "standardOut", "standardError"
        exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
        showCauses = true
        showExceptions = true
        showStackTraces = true
        showStandardStreams = true
        outputs.upToDateWhen { false }
    }
}

tasks.withType(Checkstyle) {
    reports {
        // Not to skip up-to-date checkstyles.
        outputs.upToDateWhen { false }
    }
}

checkstyle {
    toolVersion "8.7"
    configFile = file("${rootProject.projectDir}/config/checkstyle/checkstyle.xml")
    configProperties = [
        "checkstyle.config.path": file("${projectDir}/config/checkstyle")
    ]
    ignoreFailures = false
    maxWarnings = 0  // https://github.com/gradle/gradle/issues/881
}
