Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply official jOOQ gradle plugin when adding the jOOQ feature. [skip ci] #2254

Draft
wants to merge 5 commits into
base: 4.5.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gradle/templates.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.5"
jooq-gradle-plugin = "3.19.0"
jruby = "9.4.5.0"
json-bind = "2.0.0"
json-smart = "2.5.1"
Expand Down Expand Up @@ -106,6 +107,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" }
json-smart = { module = "net.minidev:json-smart", version.ref = "json-smart" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,22 @@
*/
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 io.micronaut.starter.feature.database.templates.jooqGradleConfiguration;
import io.micronaut.starter.template.RockerWritable;
import jakarta.inject.Singleton;

import java.util.Optional;

@Singleton
public class Jooq implements Feature, MinJdkFeature {
public class Jooq implements Feature {

private final JdbcFeature jdbcFeature;

Expand Down Expand Up @@ -65,6 +66,17 @@ public void apply(GeneratorContext generatorContext) {
.groupId("io.micronaut.sql")
.artifactId("micronaut-jooq")
.compile());
Optional<DatabaseDriverFeature> dbDriverOptional = generatorContext.getFeature(DatabaseDriverFeature.class);
if (generatorContext.getBuildTool().isGradle() && dbDriverOptional.isPresent()) {
DatabaseDriverFeature dbDriver = dbDriverOptional.get();
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());
}
}

@Override
Expand All @@ -82,9 +94,16 @@ 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;
// see https://www.jooq.org/doc/3.19/manual/code-generation/codegen-configuration/
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 -> "";
};
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 has Gradle jooq plugin for language=#language'() {
when:
String template = new BuildBuilder(beanContext, BuildTool.GRADLE)
.features(['jooq'])
String template = new BuildBuilder(beanContext, buildTool)
.features(['jooq', dbType])
.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)
verifier.hasBuildPlugin('org.jooq.jooq-codegen-gradle')

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("""
<dependency>
<groupId>io.micronaut.sql</groupId>
<artifactId>micronaut-jooq</artifactId>
<scope>compile</scope>
</dependency>
""")

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"

where:
language << Language.values().toList()
[buildTool, language, dbType] << [BuildTool.values() - BuildTool.MAVEN, Language.values(),
['h2', 'mariadb', 'mysql', 'oracle', 'postgres', 'sqlserver']]
.combinations()
}
}
Loading