Skip to content

Instantly share code, notes, and snippets.

@msievers
Last active October 25, 2018 06:19
Show Gist options
  • Save msievers/90312c0441c579025e4ab987b3b51c9d to your computer and use it in GitHub Desktop.
Save msievers/90312c0441c579025e4ab987b3b51c9d to your computer and use it in GitHub Desktop.
Run jooq codegen without a database

Motivation

In order to run jooqs codegen, it needs an appropriate database (with the correct schema) to be running. This often leads to checking in jooq generated code into your applications repository to ease the build process.

Idea

jooq provides an option to run the code generator based on flyway migrations without the need for a specific database to be running. The feature is described here as codegen from DDL.

How it works

Internally jooqs codegen spawns a H2 in-memory database and applies the migrations against this database. After the schema is build, the codegen is run based in the freshly created database. The major drawback of this approach is, that vendor specific db extensions are not supported. For example Postgres json(b) will not work, because H2 cannot deal with it.

Usage

The following snippet is jooqs an extract of the relevant elements of jooqs plugin section of an applications pom.xml.

<plugin>
    <groupId>org.jooq</groupId>
    <artifactId>jooq-codegen-maven</artifactId>
    <version>${jooq-codegen-maven.version}</version>
    <configuration>
        <generator>
            <database>
                <name>org.jooq.meta.extensions.ddl.DDLDatabase</name>
                <properties>
                    <property>
                        <key>scripts</key>
                        <value>src/main/resources/db/migration/*.sql</value>
                    </property>
                    <property>
                        <key>sort</key>
                        <value>semantic</value>
                    </property>
                </properties>
            </database>
        </generator>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.jooq</groupId>
            <artifactId>jooq-meta-extensions</artifactId>
            <version>${jooq.version}</version>
        </dependency>
    </dependencies>
</plugin>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment