Skip to content

Instantly share code, notes, and snippets.

@stillya
Created July 13, 2022 13:58
Show Gist options
  • Save stillya/98d40f9a4e77a632e8124fce2911f4ac to your computer and use it in GitHub Desktop.
Save stillya/98d40f9a4e77a632e8124fce2911f4ac to your computer and use it in GitHub Desktop.
It's example of configuration convention plugin for jOOQ+TC+Gradle+Flyway
// ====================== IT'S PLUGIN ITSELF ======================
import org.testcontainers.containers.PostgreSQLContainer
import org.testcontainers.utility.ResourceReaper
plugins {
id("java")
id("something.kotlin-conventions")
}
tasks.register("tcStart") {
doLast {
val db = PostgreSQLContainer<Nothing>("postgres:14-alpine").apply {
withUsername("postgres")
withDatabaseName("df_project_constructor")
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("flyway.url", db.jdbcUrl)
}
}
}
tasks.register("tcStop") {
doLast {
val containerId = project.ext["container_id"] as String
val imageName = project.ext["image_name"] as String
println("Stopping container: $containerId")
ResourceReaper
.instance()
.stopAndRemoveContainer(containerId, imageName);
}
}
// ====================== IT'S PROJECT WHERE PLUGIN IS USED ======================
import nu.studer.gradle.jooq.JooqEdition
/*
* Plugins
*/
plugins {
id 'java'
id 'org.springframework.boot' version "$springBootPluginVersion"
id 'io.spring.dependency-management' version "$springDependencyManagerPluginVersion"
id 'nu.studer.jooq' version "$jooqPluginVersion"
id "org.flywaydb.flyway" version "$flywayPluginVersion"
id "something.application-conventions"
id "something.testcontainers-conventions"
}
/**
* Dependencies
*/
dependencies {
// spring
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-jooq'
// postgres
runtimeOnly 'org.postgresql:postgresql'
// jooq
jooqGenerator "org.postgresql:postgresql:$postgresVersion"
// flyway
implementation 'org.flywaydb:flyway-core'
// kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core"
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinStdlibVersion"
// kotlin logging
implementation "io.github.microutils:kotlin-logging-jvm:$kotlinLoggingVersion"
// tests
testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
testImplementation "org.testcontainers:testcontainers:$testContainersVersion"
testImplementation "org.testcontainers:postgresql:$testContainersVersion"
}
/**
* Database
*/
flyway {
user = 'postgres'
password = 'postgres'
schemas = ['public']
locations = ['classpath:db/migration', 'filesystem:src/main/resources/db/migration']
baselineOnMigrate = true
}
/*
* jOOQ configuration
*/
jooq {
version = "$jookVersion" // default (can be omitted)
edition = JooqEdition.OSS // default (can be omitted)
configurations {
main { // name of the jOOQ configuration
generateSchemaSourceOnCompilation = true // default (can be omitted)
generationTool {
logging = org.jooq.meta.jaxb.Logging.INFO
jdbc {
driver = 'org.postgresql.Driver'
user = 'postgres'
password = 'postgres'
}
generator {
name = 'org.jooq.codegen.DefaultGenerator'
database {
name = 'org.jooq.meta.postgres.PostgresDatabase'
inputSchema = 'public'
forcedTypes {
forcedType {
userType = 'com.fasterxml.jackson.databind.JsonNode'
includeTypes = '.*'
includeExpression = '.*JSON.*'
binding = 'something.binding.PostgresJSONBBinding'
}
}
}
generate {
deprecated = false
records = true
pojos = false
immutablePojos = false
fluentSetters = true
daos = false
}
target {
directory = 'build/generated-src/jooq/main' // default (can be omitted)
}
}
}
}
}
}
def generateJooq = tasks.generateJooq {
doFirst {
jooq.configurations.main.jooqConfiguration.jdbc.url = project.ext.db_url
}
finalizedBy(tcStop)
}
flywayMigrate.dependsOn tcStart
generateJooq.dependsOn flywayMigrate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment