Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save dfrommi/152a8715c61069cc9a8a57fd01130851 to your computer and use it in GitHub Desktop.

Select an option

Save dfrommi/152a8715c61069cc9a8a57fd01130851 to your computer and use it in GitHub Desktop.
Jooq-Codegen with Flyway and Testcontainers in Gradle

Jooq-Codegen with Flyway and Testcontainers in Gradle

At the time of writing, the Gradle-Jooq-Plugin doesn’t offer a good way to use Testcontainers for generation. The fundamental issue is, that the JDBC-URL - or more specifically the port - can’t be read from TestContainers unless it’s started. Thus it can’t be used in the Plugin-configuration and setting it later is hardly possible.

So this example uses the plain Jooq-Generator classes.

import org.flywaydb.gradle.task.FlywayMigrateTask
import org.jooq.codegen.GenerationTool
import org.jooq.meta.jaxb.*
import org.jooq.meta.jaxb.Configuration
import org.jooq.meta.jaxb.Target
import org.testcontainers.containers.PostgreSQLContainer
plugins {
kotlin("jvm") version "1.9.10"
id("org.flywaydb.flyway") version "9.22.1"
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.testcontainers:postgresql:1.19.0")
classpath("org.postgresql:postgresql:42.6.0")
classpath("org.jooq:jooq-codegen:3.18.6")
}
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.jooq:jooq:3.18.6")
}
sourceSets.main {
kotlin.srcDir("src/main/generated")
}
val db = PostgreSQLContainer("postgres:latest")
.withDatabaseName("smart_home")
val jooqConfig = Configuration()
.withLogging(org.jooq.meta.jaxb.Logging.DEBUG)
.withGenerator(
Generator()
.withName("org.jooq.codegen.KotlinGenerator")
.withDatabase(
Database()
.withInputSchema(db.databaseName)
.withExcludes("FLYWAY_.*")
)
.withGenerate(
Generate()
.withGlobalObjectReferences(false)
.withGeneratedAnnotation(false)
.withGlobalSchemaReferences(false)
)
.withTarget(
Target()
.withPackageName("io.github.dfrommi.db")
.withDirectory(projectDir.absolutePath + "/src/main/generated")
)
)
val startDb by tasks.creating {
outputs.upToDateWhen { false }
doLast {
db.start()
}
}
val stopDb by tasks.creating {
dependsOn(startDb)
outputs.upToDateWhen { false }
doLast {
db.stop()
}
}
tasks.withType<FlywayMigrateTask> {
dependsOn(startDb)
finalizedBy(stopDb)
doFirst {
url = db.getJdbcUrl()
user = db.username
password = db.password
defaultSchema = db.databaseName
locations = arrayOf("filesystem:src/main/resources/db")
}
}
task("jooq") {
dependsOn(startDb, "flywayMigrate")
finalizedBy(stopDb)
outputs.upToDateWhen { false }
doFirst {
File(jooqConfig.generator.target.directory).deleteRecursively()
}
doLast {
GenerationTool.generate(
jooqConfig.withJdbc(
Jdbc()
.withDriver(db.driverClassName)
.withUrl(db.getJdbcUrl())
.withUsername(db.username)
.withPassword(db.password)
)
)
}
}
task("generate").dependsOn("jooq")
tasks["build"].dependsOn("generate")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment