简体   繁体   中英

How to configure jOOQ with Gradle, Testcontainers and Flyway?

I need example of configuration gradle build file for jOOQ with Testcontainers and Flyway, in official repository of jOOQ I found only maven example and can't translate it to gradle, I try to convert it myself, but got this:

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies { 
        // test container
        classpath "org.testcontainers:postgresql:$testContainersVersion"
    }
}

plugins {
    id 'java'
    id 'nu.studer.jooq' version "$jooqPluginVersion"
    id "org.flywaydb.flyway" version "$flywayPluginVersion"
}

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    // spring
    implementation 'org.springframework.boot:spring-boot-starter'
    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'

    // 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"
}
var JdbcDatabaseContainer<?> postgres = new PostgreSQLContainerProvider().newInstance("14-alpine")
postgres.start()

flyway {
    url = postgres.jdbcUrl
    user = postgres.username
    password = postgres.password
    schemas = ['public']
    locations = ['classpath:db/migration']
    baselineOnMigrate = true
}
tasks { 
    doLast {
        if (postgres.isRunning) {
            println("STOPPING DATABASE CONTAINER")
            postgres.stop()
        }
    }
}

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.WARN
                jdbc {
                    driver = 'org.postgresql.Driver'
                    url = postgres.jdbcUrl
                    user = postgres.username
                    password = postgres.password
                }
                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 = 'dataflow.binding.PostgresJSONBBinding'
                            }
                        }
                    }
                    generate {
                        pojosAsKotlinDataClasses = true
                        deprecated = false
                        records = true
                        pojos = true
                        immutablePojos = false
                        fluentSetters = true
                        daos = true
                    }
                    target {
                        packageName = "dataflow.$dataflowPackageName"
                        directory = 'build/generated-src/jooq/main'  // default (can be omitted)
                    }
                    strategy.name = 'org.jooq.codegen.DefaultGeneratorStrategy'
                }
            }
        }
    }
}

And it's create three container instead of one and don't stop them. PS I use Gradle jOOQ plugin

I had similar case recently with mysql, gradle, flyway and jooq. Base on examples from gradle-jooq-plugin ( https://github.com/etiennestuder/gradle-jooq-plugin/blob/master/example/ ) I came up with working solution for my case, with minor changes it should also work for yours:

import org.testcontainers.containers.MySQLContainer

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath 'org.testcontainers:mysql:1.17.3'
        classpath 'mysql:mysql-connector-java:8.0.29'
        classpath 'org.flywaydb:flyway-mysql:9.0.1'

    }
}

plugins {
    id 'java'
    id 'org.flywaydb.flyway' version '9.0.1'
    id 'nu.studer.jooq' version '7.1.1'
}

repositories {
    mavenLocal()
    mavenCentral()
}

configurations {
    flywayMigration
}

dependencies {
    jooqGenerator('mysql:mysql-connector-java:8.0.29')
}

task mysqlContainer {
    var instance = new MySQLContainer("mysql:8.0.29")
            .withDatabaseName('example')
    instance.start()
    mysqlContainer.ext.jdbcUrl = instance.getJdbcUrl()
    mysqlContainer.ext.username = instance.getUsername()
    mysqlContainer.ext.password = instance.getPassword()
    mysqlContainer.ext.databaseName = instance.getDatabaseName()
    mysqlContainer.ext.instance = instance
}

flyway {
    locations = ['filesystem:./src/main/resources/db/migration']
    configurations = ['flywayMigration']
    url = mysqlContainer.jdbcUrl
    user = mysqlContainer.username
    password = mysqlContainer.password
}


jooq {
    configurations {
        main {
            generationTool {
                logging = org.jooq.meta.jaxb.Logging.WARN
                jdbc {
                    driver = 'com.mysql.cj.jdbc.Driver'
                    url = mysqlContainer.jdbcUrl
                    user =mysqlContainer.username
                    password = mysqlContainer.password
                }
                generator {
                    name = 'org.jooq.codegen.DefaultGenerator'
                    database {
                        name = 'org.jooq.meta.mysql.MySQLDatabase'
                        includes = '.*'
                        inputSchema = mysqlContainer.databaseName
                        outputSchemaToDefault = true
                    }
                    target {
                        packageName = 'com.example.jooq'
                    }
                }
            }
        }
    }
}

tasks.named('generateJooq').configure {
    dependsOn tasks.named('mysqlContainer')
    dependsOn tasks.named('flywayMigrate')

    inputs.files(fileTree('src/main/resources/db/migration'))
            .withPropertyName('migrations')
            .withPathSensitivity(PathSensitivity.RELATIVE)

    allInputsDeclared = true
    doLast {
        mysqlContainer.instance.stop()
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM