
/* Composite build for jooq object generator (JooqGenerator.java) */

plugins {
    id "java"
}

repositories {
    maven {
        url = "https://plugins.gradle.org/m2/"
    }
    mavenCentral()
    mavenLocal()
}

group = "dmo.fs.utils"
version = "4.0.0"

ext {
  vertxVersion = "5.0.11"
}

java {
    sourceCompatibility = JavaVersion.VERSION_25
    targetCompatibility = JavaVersion.VERSION_25
}

def jooqVersion = "3.21.2"

tasks.register('copyDatabaseConfig', Copy) {
    def fromPath = "${rootDir}/../../src/main/resources/"
    from("${rootDir}/../../src/main/resources/database_config.json")
    into new File("build", "resources/main")
    doLast {
        build
    }
}

/*
  Using a java class to generate jooq objects - allows for multiple db's - see ./generate directory
  Composite build - implementation("dmo.fs.utils:generate:0.0.1") - this avoids the circular dependancy error
*/
tasks.register('jooqGenerate', JavaExec) {
    doFirst {
        defaultTasks 'clean'
        classpath = sourceSets.main.runtimeClasspath
    }

    def defaultValue = environment.get("DEFAULT_DB")
    if("mariadb" == defaultValue) {
        environment.remove("DEFAULT_DB")
    }
    environment "VERTXWEB_ENVIRONMENT", "dev"
    mainClass = "dmo.fs.utils.JooqGenerate"
    args = ["dev"]
    ignoreExitValue true
    dependsOn copyDatabaseConfig
}

dependencies {
    implementation platform("io.vertx:vertx-stack-depchain:$vertxVersion")
    implementation "org.apache.logging.log4j:log4j-core:2.24.3"
    implementation "com.fasterxml.jackson.core:jackson-databind:2.21.2"
    implementation "io.vertx:vertx-core:$vertxVersion"
    implementation "io.vertx:vertx-rx-java3:$vertxVersion"
    implementation "io.vertx:vertx-config:$vertxVersion"
    implementation "io.vertx:vertx-jdbc-client:$vertxVersion"
    implementation "org.slf4j:slf4j-reload4j:2.0.13"
    implementation "io.agroal:agroal-pool:2.5"
    implementation "com.h2database:h2:2.4.240"
    implementation "org.xerial:sqlite-jdbc:3.46.0.0"
    implementation "io.vertx:vertx-mysql-client:$vertxVersion"
    implementation "io.vertx:vertx-pg-client:$vertxVersion"
    implementation "org.jooq:jooq:$jooqVersion"
    implementation "org.jooq:jooq-codegen-maven:$jooqVersion"
    implementation "org.jooq:jooq-meta:$jooqVersion"
    implementation "org.jooq:jooq-postgres-extensions:$jooqVersion"
    implementation "javax.annotation:javax.annotation-api:1.3.2"
    implementation "com.google.guava:guava:32.0.1-android"
    runtimeOnly "org.postgresql:postgresql:42.7.10"
}

tasks.withType(JavaExec).configureEach {
    jvmArgs += ["--enable-native-access=ALL-UNNAMED", "--add-opens", "java.base/java.lang=ALL-UNNAMED"]

    if(java.targetCompatibility  >= JavaVersion.VERSION_25) {
        jvmArgs += ["--sun-misc-unsafe-memory-access=allow"]
    }
}
