Recently, I started working on a new project and I wanted to give Jooq a go. I also wanted to integrate
Flyway: I wanted Jooq to generate its various classes based off the database schema, and I want to
Flyway to create that schema. That’s all easy enough, but I’m resisting, right now, committing the generated classes to source
control (to avoid the churn and additional maintenance), so how do I make that happen with as little work as possible? How do I make
it work in a CI environment? Thanks to Maven, the answer is lots and lots of XML. :) Let’s take a look…
Adding the dependencies
To add Jooq and Flyway to a project, you need these dependencies:
This will enable the use of the Jooq libraries in your code, as well as for runtime Flyway migrations. The mechanics of both of those are
outside the scope of this post, so, if you need help there, please see the respective project web sites.
Setting up build-time Flyway
The next step is setting up the build to run the Jooq generator. For there to be anything to generate, we need Flyway to generate the schema.
For this project, I’m using H2 for tests, so I’m going to configure Maven and Flyway create an H2 database:
Using the Build Helper plugin, we add Jooq’s output directory to
the build, and we’re in business.
One last note: if you make changes to the Flyway migrate file, you’ll need to execute a mvn clean to remove the test
database and any Flyway checksum caches. If you don’t, your build will fail.
All of this does add a bit to the build process, but, for me, so far it’s justified. As I make changes to the schema,
my Jooq classes are automatically recreated, and the test database is brought up to date.