Created
November 19, 2022 20:43
-
-
Save stillya/0e860ca13039c289e4cb0b45fabedbb5 to your computer and use it in GitHub Desktop.
It's example of configuration convention plugin for jOOQ+TC+Gradle+Flyway. V2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import nu.studer.gradle.jooq.JooqEdition | |
import org.jooq.meta.jaxb.ForcedType | |
import nu.studer.gradle.jooq.JooqGenerate | |
import org.flywaydb.gradle.task.FlywayMigrateTask | |
import org.testcontainers.containers.PostgreSQLContainer | |
plugins { | |
id("java") | |
id("org.flywaydb.flyway") | |
id("nu.studer.jooq") | |
id("my.conventions.kotlin-conventions") | |
} | |
tasks.register("tcStart") { | |
doLast { | |
val db = PostgreSQLContainer<Nothing>("postgres:14-alpine").apply { | |
withUsername("postgres") | |
withDatabaseName("postgres") | |
withPassword("postgres") | |
} | |
db.start() | |
println("Starting container: ${db.containerId}") | |
ext { | |
set("db_url", db.jdbcUrl) | |
set("container_id", db.containerId) | |
set("image_name", db.dockerImageName) | |
set("instance", db) | |
set("flyway.url", db.jdbcUrl) | |
} | |
} | |
} | |
tasks.register("tcStop") { | |
doLast { | |
val containerId = project.ext["container_id"] as String | |
println("Stopping container: $containerId") | |
(project.ext["instance"] as PostgreSQLContainer<*>).stop() | |
} | |
} | |
// Maybe better to move it to platform and just add jooqGenerator configuration for platform, | |
// but I don't want to add jooq plugin dependency | |
dependencies { | |
jooqGenerator("org.postgresql:postgresql:42.4.0") | |
} | |
flyway { | |
user = project.properties["postgresUser"].toString() | |
password = project.properties["postgresPassword"].toString() | |
schemas = arrayOf("public") | |
locations = arrayOf("classpath:db/migration", "filesystem:src/main/resources/db/migration") | |
baselineOnMigrate = true | |
} | |
jooq { | |
version.set("3.15.7") | |
edition.set(JooqEdition.OSS) | |
configurations { | |
create("main") { | |
jooqConfiguration.apply { | |
logging = org.jooq.meta.jaxb.Logging.WARN | |
jdbc.apply { | |
driver = "org.postgresql.Driver" | |
user = project.properties["postgresUser"].toString() | |
password = project.properties["postgresPassword"].toString() | |
} | |
generator.apply { | |
name = "org.jooq.codegen.DefaultGenerator" | |
database.apply { | |
name = "org.jooq.meta.postgres.PostgresDatabase" | |
inputSchema = "public" | |
forcedTypes = listOf( | |
ForcedType().apply { | |
userType = "com.fasterxml.jackson.databind.JsonNode" | |
includeTypes = ".*" | |
includeExpression = ".*JSON.*" | |
binding = "my.binding.PostgresJSONBBinding" | |
}, | |
ForcedType().apply { | |
name = "varchar" | |
includeExpression = ".*" | |
includeTypes = "INET" | |
} | |
) | |
} | |
generate.apply { | |
isDeprecated = false | |
isRecords = true | |
isPojos = false | |
isImmutablePojos = false | |
isFluentSetters = true | |
isDaos = false | |
} | |
target.apply { | |
packageName = project.properties["jooqPackage"].toString() | |
directory = "build/generated-src/jooq/main" | |
} | |
strategy.name = "org.jooq.codegen.DefaultGeneratorStrategy" | |
} | |
} | |
} | |
} | |
tasks.withType<JooqGenerate>().configureEach { | |
dependsOn("flywayMigrate") | |
doFirst { | |
jooq.configurations["main"].jooqConfiguration.jdbc.url = project.extra["db_url"] as String | |
} | |
finalizedBy("tcStop") | |
allInputsDeclared.set(true) | |
} | |
tasks.withType<FlywayMigrateTask>().configureEach { | |
dependsOn("tcStart") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment