Home > Back-end >  jooq does not generate models with testcontainers in gradle task
jooq does not generate models with testcontainers in gradle task

Time:09-24

I wrote a gradle task to generate jooq models from a postgresql testcontainer. After starting the container, I run migrations using flyway and then the jooq GenerationTool.

The relevant part of my build.gradle.kts looks like this:

buildscript {
  repositories {
    mavenLocal()
    mavenCentral()
  }
  dependencies {
    classpath("org.jooq:jooq-codegen:3.17.4")
    classpath("org.postgresql:postgresql:42.5.0")
    classpath("org.testcontainers:postgresql:1.17.3")
    classpath("org.flywaydb:flyway-core:9.3.0")
  }
}

task("jooqGenerate") {
  doLast {
    PostgreSQLContainer<Nothing>("postgres:14.3")
      .apply { start() }
      .use { pgContainer ->
        val flyway = Flyway(
          FluentConfiguration()
            .locations("filesystem:src/main/resources/db/migration")
            .baselineOnMigrate(true)
            .dataSource(pgContainer.jdbcUrl, pgContainer.username, pgContainer.password)
        )
        flyway.migrate()

        GenerationTool.generate(
          Configuration()
            .withJdbc(
              Jdbc()
                .withDriver(pgContainer.driverClassName)
                .withUrl(pgContainer.jdbcUrl)
                .withUser(pgContainer.username)
                .withPassword(pgContainer.password)
            )
            .withGenerator(
              Generator()
                .withDatabase(Database().withInputSchema("public"))
                .withGenerate(Generate().withPojosAsKotlinDataClasses(true))
                .withTarget(
                  Target()
                    .withPackageName("co.selim.nemrut.jooq")
                    .withDirectory("src/main/java")
                )
            )
        )
      }
  }
}

When I run ./gradlew jooqGenerate I can see that flyway finds the migrations and runs them, jooq logs a bunch of warnings but no code is being generated in src/main/java. What am I missing?

I have also created a reproducer here: https://github.com/wowselim/nemrut

CodePudding user response:

Turns out the target config from the official docs didn't work for me.

When I added the projectDir prefix it started to work.

Target()
  .withPackageName("co.selim.nemrut.jooq")
  .withDirectory("${projectDir}/src/main/java")
  • Related