DEV Community

Cover image for Generate jooq classes using docker containers
Rohith Kunnath
Rohith Kunnath

Posted on

4 2

Generate jooq classes using docker containers

Tech stack - #java, #maven, #liquibase, #docker

Why?

I will talk about what we did to achieve below things,

  1. Generate jooq-classes from an in-memory or Adhoc database instead of connecting to prelive/live environments.

  2. How to apply all the migrations using liquibase before generating jooq-classes.

  3. Generate jooq-classes based on Postgres driver. Jooq supports generating classes connecting to h2 (in-memory database) but not Postgres. We use Postgres mostly and h2 mostly does not support many features Postgres has.

  4. Avoid using multiple maven plugins and 100 lines of code instead use one maven plugin.

What we did

  1. Start "Test-containers" during maven pre-compile stage. (https://www.testcontainers.org/#about)

  2. Apply liquibase migrations over the test-container.

  3. Generate jooq-classes based for the schema provided.

Where can I find

GitHub logo jango89 / jooqgen-liquibase-postgres

Maven plugin with jooq, liquibase and postgres

What is this

Maven plugin which can be integrated to any maven project Sample :

<plugin>  
    <groupId>com.mytaxi</groupId>  
     <artifactId>jooqgen-liquibase-postgres</artifactId>
    <configuration>
        <schema>bookingoptionsservice</schema> <-- schema name -->
        <packageName>com.mytaxi.bookingoptionsservice</packageName> <-- package to be created for generated classes -->
        <liquibaseChangeLogFile>${liquibase.changeLogFile}</liquibaseChangeLogFile> 
    </configuration>
    <executions>
         <execution>
             <phase>generate-sources</phase>
            <goals>
                 <goal>jooqOverPostgresContainer</goal>
            </goals>
        </execution>
    </executions>
 </plugin>      

What it does

  1. Starts a postgress docker container.
  2. Applies liquibase changes over the container.
  3. Generates JOOQ classes for the source project connecting to postgres container.

Problems and solutions

If generated classes fail to compile,

  1. include /target/generated-sources/jooq/ folder to corresponding compiler plugin.
  2. If kotlin-maven-plugin compilation failes, add
    <configuration>
        <sourceDirs>
            <source>src/main/java</source>
            <source>target/generated-sources/jooq</source>
        </sourceDirs>
    </configuration>
    
  3. If the NoClassDefError happens, this means the class files are missing. Add the following plugin
 <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>${project.build.directory}/generated-sources/jooq</source>
                </sources>
            </configuration>
        </execution>
    </executions>
 </plugin>



Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (1)

Collapse
 
snaketl profile image
Luiz Henrique Feltes •

Nice work!!!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

AWS GenAI Live!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️