import org.gradle.api.tasks.testing.logging.TestLogEvent import org.gradle.jvm.toolchain.JavaLanguageVersion import java.sql.DriverManager // Version strings for libraries val postgresqlVersion: String = "42.7.10" val flywayVersion: String = "12.4.0" val jooqVersion: String = "3.21.0" val logbackVersion: String = "1.5.0" val springSecurityVersion: String = "6.3.1" val springdocOpenApiVersion: String = "2.5.0" val jacksonVersion: String = "2.18.0" val commonsLang3Version: String = "3.14.0" val guavaVersion: String = "33.1.0-jre" val lombokVersion: String = "1.18.30" val junitVersion: String = "5.10.2" val mockitoVersion: String = "5.11.0" val testcontainersVersion: String = "1.20.0" val h2Version: String = "2.3.232" val restAssuredVersion: String = "5.4.0" buildscript { dependencies { classpath("org.postgresql:postgresql:42.7.10") classpath("org.flywaydb:flyway-database-postgresql:12.4.0") } } plugins { id("org.springframework.boot") version "4.0.+" id("io.spring.dependency-management") version "1.1.6" id("org.openapi.generator") version "7.6.0" id("nu.studer.jooq") version "9.0" id("org.flywaydb.flyway") version "12.4.0" id("application") id("java") } group = "net.moustos" version = "0.1.0" repositories { mavenCentral() } dependencies { implementation("org.springframework.boot:spring-boot-starter-web") implementation("org.springframework.boot:spring-boot-starter-data-jpa") implementation("org.springframework.boot:spring-boot-starter-security") implementation("org.springframework.boot:spring-boot-starter-validation") developmentOnly("org.springframework.boot:spring-boot-devtools") implementation("org.postgresql:postgresql:$postgresqlVersion") implementation("org.flywaydb:flyway-core:$flywayVersion") implementation("org.flywaydb:flyway-database-postgresql:$flywayVersion") implementation("org.jooq:jooq:$jooqVersion") implementation("org.jooq:jooq-meta:$jooqVersion") implementation("org.jooq:jooq-codegen:$jooqVersion") jooqGenerator("org.postgresql:postgresql:$postgresqlVersion") jooqGenerator("org.jooq:jooq:$jooqVersion") jooqGenerator("org.jooq:jooq-meta:$jooqVersion") jooqGenerator("org.jooq:jooq-codegen:$jooqVersion") implementation("com.auth0:java-jwt:4.4.0") implementation("org.springframework.security:spring-security-crypto:$springSecurityVersion") implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:$springdocOpenApiVersion") implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jacksonVersion") implementation("ch.qos.logback:logback-core:$logbackVersion") implementation("ch.qos.logback:logback-classic:$logbackVersion") implementation("net.logstash.logback:logstash-logback-encoder:8.0") compileOnly("org.projectlombok:lombok:$lombokVersion") annotationProcessor("org.projectlombok:lombok:$lombokVersion") implementation("org.apache.commons:commons-lang3:$commonsLang3Version") implementation("com.google.guava:guava:$guavaVersion") testImplementation("org.springframework.boot:spring-boot-starter-test") testImplementation("org.springframework.security:spring-security-test") testImplementation("org.testcontainers:testcontainers:$testcontainersVersion") testImplementation("org.testcontainers:junit-jupiter:$testcontainersVersion") testImplementation("org.testcontainers:postgresql:$testcontainersVersion") testImplementation("com.h2database:h2:$h2Version") testImplementation("org.junit.jupiter:junit-jupiter:$junitVersion") testImplementation("org.mockito:mockito-core:$mockitoVersion") testImplementation("org.mockito:mockito-junit-jupiter:$mockitoVersion") testImplementation("io.rest-assured:rest-assured:$restAssuredVersion") } java { toolchain { languageVersion.set(JavaLanguageVersion.of(21)) } } application { mainClass.set("net.moustos.mtgsearch.MtgSearchApplication") } springBoot { buildInfo() } openApiGenerate { generatorName.set("spring") inputSpec.set("$projectDir/openapi/api.yaml") outputDir.set("$projectDir/build/generated") apiPackage.set("net.moustos.mtgsearch.api") modelPackage.set("net.moustos.mtgsearch.model.api") globalProperties.set(mapOf( "apis" to "true", "models" to "true" )) configOptions.set(mapOf( "delegatePattern" to "true", "title" to "MTG Search API", "interfaceOnly" to "false", "skipDefaultInterface" to "false", "useSpringBoot3" to "true", "useJakartaEe" to "true" )) } flyway { url = project.property("db.url") as String user = project.property("db.user") as String password = project.property("db.password") as String } jooq { version.set("$jooqVersion") configurations { create("main") { jooqConfiguration.apply { jdbc.apply { driver = "org.postgresql.Driver" url = project.property("db.url") as String user = project.property("db.user") as String password = project.property("db.password") as String } generator.apply { name = "org.jooq.codegen.JavaGenerator" database.apply { name = "org.jooq.meta.postgres.PostgresDatabase" inputSchema = "public" } target.apply { packageName = "net.moustos.mtgsearch.jooq.generated" directory = "$projectDir/build/generated/jooq" } } } } } } fun isPostgresAvailable(): Boolean { val dbUrl = project.property("db.url") as String val dbUser = project.property("db.user") as String logger.lifecycle("Checking PostgreSQL availability at $dbUrl with user $dbUser") return try { // Load the PostgreSQL driver Class.forName("org.postgresql.Driver") DriverManager.setLoginTimeout(10) DriverManager.getConnection(dbUrl, dbUser, project.property("db.password") as String).use { connection -> logger.lifecycle("Successfully connected to PostgreSQL database.") true } } catch (ex: Exception) { logger.error("Failed to connect to PostgreSQL at $dbUrl with user $dbUser: ${ex.message}") logger.error("Exception details: ", ex) false } } tasks.named("generateJooq") { dependsOn("flywayMigrate") onlyIf { isPostgresAvailable() } doFirst { logger.lifecycle("Starting jOOQ code generation...") } doLast { logger.lifecycle("Finished jOOQ task.") } } tasks.named("flywayMigrate") { onlyIf { isPostgresAvailable() } } tasks.named("compileJava") { dependsOn("openApiGenerate") } sourceSets["main"].java { srcDir("$buildDir/generated/src/main/java") srcDir("$buildDir/generated/jooq") } tasks.withType { options.encoding = "UTF-8" options.compilerArgs.add("-parameters") } tasks.withType { useJUnitPlatform() testLogging { events(TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.FAILED) exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL showStandardStreams = false } } tasks.named("build") { dependsOn("openApiGenerate") }