I do this exact thing in one of my Gradle builds. Runnig this task with
-Penvironment=test will:
- Create a Postgres Docker container
- Initialize it with your Liquibase changelogs
- Generate your jOOQ artifacts, and
- Shut down the container afterwards
You can see if(environment == "test") then Docker + Liquibase operations are invoked.
There's some hard-coded concepts (like environment names, Docker image version, etc), but you should be able to adapt it to your needs.
###################################
import liquibase.Liquibase
import liquibase.database.DatabaseFactory
import liquibase.database.jvm.JdbcConnection
import liquibase.resource.DirectoryResourceAccessor
import java.sql.DriverManager
tasks.register("jooqGenerate") {
group = "jooq"
description = "Runs the jOOQ code generator against the schema defined in the Liquibase changelogs"
doLast {
val environment = findProperty("environment")?.toString() ?: "local"
var postgresContainer: PostgreSQLContainer<Nothing>? = null
val schemaName: String
val driverClassName: String
val jdbcUrl: String
val username: String
val password: String
if(environment == "test") {
postgresContainer = PostgreSQLContainer<Nothing>("postgres:16.1").apply { start() }
schemaName = "public"
driverClassName = postgresContainer!!.driverClassName
jdbcUrl = postgresContainer.jdbcUrl
username = postgresContainer.username
password = postgresContainer.password
Class.forName(driverClassName)
DriverManager.getConnection(jdbcUrl, username, password)
.use { connection ->
connection.autoCommit = true
connection.createStatement().use { statement ->
statement.executeUpdate("create schema if not exists $schemaName")
}
connection.schema = schemaName
JdbcConnection(connection).use { liquibaseJdbcConnection ->
val liquibaseDatabase = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(liquibaseJdbcConnection)
val liquibase = Liquibase("main.yaml", DirectoryResourceAccessor(File("...")), liquibaseDatabase)
liquibase.update("")
liquibase.forceReleaseLocks()
}
}
} else {
schemaName = if(environment == "local") "public" else environment
driverClassName = findProperty(...)!!.toString()
jdbcUrl = findProperty(...)!!.toString()
username = findProperty(...)!!.toString()
password = findProperty(...)!!.toString()
}
GenerationTool.generate(
Configuration()
.withJdbc(
Jdbc()
.withDriver(org.postgresql.Driver::
class.java.name)
.withUrl(jdbcUrl)
.withUsername(username)
.withPassword(password)
)
.withGenerator(
Generator()
.withName(...)
.withStrategy(...)
.withDatabase(...)
.withGenerate(...)
.withTarget(...)
)
)
postgresContainer?.stop()