The work on this software project is in no way associated with my employer nor with the role I'm having at my employer. Any requests for changes will be decided upon exclusively by myself based on my personal preferences. I maintain this project as much or as little as my spare time permits.
Gradle plugin that integrates the jOOQ code generation tool.
For each named jOOQ configuration declared in the build, the plugin adds a task to generate the jOOQ sources from the specified database schema and includes the generated Java sources in the matching source set, if existing. The code generation tasks participate in task configuration avoidance, in build configuration caching, in incremental builds, and in task output caching. The plugin can be applied on both Java projects and Android projects.
You can find more details about the actual jOOQ source code generation in the jOOQ documentation.
The jOOQ plugin is hosted at Bintray's JCenter, also available from the Gradle Plugin Portal.
Recent build scan: https://gradle.com/s/bkc4davu2dvu4
Find out more about build scans for Gradle and Maven at https://scans.gradle.com.
The following functionality is provided by the jOOQ plugin:
- Generate Java sources from a given database schema
- Add the generated Java sources to the name-matching source set, if existing
- Wire task dependencies such that the Java sources are generated before the Java compile task of the name-matching source set compiles them, if existing
- Provide a configuration option to suppress automatic task wiring between the Java compile task and the jOOQ source generation task
The following Gradle configuration changes are contributed by the jOOQ plugin:
- Add the
org.jooq:jooq-codegen
dependency needed to execute the jOOQ code generation tool to the newjooqGenerate
configuration - Add the
org.jooq:jooq
dependency to the name-matchingimplementation
configuration to successfully compile the Java sources generated from the database schema - Use the customizable jOOQ version across all resolved
org.jooq*:jooq*
dependencies
The following Gradle features are supported by the jOOQ plugin:
JooqGenerate
task instances participate in task configuration avoidanceJooqGenerate
task instances participate in configuration cachingJooqGenerate
task instances participate in incremental builds (if the task gets explicitly marked as all inputs being declared)JooqGenerate
task instances participate in task output caching (if the task gets explicitly marked as cacheable)
Plugin version | Compatible Gradle versions | Support for Gradle Kotlin DSL | Support for Gradle Configuration Cache |
---|---|---|---|
5.0+ | 6.1+ | Yes | Yes |
4.0 | 5.0+, 6.0+ | No | No |
See the Migration section on how to migrate your build from jOOQ plugin 4.x to 5.x.
Apply the nu.studer.jooq
plugin to your Gradle project.
plugins {
id 'nu.studer.jooq' version '5.2'
}
plugins {
id("nu.studer.jooq") version "5.2"
}
Add the database driver of the database that the jOOQ code generation tool will introspect to the jooqGenerator
configuration. This ensures that the database driver
is on the classpath when the jOOQ code generation tool is executed. Optionally, you can add additional dependencies that are required to run the jOOQ code generation tool.
dependencies {
jooqGenerator 'org.postgresql:postgresql:42.2.14'
}
dependencies {
jooqGenerator("org.postgresql:postgresql:42.2.14")
}
Specify the version and edition that should be applied to all jOOQ dependencies that are declared in your project either explicitly or pulled in transitively.
Note that the org.jooq:jooq
dependency of the specified version and edition is automatically added to the implementation
configuration of the source set that matches the
name of the declared jOOQ configuration.
jooq {
version = '3.14.7' // the default (can be omitted)
edition = nu.studer.gradle.jooq.JooqEdition.OSS // the default (can be omitted)
}
jooq {
version.set("3.14.7") // the default (can be omitted)
edition.set(nu.studer.gradle.jooq.JooqEdition.OSS) // the default (can be omitted)
}
Enforce a certain version of the jOOQ configuration XML schema by declaring what version of the jOOQ code generation tool to make available to the jOOQ plugin at configuration time, i.e. in the DSL of the jOOQ plugin.
buildscript {
configurations['classpath'].resolutionStrategy.eachDependency {
if (requested.group == 'org.jooq') {
useVersion '3.12.4'
}
}
}
buildscript {
configurations["classpath"].resolutionStrategy.eachDependency {
if (requested.group == "org.jooq") {
useVersion("3.12.4")
}
}
}
Configure the jOOQ generation tool via jooq
extension, made available by the jOOQ plugin. The full set of configuration options when using jOOQ 3.13.x can
be seen on the jOOQ generation tool's Configuration class, or
on the jOOQ XSD.
By default, the generated sources are written to <projectDir>/build/generated-src/jooq/<configurationName>
. The target directory can be changed by
explicitly setting the directory
attribute of the target
configuration of the generator
configuration.
jooq {
version = '3.14.7' // default (can be omitted)
edition = nu.studer.gradle.jooq.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 = 'jdbc:postgresql://localhost:5432/sample'
user = 'some_user'
password = 'some_secret'
properties {
property {
key = 'ssl'
value = 'true'
}
}
}
generator {
name = 'org.jooq.codegen.DefaultGenerator'
database {
name = 'org.jooq.meta.postgres.PostgresDatabase'
inputSchema = 'public'
forcedTypes {
forcedType {
name = 'varchar'
includeExpression = '.*'
includeTypes = 'JSONB?'
}
forcedType {
name = 'varchar'
includeExpression = '.*'
includeTypes = 'INET'
}
}
}
generate {
deprecated = false
records = true
immutablePojos = true
fluentSetters = true
}
target {
packageName = 'nu.studer.sample'
directory = 'build/generated-src/jooq/main' // default (can be omitted)
}
strategy.name = 'org.jooq.codegen.DefaultGeneratorStrategy'
}
}
}
}
}
See the Examples section for complete, exemplary build scripts that apply the jOOQ plugin.
jooq {
version.set("3.14.7") // default (can be omitted)
edition.set(nu.studer.gradle.jooq.JooqEdition.OSS) // default (can be omitted)
configurations {
create("main") { // name of the jOOQ configuration
generateSchemaSourceOnCompilation.set(true) // default (can be omitted)
jooqConfiguration.apply {
logging = org.jooq.meta.jaxb.Logging.WARN
jdbc.apply {
driver = "org.postgresql.Driver"
url = "jdbc:postgresql://localhost:5432/sample"
user = "some_user"
password = "some_secret"
properties.add(Property().withKey("ssl").withValue("true"))
}
generator.apply {
name = "org.jooq.codegen.DefaultGenerator"
database.apply {
name = "org.jooq.meta.postgres.PostgresDatabase"
inputSchema = "public"
forcedTypes.addAll(arrayOf(
ForcedType()
.withName("varchar")
.withIncludeExpression(".*")
.withIncludeTypes("JSONB?"),
ForcedType()
.withName("varchar")
.withIncludeExpression(".*")
.withIncludeTypes("INET")
).toList())
}
generate.apply {
isDeprecated = false
isRecords = true
isImmutablePojos = true
isFluentSetters = true
}
target.apply {
packageName = "nu.studer.sample"
directory = "build/generated-src/jooq/main" // default (can be omitted)
}
strategy.name = "org.jooq.codegen.DefaultGeneratorStrategy"
}
}
}
}
}
See the Examples section for complete, exemplary build scripts that apply the jOOQ plugin.
If you configure the state of the database schema from which to derive the jOOQ sources as an input to the jOOQ task, you can mark the
jOOQ task as having all its inputs declared by setting the allInputsDeclared
task property to true
. The jOOQ task will then participate
in Gradle's incremental build feature. The allInputsDeclared
task property is false
by default.
See here for a complete example on how to enable participation in incremental build and build caching.
tasks.named('generateJooq').configure { allInputsDeclared = true }
tasks.named<nu.studer.gradle.jooq.JooqGenerate>("generateJooq") { allInputsDeclared.set(true) }
It is advisable that the jOOQ Gradle plugin and the Spring Boot Gradle plugin are configured to use the same version of jOOQ.
If you want the Spring Boot plugin to pull in the same version of jOOQ as defined by the jOOQ plugin, you have to explicitly set ext['jooq.version'] = jooq.version.get()
.
The other way around, if you want the jOOQ plugin to pull in the same version of jOOQ as defined by the Spring Boot plugin, you have to explicitly
set jooq.version = dependencyManagement.importedProperties['jooq.version']
.
My recommendation is to generate the jOOQ sources into a distinct folder, e.g. src/generated/jooq or build/generated-src/jooq (default). This avoids overlapping outputs, and it also keeps the door open to let Gradle cache the generated sources which can be a significant build performance gain. The rationale is explained very well in the Build Cache User Guide.
Resemblance of the jOOQ configuration DSL with the Groovy language is coincidental. Complex types that include sequences like ForcedTypes must be defined in the DSL's nesting style:
forcedTypes {
forcedType {
name = 'varchar'
expression = '.*'
types = 'JSONB?'
}
forcedType {
name = 'varchar'
expression = '.*'
types = 'INET'
}
}
The Groovy list style is not supported:
forcedTypes = [
{
name = 'varchar'
expression = '.*'
types = 'JSONB?'
},
{
name = 'varchar'
expression = '.*'
types = 'INET'
}
]
You can generate the jOOQ sources for a given jOOQ configuration by invoking the task generate<configName>Jooq
, e.g. generateTestJooq
. The only exception
being main that is abbreviated to generateJooq
, similarly to how it is done for the JavaCompile
tasks contributed by the java
plugin. The generated jOOQ
sources are automatically added to the source set with the name that matches the name of the given jOOQ configuration.
./gradlew generateJooq
By default, the code generation tasks are automatically configured as dependencies of the corresponding source compilation tasks provided by the JavaBasePlugin
plugin. Hence,
running a build that eventually needs to compile sources will first trigger the required jOOQ code generation tasks. This auto-triggering of the code generation when compiling
the containing source set can be turned off by setting generateSchemaSourceOnCompilation
to false
on the jOOQ configuration.
You can delete the generated jOOQ sources by invoking the task rule cleanGenerate<configName>Jooq
, e.g. cleanGenerateTestJooq
. The only exception
being main that is abbreviated to cleanGenerateJooq
, similarly to how it is done for the JavaCompile
tasks contributed by the java
plugin. The
task rule will delete all files in the folder that is configured as the destination directory, regardless of whether the files were generated by the
jOOQ plugin or not.
./gradlew cleanGenerateJooq
When migrating your build from jOOQ plugin 4.x to 5.x, follow these steps:
- Rename the configuration provided the jOOQ plugin from
jooqRuntime
tojooqGenerator
- Set the
edition
property as a JooqEdition enum value instead of a String value - Wrap the entirety of your jOOQ configurations with a
configurations
block - Rename the jOOQ configuration to the name of the previously passed source set
- Move the
generateSchemaSourceOnCompilation
property assignment from thejooq
block to the desired jOOQ configuration - Wrap the configuration of the generation tool with a
generationTool
block - Rename any references to the jOOQ task type from
JooqTask
toJooqGenerate
- Rename any references to the jOOQ tasks from
generate<configName>JooqSchemaSource
togenerate<configName>Jooq
- Configuring the jOOQ code generation via Gradle Groovy DSL: here.
- Configuring the jOOQ code generation via Gradle Kotlin DSL: here.
- Extracting the jOOQ configuration into a precompiled script plugin: here.
- Passing JVM args to the jOOQ code generation process: here.
- Using a custom generator strategy defined in the same Gradle multi-module project: here.
- Suppressing the task dependency between the compile task and the jOOQ source generation task: here.
- Declaring multiple configurations whose outputs are all added to the same source set: here.
- Participating in incremental build and build caching: here.
- Customizing the execution of the code generation tool: here.
- Normalizing the jOOQ config to ensure relocatability: here.
- Specifying applied jOOQ config XML schema version: here.
- Using Spring Boot's jOOQ version in the jOOQ plugin: here.
- Using Flyway in combination with jOOQ to generate the schema and jOOQ sources: here.
- 5.2.1 - Upgrade to jOOQ 3.14.7
- 5.2 - Fail build if cleaning of output directory is set to false in the jOOQ configuration. Upgrade to jOOQ 3.13.5.
- 5.1.1 - Expose org.jooq:jooq-codegen library as
compile
dependency instead ofruntime
dependency - 5.1.0 - Require explicit opt-in to participate in incremental builds
- 5.0.3 - Clean output directory before generating jOOQ sources
- 5.0.2 - Do not write out JDBC configuration when empty
- 5.0.1 - Support dependency substitution to use different versions of jOOQ dependencies than those pulled in by the jOOQ plugin
- 5.0 - Change DSL. Support Gradle Kotlin DSL. Add normalization hook. Make Gradle 6.1 the minimum version. Upgrade to jOOQ 3.13.4.
- 4.2 - Add new jOOQ editions for Java 8 and Java 6. Upgrade to jOOQ 3.13.1.
- 4.1 - Global flag to turn off auto-generation of jOOQ schema source when compiling the containing source set
- 4.0 - Make Gradle 5.0 the minimum compatible version. Upgrade to jOOQ 3.12.3.
- 3.0.3 - Explicitly add JAXB dependencies to run on JDK 9 and higher out-of-the-box. Upgrade to jOOQ 3.11.9.
- 3.0.2 - Bug fix when running on JDK 9+
- 3.0.1 - Improve Gradle build cache effectiveness of the jOOQ task
- 3.0.0 - Upgrade to jOOQ 3.11.2 (jOOQ 3.11.x breaks compatibility with jOOQ 3.10.x)
- 2.0.11 - Upgrade to jOOQ 3.10.4
- 2.0.10 - Removal of wiring between clean task and deleting generated jOOQ sources
- 2.0.9 - Make jOOQ 3.10.1 the default applied version
- 2.0.8 - Upgrade to jOOQ 3.10.1
- 2.0.7 - Upgrade to jOOQ 3.9.5
- 2.0.6 - Upgrade to jOOQ 3.9.3
- 2.0.5 - Make the jOOQ task parallelizable
- 2.0.4 - Upgrade to jOOQ 3.9.1 and better configuration error messages
- 2.0.3 - Upgrade to jOOQ 3.9.0
- 2.0.2 - Configuration of call-backs for code generation java execution process
- 2.0.1 - Bug fixes
- 2.0.0 - Make jOOQ version used for code generation independent from jOOQ version used by gradle-jooq plugin
- 1.0.6 - Upgrade to jOOQ 3.6.2
Both feedback and contributions are very welcome.
- Double-O-Seven (issue analysis)
- wolfs (design)
- jonnybbb (pr)
- Sineaggi (pr)
- martintreurnicht (pr)
- anuraaga (pr)
- ldaley (pr)
- masc3d (pr)
- mark-vieira (pr)
- felipefzdz (commits)
- oehme (pr)
- jamespedwards42 (idea)
- dubacher (patch)
- lukaseder (patch)
This plugin is available under the Apache License, Version 2.0.
(c) by Etienne Studer