From afb0f009e34efacdd7b2e4833822571b0de2160b Mon Sep 17 00:00:00 2001 From: Dean Wette Date: Mon, 18 Dec 2023 15:08:30 -0600 Subject: [PATCH 1/4] Apply official jOOQ gradle plugin when adding the jOOQ feature. [skip ci] closes #2228 --- gradle/templates.versions.toml | 2 + .../starter/feature/database/Jooq.java | 19 +++---- .../starter/feature/database/JooqSpec.groovy | 53 +++++-------------- 3 files changed, 22 insertions(+), 52 deletions(-) diff --git a/gradle/templates.versions.toml b/gradle/templates.versions.toml index 0d078261308..9a1a67a690b 100644 --- a/gradle/templates.versions.toml +++ b/gradle/templates.versions.toml @@ -37,6 +37,7 @@ gradle-jrebel-plugin = "1.1.10" gradle-shadow-plugin = "8.1.1" jib-gradle-plugin = "2.8.0" jobrunr = "6.3.3" +jooq-gradle-plugin = "3.19.0" jruby = "9.4.5.0" json-bind = "2.0.0" jte-gradle-plugin= "3.0.3" @@ -104,6 +105,7 @@ gradle-jrebel-plugin = { module = "gradle.plugin.org.zeroturnaround:gradle-jrebe gradle-shadow-plugin = { module = "gradle.plugin.com.github.johnrengelman:shadow", version.ref = "gradle-shadow-plugin" } jib-gradle-plugin = { module = "gradle.plugin.com.google.cloud.tools:jib-gradle-plugin", version.ref = "jib-gradle-plugin" } jobrunr-micronaut = { module = "org.jobrunr:jobrunr-micronaut-feature", version.ref = "jobrunr" } +jooq-gradle-plugin = { module = "org.jooq.jooq-codegen-gradle:org.jooq.jooq-codegen-gradle.gradle.plugin", version.ref = "jooq-gradle-plugin" } jruby = { module = "org.jruby:jruby", version.ref = "jruby" } json-bind = { module = "jakarta.json.bind:jakarta.json.bind-api", version.ref = "json-bind" } jte-gradle-plugin = { module = "gg.jte:jte-gradle-plugin", version.ref = "jte-gradle-plugin" } diff --git a/starter-core/src/main/java/io/micronaut/starter/feature/database/Jooq.java b/starter-core/src/main/java/io/micronaut/starter/feature/database/Jooq.java index ae198cfef54..a091f9284f2 100644 --- a/starter-core/src/main/java/io/micronaut/starter/feature/database/Jooq.java +++ b/starter-core/src/main/java/io/micronaut/starter/feature/database/Jooq.java @@ -15,21 +15,18 @@ */ package io.micronaut.starter.feature.database; -import io.micronaut.core.annotation.NonNull; import io.micronaut.starter.application.ApplicationType; import io.micronaut.starter.application.generator.GeneratorContext; import io.micronaut.starter.build.dependencies.Dependency; +import io.micronaut.starter.build.gradle.GradlePlugin; import io.micronaut.starter.feature.Category; import io.micronaut.starter.feature.Feature; import io.micronaut.starter.feature.FeatureContext; -import io.micronaut.starter.feature.MinJdkFeature; import io.micronaut.starter.feature.database.jdbc.JdbcFeature; - -import io.micronaut.starter.options.JdkVersion; import jakarta.inject.Singleton; @Singleton -public class Jooq implements Feature, MinJdkFeature { +public class Jooq implements Feature { private final JdbcFeature jdbcFeature; @@ -65,6 +62,12 @@ public void apply(GeneratorContext generatorContext) { .groupId("io.micronaut.sql") .artifactId("micronaut-jooq") .compile()); + if (generatorContext.getBuildTool().isGradle()) { + generatorContext.addBuildPlugin(GradlePlugin.builder() + .id("org.jooq.jooq-codegen-gradle") + .lookupArtifactId("org.jooq.jooq-codegen-gradle.gradle.plugin") + .build()); + } } @Override @@ -81,10 +84,4 @@ public String getCategory() { public String getMicronautDocumentation() { return "https://micronaut-projects.github.io/micronaut-sql/latest/guide/index.html#jooq"; } - - @Override - @NonNull - public JdkVersion minJdk() { - return JdkVersion.JDK_11; - } } diff --git a/starter-core/src/test/groovy/io/micronaut/starter/feature/database/JooqSpec.groovy b/starter-core/src/test/groovy/io/micronaut/starter/feature/database/JooqSpec.groovy index 4f1d767f921..a114d1de0dc 100644 --- a/starter-core/src/test/groovy/io/micronaut/starter/feature/database/JooqSpec.groovy +++ b/starter-core/src/test/groovy/io/micronaut/starter/feature/database/JooqSpec.groovy @@ -3,9 +3,11 @@ package io.micronaut.starter.feature.database import io.micronaut.starter.ApplicationContextSpec import io.micronaut.starter.BuildBuilder import io.micronaut.starter.application.ApplicationType +import io.micronaut.starter.build.BuildTestUtil +import io.micronaut.starter.build.BuildTestVerifier +import io.micronaut.starter.build.dependencies.Scope import io.micronaut.starter.fixture.CommandOutputFixture import io.micronaut.starter.options.BuildTool -import io.micronaut.starter.options.JdkVersion import io.micronaut.starter.options.Language import io.micronaut.starter.options.MicronautJdkVersionConfiguration import io.micronaut.starter.options.Options @@ -22,55 +24,24 @@ class JooqSpec extends ApplicationContextSpec implements CommandOutputFixture { readme.contains("https://micronaut-projects.github.io/micronaut-sql/latest/guide/index.html#jooq") } - void 'test gradle jooq feature for language=#language'() { + void 'test buildTool=#buildTool jooq feature for language=#language'() { when: - String template = new BuildBuilder(beanContext, BuildTool.GRADLE) + String template = new BuildBuilder(beanContext, buildTool) .features(['jooq']) .language(language) .jdkVersion(MicronautJdkVersionConfiguration.DEFAULT_OPTION) .render() + BuildTestVerifier verifier = BuildTestUtil.verifier(buildTool, language, template) then: - template.contains('implementation("io.micronaut.sql:micronaut-jooq")') + verifier.hasDependency('io.micronaut.sql','micronaut-jooq', Scope.COMPILE) - where: - language << Language.values().toList() - } - - void 'test maven jooq feature for language=#language'() { - when: - String template = new BuildBuilder(beanContext, BuildTool.MAVEN) - .features(['jooq']) - .language(language) - .jdkVersion(MicronautJdkVersionConfiguration.DEFAULT_OPTION) - .render() - - then: - template.contains(""" - - io.micronaut.sql - micronaut-jooq - compile - -""") - - where: - language << Language.values().toList() - } - - void "test jooq cannot be applied for #language with Java 8"() { - when: - new BuildBuilder(beanContext, BuildTool.GRADLE) - .features(['jooq']) - .language(language) - .jdkVersion(JdkVersion.JDK_8) - .render() - - then: - IllegalArgumentException ex = thrown() - ex.message == "The selected feature jooq requires at latest Java 11" + and: + if (buildTool.isGradle()) { + assert verifier.hasBuildPlugin('org.jooq.jooq-codegen-gradle') + } where: - language << Language.values().toList() + [buildTool,language] << [BuildTool.values(), Language.values()].combinations() } } From 31b574a8f9402bf5d15fec552d1fe9697c95acbf Mon Sep 17 00:00:00 2001 From: Dean Wette Date: Thu, 21 Dec 2023 17:19:40 -0600 Subject: [PATCH 2/4] Add plugin extension rocker file [skip ci] --- .../starter/feature/database/Jooq.java | 36 ++++++++++++++++--- .../jooqGradleConfiguration.rocker.raw | 29 +++++++++++++++ .../starter/feature/database/JooqSpec.groovy | 28 +++++++++++++-- 3 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 starter-core/src/main/java/io/micronaut/starter/feature/database/templates/jooqGradleConfiguration.rocker.raw diff --git a/starter-core/src/main/java/io/micronaut/starter/feature/database/Jooq.java b/starter-core/src/main/java/io/micronaut/starter/feature/database/Jooq.java index a091f9284f2..46312098b48 100644 --- a/starter-core/src/main/java/io/micronaut/starter/feature/database/Jooq.java +++ b/starter-core/src/main/java/io/micronaut/starter/feature/database/Jooq.java @@ -23,8 +23,13 @@ import io.micronaut.starter.feature.Feature; import io.micronaut.starter.feature.FeatureContext; import io.micronaut.starter.feature.database.jdbc.JdbcFeature; +import io.micronaut.starter.feature.database.templates.jooqGradleConfiguration; +import io.micronaut.starter.feature.testresources.DbType; +import io.micronaut.starter.template.RockerWritable; import jakarta.inject.Singleton; +import java.util.Optional; + @Singleton public class Jooq implements Feature { @@ -62,11 +67,19 @@ public void apply(GeneratorContext generatorContext) { .groupId("io.micronaut.sql") .artifactId("micronaut-jooq") .compile()); - if (generatorContext.getBuildTool().isGradle()) { - generatorContext.addBuildPlugin(GradlePlugin.builder() - .id("org.jooq.jooq-codegen-gradle") - .lookupArtifactId("org.jooq.jooq-codegen-gradle.gradle.plugin") - .build()); + Optional dbDriverOptional = generatorContext.getFeature(DatabaseDriverFeature.class); + if (generatorContext.getBuildTool().isGradle() && dbDriverOptional.isPresent()) { + DatabaseDriverFeature dbDriver = dbDriverOptional.get(); + Optional jooqGeneratorDb = jooqGeneratorDb(dbDriver); + if (jooqGeneratorDb.isPresent()) { + generatorContext.addHelpLink("jOOQ Generation with the Gradle plugin", "https://www.jooq.org/doc/3.19/manual/code-generation/codegen-gradle/"); + generatorContext.addHelpLink("jOOQ Configuration Reference", "https://www.jooq.org/doc/3.19/manual/code-generation/codegen-configuration/"); + generatorContext.addBuildPlugin(GradlePlugin.builder() + .id("org.jooq.jooq-codegen-gradle") + .lookupArtifactId("org.jooq.jooq-codegen-gradle.gradle.plugin") + .extension(new RockerWritable(jooqGradleConfiguration.template(dbDriver, jooqGeneratorDb.get()))) + .build()); + } } } @@ -84,4 +97,17 @@ public String getCategory() { public String getMicronautDocumentation() { return "https://micronaut-projects.github.io/micronaut-sql/latest/guide/index.html#jooq"; } + + // see https://www.jooq.org/doc/3.19/manual/code-generation/codegen-configuration/ + private Optional jooqGeneratorDb(DatabaseDriverFeature dbDriver) { + Optional dbType = dbDriver.getDbType(); + return dbType.flatMap(type -> switch (type) { + case MYSQL -> Optional.of("org.jooq.meta.mysql.MySQLDatabase"); + case MARIADB -> Optional.of("org.jooq.meta.mariadb.MariaDBDatabase"); + case ORACLEXE -> Optional.of("org.jooq.meta.oracle.OracleDatabase"); + case SQLSERVER -> Optional.of("org.jooq.meta.sqlserver.SQLServerDatabase"); + case POSTGRESQL -> Optional.of("org.jooq.meta.postgres.PostgresDatabase"); + default -> Optional.empty(); + }); + } } diff --git a/starter-core/src/main/java/io/micronaut/starter/feature/database/templates/jooqGradleConfiguration.rocker.raw b/starter-core/src/main/java/io/micronaut/starter/feature/database/templates/jooqGradleConfiguration.rocker.raw new file mode 100644 index 00000000000..facbdb7bbc8 --- /dev/null +++ b/starter-core/src/main/java/io/micronaut/starter/feature/database/templates/jooqGradleConfiguration.rocker.raw @@ -0,0 +1,29 @@ +@import io.micronaut.starter.feature.database.DatabaseDriverFeature +@args ( + DatabaseDriverFeature dbFeature, + String jooqGeneratorDb +) + +jooq { + configuration { + jdbc { + driver = @dbFeature.getDriverClass() + url = @dbFeature.getJdbcUrl() + user = @dbFeature.getDefaultUser() + password = @dbFeature.getDefaultPassword() + } + generator { + database { + name = @jooqGeneratorDb + includes = ".*" + excludes = "flyway_schema_history" + inputSchema = "public" + } + + target { + packageName = "example.micronaut.jooq" + directory = "build/generated-sources/jooq" + } + } + } +} diff --git a/starter-core/src/test/groovy/io/micronaut/starter/feature/database/JooqSpec.groovy b/starter-core/src/test/groovy/io/micronaut/starter/feature/database/JooqSpec.groovy index a114d1de0dc..c540fcded8c 100644 --- a/starter-core/src/test/groovy/io/micronaut/starter/feature/database/JooqSpec.groovy +++ b/starter-core/src/test/groovy/io/micronaut/starter/feature/database/JooqSpec.groovy @@ -24,7 +24,7 @@ class JooqSpec extends ApplicationContextSpec implements CommandOutputFixture { readme.contains("https://micronaut-projects.github.io/micronaut-sql/latest/guide/index.html#jooq") } - void 'test buildTool=#buildTool jooq feature for language=#language'() { + void 'test buildTool=#buildTool jooq feature does not have jooq plugin for language=#language'() { when: String template = new BuildBuilder(beanContext, buildTool) .features(['jooq']) @@ -36,12 +36,36 @@ class JooqSpec extends ApplicationContextSpec implements CommandOutputFixture { then: verifier.hasDependency('io.micronaut.sql','micronaut-jooq', Scope.COMPILE) + and: + if (buildTool.isGradle()) { + assert !verifier.hasBuildPlugin('org.jooq.jooq-codegen-gradle') + } + + where: + [buildTool,language] << [BuildTool.values()-BuildTool.MAVEN, Language.values()].combinations() + } + + void 'test buildTool=#buildTool jooq feature does not have jooq plugin for language=#language'() { + when: + String template = new BuildBuilder(beanContext, buildTool) + .features(['jooq', dbType]) + .language(language) + .jdkVersion(MicronautJdkVersionConfiguration.DEFAULT_OPTION) + .render() + BuildTestVerifier verifier = BuildTestUtil.verifier(buildTool, language, template) + + then: + verifier.hasDependency('io.micronaut.sql','micronaut-jooq', Scope.COMPILE) + and: if (buildTool.isGradle()) { assert verifier.hasBuildPlugin('org.jooq.jooq-codegen-gradle') } where: - [buildTool,language] << [BuildTool.values(), Language.values()].combinations() + [buildTool,language, dbType] << [BuildTool.values()-BuildTool.MAVEN, + Language.values(), + ['mariadb', 'mysql', 'oracle', 'postgres', 'sqlserver']] + .combinations() } } From 9dfce062e47e098ad44678704d8ab86212ffd7a9 Mon Sep 17 00:00:00 2001 From: Dean Wette Date: Fri, 22 Dec 2023 11:02:57 -0600 Subject: [PATCH 3/4] fix jooq plugin template --- .../templates/jooqGradleConfiguration.rocker.raw | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/starter-core/src/main/java/io/micronaut/starter/feature/database/templates/jooqGradleConfiguration.rocker.raw b/starter-core/src/main/java/io/micronaut/starter/feature/database/templates/jooqGradleConfiguration.rocker.raw index facbdb7bbc8..8ac51208dc0 100644 --- a/starter-core/src/main/java/io/micronaut/starter/feature/database/templates/jooqGradleConfiguration.rocker.raw +++ b/starter-core/src/main/java/io/micronaut/starter/feature/database/templates/jooqGradleConfiguration.rocker.raw @@ -7,14 +7,14 @@ jooq { configuration { jdbc { - driver = @dbFeature.getDriverClass() - url = @dbFeature.getJdbcUrl() - user = @dbFeature.getDefaultUser() - password = @dbFeature.getDefaultPassword() + driver = "@dbFeature.getDriverClass()" + url = "@dbFeature.getJdbcUrl()" + user = "@dbFeature.getDefaultUser()" + password = "@dbFeature.getDefaultPassword()" } generator { database { - name = @jooqGeneratorDb + name = "@jooqGeneratorDb" includes = ".*" excludes = "flyway_schema_history" inputSchema = "public" From 8d1a10f6b0af858315d05c7ca2a69e197c61b6d5 Mon Sep 17 00:00:00 2001 From: Dean Wette Date: Fri, 22 Dec 2023 11:11:46 -0600 Subject: [PATCH 4/4] fix jooq db mapping logic [skip ci] --- .../starter/feature/database/Jooq.java | 38 +++++++++---------- .../starter/feature/database/JooqSpec.groovy | 34 +++-------------- 2 files changed, 22 insertions(+), 50 deletions(-) diff --git a/starter-core/src/main/java/io/micronaut/starter/feature/database/Jooq.java b/starter-core/src/main/java/io/micronaut/starter/feature/database/Jooq.java index 46312098b48..0ce392c86db 100644 --- a/starter-core/src/main/java/io/micronaut/starter/feature/database/Jooq.java +++ b/starter-core/src/main/java/io/micronaut/starter/feature/database/Jooq.java @@ -24,7 +24,6 @@ import io.micronaut.starter.feature.FeatureContext; import io.micronaut.starter.feature.database.jdbc.JdbcFeature; import io.micronaut.starter.feature.database.templates.jooqGradleConfiguration; -import io.micronaut.starter.feature.testresources.DbType; import io.micronaut.starter.template.RockerWritable; import jakarta.inject.Singleton; @@ -70,16 +69,13 @@ public void apply(GeneratorContext generatorContext) { Optional dbDriverOptional = generatorContext.getFeature(DatabaseDriverFeature.class); if (generatorContext.getBuildTool().isGradle() && dbDriverOptional.isPresent()) { DatabaseDriverFeature dbDriver = dbDriverOptional.get(); - Optional jooqGeneratorDb = jooqGeneratorDb(dbDriver); - if (jooqGeneratorDb.isPresent()) { - generatorContext.addHelpLink("jOOQ Generation with the Gradle plugin", "https://www.jooq.org/doc/3.19/manual/code-generation/codegen-gradle/"); - generatorContext.addHelpLink("jOOQ Configuration Reference", "https://www.jooq.org/doc/3.19/manual/code-generation/codegen-configuration/"); - generatorContext.addBuildPlugin(GradlePlugin.builder() - .id("org.jooq.jooq-codegen-gradle") - .lookupArtifactId("org.jooq.jooq-codegen-gradle.gradle.plugin") - .extension(new RockerWritable(jooqGradleConfiguration.template(dbDriver, jooqGeneratorDb.get()))) - .build()); - } + generatorContext.addHelpLink("jOOQ Generation with the Gradle plugin", "https://www.jooq.org/doc/3.19/manual/code-generation/codegen-gradle/"); + generatorContext.addHelpLink("jOOQ Configuration Reference", "https://www.jooq.org/doc/3.19/manual/code-generation/codegen-configuration/"); + generatorContext.addBuildPlugin(GradlePlugin.builder() + .id("org.jooq.jooq-codegen-gradle") + .lookupArtifactId("org.jooq.jooq-codegen-gradle.gradle.plugin") + .extension(new RockerWritable(jooqGradleConfiguration.template(dbDriver, jooqGeneratorDb(dbDriver)))) + .build()); } } @@ -99,15 +95,15 @@ public String getMicronautDocumentation() { } // see https://www.jooq.org/doc/3.19/manual/code-generation/codegen-configuration/ - private Optional jooqGeneratorDb(DatabaseDriverFeature dbDriver) { - Optional dbType = dbDriver.getDbType(); - return dbType.flatMap(type -> switch (type) { - case MYSQL -> Optional.of("org.jooq.meta.mysql.MySQLDatabase"); - case MARIADB -> Optional.of("org.jooq.meta.mariadb.MariaDBDatabase"); - case ORACLEXE -> Optional.of("org.jooq.meta.oracle.OracleDatabase"); - case SQLSERVER -> Optional.of("org.jooq.meta.sqlserver.SQLServerDatabase"); - case POSTGRESQL -> Optional.of("org.jooq.meta.postgres.PostgresDatabase"); - default -> Optional.empty(); - }); + private String jooqGeneratorDb(DatabaseDriverFeature dbDriver) { + return switch (dbDriver.getName()) { + case "h2" -> "org.jooq.meta.h2.H2Database"; + case "mariadb" -> "org.jooq.meta.mariadb.MariaDBDatabase"; + case "mysql" -> "org.jooq.meta.mysql.MySQLDatabase"; + case "oracle" -> "org.jooq.meta.oracle.OracleDatabase"; + case "postgres" -> "org.jooq.meta.postgres.PostgresDatabase"; + case "sqlserver" -> "org.jooq.meta.sqlserver.SQLServerDatabase"; + default -> ""; + }; } } diff --git a/starter-core/src/test/groovy/io/micronaut/starter/feature/database/JooqSpec.groovy b/starter-core/src/test/groovy/io/micronaut/starter/feature/database/JooqSpec.groovy index c540fcded8c..c59f1dbed3f 100644 --- a/starter-core/src/test/groovy/io/micronaut/starter/feature/database/JooqSpec.groovy +++ b/starter-core/src/test/groovy/io/micronaut/starter/feature/database/JooqSpec.groovy @@ -24,28 +24,7 @@ class JooqSpec extends ApplicationContextSpec implements CommandOutputFixture { readme.contains("https://micronaut-projects.github.io/micronaut-sql/latest/guide/index.html#jooq") } - void 'test buildTool=#buildTool jooq feature does not have jooq plugin for language=#language'() { - when: - String template = new BuildBuilder(beanContext, buildTool) - .features(['jooq']) - .language(language) - .jdkVersion(MicronautJdkVersionConfiguration.DEFAULT_OPTION) - .render() - BuildTestVerifier verifier = BuildTestUtil.verifier(buildTool, language, template) - - then: - verifier.hasDependency('io.micronaut.sql','micronaut-jooq', Scope.COMPILE) - - and: - if (buildTool.isGradle()) { - assert !verifier.hasBuildPlugin('org.jooq.jooq-codegen-gradle') - } - - where: - [buildTool,language] << [BuildTool.values()-BuildTool.MAVEN, Language.values()].combinations() - } - - void 'test buildTool=#buildTool jooq feature does not have jooq plugin for language=#language'() { + void 'test buildTool=#buildTool jooq feature has Gradle jooq plugin for language=#language'() { when: String template = new BuildBuilder(beanContext, buildTool) .features(['jooq', dbType]) @@ -56,16 +35,13 @@ class JooqSpec extends ApplicationContextSpec implements CommandOutputFixture { then: verifier.hasDependency('io.micronaut.sql','micronaut-jooq', Scope.COMPILE) + verifier.hasBuildPlugin('org.jooq.jooq-codegen-gradle') + - and: - if (buildTool.isGradle()) { - assert verifier.hasBuildPlugin('org.jooq.jooq-codegen-gradle') - } where: - [buildTool,language, dbType] << [BuildTool.values()-BuildTool.MAVEN, - Language.values(), - ['mariadb', 'mysql', 'oracle', 'postgres', 'sqlserver']] + [buildTool, language, dbType] << [BuildTool.values() - BuildTool.MAVEN, Language.values(), + ['h2', 'mariadb', 'mysql', 'oracle', 'postgres', 'sqlserver']] .combinations() } }