A Gradle plugin for working with the jOOQ code generation tool that supports the Gradle Kotlin DSL. For working with jOOQ โ 3.10, use version 1.x of the plugin. For working with jOOQ >= 3.11, use version 2.x of the plugin.
This is a Kotlin port of the Groovy-based gradle-jooq-plugin. This plugin was developed because it was decided that the Groovy plugin would not add Kotlin DSL support.
The plugin id needs to be added to the plugins
block in the build.gradle.kts
file in order to be used.
plugins {
id("dev.bombinating.jooq-codegen") version "2.0.0"
}
The JDBC driver for introspecting the database must be provided in the dependencies
block using the jooqRuntime
scope.
dependencies {
jooqRuntime(group = "org.postgresql", name = "postgresql", version = "42.2.6")
}
In the jOOQ extension block, the version and edition of jOOQ can be specified, along with whether a dependency should be created between the compilation task and the jOOQ generation tasks.
The defaults are shown below.
import dev.bombinating.gradle.jooq.JooqEdition.OpenSource
...
jooq {
version = "3.11.11"
edition = OpenSource
compileDep = true
}
In addition, there are two more properties that can be specified on the extension, javaExecSpec
and execResultHandler
, that set the default Java execution options for running the jOOQ code generation tool and that specify a handler for the result of running the jOOQ code generation tool.
import dev.bombinating.gradle.jooq.JooqEdition.OpenSource
...
jooq {
version = "3.11.11"
edition = OpenSource
compileDep = false
runConfig = {
setIgnoreExitValue(true)
jvmArgs("-Xmx512M")
setStandardOutput(System.out)
setErrorOutput(System.out)
}
resultHandler = {
if (exitValue != 0) {
throw RuntimeException("jOOQ source code generation failed:\n\n$out")
}
}
}
jOOQ code generation configurations are also configured in the jooq
block by providing a name for the config, along with the source set the config is associated with and the configuration itself, based on the jOOQ codegen XSD
import dev.bombinating.gradle.jooq.JooqEdition.Professional
...
val dbUrl: String? by project
val dbUsername: String? by project
val dbPassword: String? by project
val dbSchema: String? by project
val genDir: String = "$projectDir/generated/src/main/java"
...
jooq {
edition = Professional
"oracle"(sourceSets["main"]) {
jdbc {
driver = "oracle.jdbc.driver.OracleDriver"
url = dbUrl
user = dbUsername
password = dbPassword
}
generator {
database {
name = "org.jooq.meta.oracle.OracleDatabase"
includes = ".*"
excludes = "^BIN\\$.*|flyway_schema_history"
inputSchema = dbSchema
}
target {
directory = genDir
packageName = "com.acme.domain.generated"
}
}
logging = Logging.INFO
}
}
The name of the task to execute to generate the Java code is generate<Name>Jooq
. In the example above, the name of the task will be generateOracleJooq
.
$ ./gradlew generateOracleJooq
In addition to the individual jOOQ tasks, there is a jooqGenerate
task that all of the individual jOOQ tasks depend on.
$ ./gradlew generateJooq
An advantage to using a Kotlin-based plugin is that the generation config is typed and the IDE can provide code completion.