From b6fac3efa2d083794f129a422c858433fa768955 Mon Sep 17 00:00:00 2001 From: fabienpuissant Date: Sun, 20 Oct 2024 21:39:46 +0200 Subject: [PATCH] add jooq module with default maven codegen --- .../JooqModuleCodegenConfiguration.java | 157 ++++++++++++++++++ .../jooq/domain/JooqModuleFactory.java | 139 +++++++--------- .../primary/JooqModuleConfiguration.java | 8 +- .../resources/generator/dependencies/pom.xml | 1 + .../springboot/database/jooq-mariadb.feature | 3 +- .../springboot/database/jooq-mssql.feature | 3 +- .../springboot/database/jooq-mysql.feature | 3 +- .../database/jooq-postgresql.feature | 3 +- .../jooq/domain/JooqModuleFactoryTest.java | 2 +- 9 files changed, 224 insertions(+), 95 deletions(-) create mode 100644 src/main/java/tech/jhipster/lite/generator/server/springboot/database/jooq/domain/JooqModuleCodegenConfiguration.java diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jooq/domain/JooqModuleCodegenConfiguration.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jooq/domain/JooqModuleCodegenConfiguration.java new file mode 100644 index 00000000000..1d894742b84 --- /dev/null +++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jooq/domain/JooqModuleCodegenConfiguration.java @@ -0,0 +1,157 @@ +package tech.jhipster.lite.generator.server.springboot.database.jooq.domain; + +import tech.jhipster.lite.shared.error.domain.Assert; + +public final class JooqModuleCodegenConfiguration { + + private final DatabaseType database; + private final String databaseUrl; + private final String user; + private final String inputSchema; + private final String jooqGenerationPackage; + private final String password; + + private JooqModuleCodegenConfiguration(JooqModuleCodegenConfigurationBuilder builder) { + Assert.notNull("database", builder.database); + Assert.notNull("databaseUrl", builder.databaseUrl); + Assert.notNull("user", builder.user); + Assert.notNull("inputSchema", builder.inputSchema); + Assert.notNull("jooqGenerationPackage", builder.jooqGenerationPackage); + + this.database = builder.database; + this.databaseUrl = builder.databaseUrl; + this.user = builder.user; + this.inputSchema = builder.inputSchema; + this.jooqGenerationPackage = builder.jooqGenerationPackage; + this.password = builder.password == null ? "" : builder.password; + } + + public String getConfiguration() { + return String.format( + """ + + %s + %s + %s + + + + %s + .* + %s + + + %s + target/generated-sources/jooq + + + """, + databaseUrl, + user, + password, + databaseJooqName(), + inputSchema, + jooqGenerationPackage + ); + } + + private String databaseJooqName() { + return switch (database) { + case POSTGRESQL -> "org.jooq.meta.postgres.PostgresDatabase"; + case MYSQL -> "org.jooq.meta.mysql.MySQLDatabase"; + case MARIADB -> "org.jooq.meta.mariadb.MariaDBDatabase"; + case MSSQL -> "org.jooq.meta.sqlserver.SQLServerDatabase"; + }; + } + + public static JooqModuleCodegenConfigurationBuilder builder() { + return new JooqModuleCodegenConfigurationBuilder(); + } + + public interface JooqModuleCodegenConfigurationDatabaseBuilder { + JooqModuleCodegenConfigurationDatabaseUrlBuilder database(DatabaseType database); + } + + public interface JooqModuleCodegenConfigurationDatabaseUrlBuilder { + JooqModuleCodegenConfigurationUserBuilder databaseUrl(String databaseUrl); + } + + public interface JooqModuleCodegenConfigurationUserBuilder { + JooqModuleCodegenConfigurationInputSchemaBuilder user(String user); + } + + public interface JooqModuleCodegenConfigurationInputSchemaBuilder { + JooqModuleCodegenConfigurationJooqGenerationPackageBuilder inputSchema(String inputSchema); + } + + public interface JooqModuleCodegenConfigurationJooqGenerationPackageBuilder { + JooqModuleCodegenConfigurationBuilder jooqGenerationPackage(String jooqGenerationPackage); + } + + public interface JooqModuleCodegenConfigurationPasswordBuilder { + JooqModuleCodegenConfiguration password(String password); + } + + public static final class JooqModuleCodegenConfigurationBuilder + implements + JooqModuleCodegenConfigurationDatabaseBuilder, + JooqModuleCodegenConfigurationDatabaseUrlBuilder, + JooqModuleCodegenConfigurationUserBuilder, + JooqModuleCodegenConfigurationInputSchemaBuilder, + JooqModuleCodegenConfigurationJooqGenerationPackageBuilder, + JooqModuleCodegenConfigurationPasswordBuilder { + + private DatabaseType database; + private String databaseUrl; + private String user; + private String inputSchema; + private String jooqGenerationPackage; + private String password; + + @Override + public JooqModuleCodegenConfigurationDatabaseUrlBuilder database(DatabaseType database) { + this.database = database; + + return this; + } + + @Override + public JooqModuleCodegenConfigurationUserBuilder databaseUrl(String databaseUrl) { + this.databaseUrl = databaseUrl; + + return this; + } + + @Override + public JooqModuleCodegenConfigurationInputSchemaBuilder user(String user) { + this.user = user; + + return this; + } + + @Override + public JooqModuleCodegenConfigurationJooqGenerationPackageBuilder inputSchema(String inputSchema) { + this.inputSchema = inputSchema; + + return this; + } + + @Override + public JooqModuleCodegenConfigurationBuilder jooqGenerationPackage(String jooqGenerationPackage) { + this.jooqGenerationPackage = jooqGenerationPackage; + + return this; + } + + @Override + public JooqModuleCodegenConfiguration password(String password) { + this.password = password; + + return build(); + } + + public JooqModuleCodegenConfiguration build() { + return new JooqModuleCodegenConfiguration(this); + } + } +} diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jooq/domain/JooqModuleFactory.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jooq/domain/JooqModuleFactory.java index 777d2989b6b..ef05ca0dd12 100644 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jooq/domain/JooqModuleFactory.java +++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jooq/domain/JooqModuleFactory.java @@ -1,6 +1,7 @@ package tech.jhipster.lite.generator.server.springboot.database.jooq.domain; import static tech.jhipster.lite.generator.server.springboot.database.jooq.domain.CommonModuleBuilder.commonModuleBuilder; +import static tech.jhipster.lite.generator.server.springboot.database.jooq.domain.DatabaseType.*; import static tech.jhipster.lite.module.domain.JHipsterModule.*; import static tech.jhipster.lite.module.domain.JHipsterModule.lineBeforeText; @@ -16,8 +17,13 @@ public class JooqModuleFactory { + public static final String GENERATE = "generate"; + public static final String JOOQ_CODEGEN = "jooq-codegen"; + public static final String JOOQ_CODEGEN_MAVEN = "jooq-codegen-maven"; + public static final String ORG_JOOQ = "org.jooq"; + public static final String MSSQL_PASSWORD = "yourStrong(!)Password"; public static final String ORG_POSTGRESQL = "org.postgresql"; - private static final String MYSQL = "mysql"; + private static final String MYSQL_PACKAGE = "mysql"; private static final String MYSQL_GROUP_ID = "com.mysql"; private static final String MYSQL_ARTIFACT_ID = "mysql-connector-j"; @@ -37,7 +43,7 @@ public JHipsterModule buildPostgresql(JHipsterModuleProperties properties) { //@formatter:off return commonModuleBuilder( properties, - DatabaseType.POSTGRESQL, + POSTGRESQL, dockerImage, documentationTitle("Postgresql"), artifactId("postgresql") @@ -53,11 +59,18 @@ public JHipsterModule buildPostgresql(JHipsterModuleProperties properties) { .and() .mavenPlugins() .plugin(mavenPlugin() - .groupId("org.jooq") - .artifactId("jooq-codegen-maven") + .groupId(ORG_JOOQ) + .artifactId(JOOQ_CODEGEN_MAVEN) .versionSlug("jooq") - .addExecution(pluginExecution().goals("generate").id("jooq-codegen").phase(MavenBuildPhase.GENERATE_RESOURCES)) - .configuration(jooqCodegenPluginConfiguration("jdbc:postgresql://localhost:5432/" + properties.projectBaseName().name(), DatabaseType.POSTGRESQL, properties.projectBaseName().name(), "", "public")) + .addExecution(pluginExecution().goals(GENERATE).id(JOOQ_CODEGEN).phase(MavenBuildPhase.GENERATE_RESOURCES)) + .configuration(JooqModuleCodegenConfiguration.builder() + .database(POSTGRESQL) + .databaseUrl("jdbc:postgresql://localhost:5432/" + properties.projectBaseName().name()) + .user(properties.projectBaseName().name()) + .inputSchema("public") + .jooqGenerationPackage(properties.basePackage().basePackage()) + .build() + .getConfiguration()) .build()) .and() .springMainProperties() @@ -84,7 +97,7 @@ public JHipsterModule buildMariaDB(JHipsterModuleProperties properties) { //@formatter:off return commonModuleBuilder( properties, - DatabaseType.MARIADB, + MARIADB, dockerImages.get("mariadb"), documentationTitle("MariaDB"), artifactId("mariadb") @@ -96,11 +109,18 @@ public JHipsterModule buildMariaDB(JHipsterModuleProperties properties) { .and() .mavenPlugins() .plugin(mavenPlugin() - .groupId("org.jooq") - .artifactId("jooq-codegen-maven") + .groupId(ORG_JOOQ) + .artifactId(JOOQ_CODEGEN_MAVEN) .versionSlug("jooq") - .addExecution(pluginExecution().goals("generate").id("jooq-codegen").phase(MavenBuildPhase.GENERATE_RESOURCES)) - .configuration(jooqCodegenPluginConfiguration("jdbc:mariadb://localhost:3306/" + properties.projectBaseName().name(), DatabaseType.MARIADB, "root", "", properties.projectBaseName().name())) + .addExecution(pluginExecution().goals(GENERATE).id(JOOQ_CODEGEN).phase(MavenBuildPhase.GENERATE_RESOURCES)) + .configuration(JooqModuleCodegenConfiguration.builder() + .database(MARIADB) + .databaseUrl("jdbc:mariadb://localhost:3306/" + properties.projectBaseName().name()) + .user("root") + .inputSchema(properties.projectBaseName().name()) + .jooqGenerationPackage(properties.basePackage().basePackage()) + .build() + .getConfiguration()) .build()) .and() .springMainProperties() @@ -116,21 +136,28 @@ public JHipsterModule buildMySQL(JHipsterModuleProperties properties) { //@formatter:off return commonModuleBuilder( properties, - DatabaseType.MYSQL, - dockerImages.get(MYSQL), + MYSQL, + dockerImages.get(MYSQL_PACKAGE), documentationTitle("MySQL"), - artifactId(MYSQL) + artifactId(MYSQL_PACKAGE) ) .javaDependencies() .addDependency(javaDependency().groupId(MYSQL_GROUP_ID).artifactId(MYSQL_ARTIFACT_ID).scope(JavaDependencyScope.RUNTIME).build()) .and() .mavenPlugins() .plugin(mavenPlugin() - .groupId("org.jooq") - .artifactId("jooq-codegen-maven") + .groupId(ORG_JOOQ) + .artifactId(JOOQ_CODEGEN_MAVEN) .versionSlug("jooq") - .addExecution(pluginExecution().goals("generate").id("jooq-codegen").phase(MavenBuildPhase.GENERATE_RESOURCES)) - .configuration(jooqCodegenPluginConfiguration("jdbc:mysql://localhost:3306/" + properties.projectBaseName().name(), DatabaseType.MYSQL, "root", "", properties.projectBaseName().name())) + .addExecution(pluginExecution().goals(GENERATE).id(JOOQ_CODEGEN).phase(MavenBuildPhase.GENERATE_RESOURCES)) + .configuration(JooqModuleCodegenConfiguration.builder() + .database(MYSQL) + .databaseUrl("jdbc:mysql://localhost:3306/" + properties.projectBaseName().name()) + .user("root") + .inputSchema(properties.projectBaseName().name()) + .jooqGenerationPackage(properties.basePackage().basePackage()) + .build() + .getConfiguration()) .build()) .and() .springMainProperties() @@ -149,7 +176,7 @@ public JHipsterModule buildMsSQL(JHipsterModuleProperties properties) { //@formatter:off return commonModuleBuilder( properties, - DatabaseType.MSSQL, + MSSQL, dockerImage, documentationTitle("MsSQL"), artifactId("mssqlserver") @@ -166,11 +193,18 @@ public JHipsterModule buildMsSQL(JHipsterModuleProperties properties) { .and() .mavenPlugins() .plugin(mavenPlugin() - .groupId("org.jooq") - .artifactId("jooq-codegen-maven") + .groupId(ORG_JOOQ) + .artifactId(JOOQ_CODEGEN_MAVEN) .versionSlug("jooq") - .addExecution(pluginExecution().goals("generate").id("jooq-codegen").phase(MavenBuildPhase.GENERATE_RESOURCES)) - .configuration(jooqCodegenPluginConfiguration("jdbc:sqlserver://localhost:1433;database=" + properties.projectBaseName().name() + ";trustServerCertificate=true", DatabaseType.MSSQL, "SA", "yourStrong(!)Password", "model")) + .addExecution(pluginExecution().goals(GENERATE).id(JOOQ_CODEGEN).phase(MavenBuildPhase.GENERATE_RESOURCES)) + .configuration(JooqModuleCodegenConfiguration.builder() + .database(MSSQL) + .databaseUrl("jdbc:sqlserver://localhost:1433;database=" + properties.projectBaseName().name() + ";trustServerCertificate=true") + .user("SA") + .inputSchema("model") + .jooqGenerationPackage(properties.basePackage().basePackage()) + .password(MSSQL_PASSWORD) + .getConfiguration()) .build()) .and() .springMainProperties() @@ -179,7 +213,7 @@ public JHipsterModule buildMsSQL(JHipsterModuleProperties properties) { propertyValue("jdbc:sqlserver://localhost:1433;database=" + properties.projectBaseName().name() + ";trustServerCertificate=true") ) .set(propertyKey(SPRING_DATASOURCE_USERNAME), propertyValue("SA")) - .set(propertyKey("spring.datasource.password"), propertyValue("yourStrong(!)Password")) + .set(propertyKey("spring.datasource.password"), propertyValue(MSSQL_PASSWORD)) .set(propertyKey(SPRING_DATASOURCE_DRIVER_CLASS_NAME), propertyValue("com.microsoft.sqlserver.jdbc.SQLServerDriver")) .and() .springTestProperties() @@ -190,7 +224,7 @@ public JHipsterModule buildMsSQL(JHipsterModuleProperties properties) { ) ) .set(propertyKey(SPRING_DATASOURCE_USERNAME), propertyValue("SA")) - .set(propertyKey("spring.datasource.password"), propertyValue("yourStrong(!)Password")) + .set(propertyKey("spring.datasource.password"), propertyValue(MSSQL_PASSWORD)) .and() .mandatoryReplacements() .in(path("src/test/java").append(properties.basePackage().path()).append("IntegrationTest.java")) @@ -206,59 +240,4 @@ public JHipsterModule buildMsSQL(JHipsterModuleProperties properties) { .build(); //@formatter:on } - - //TODO Create an API for MavenPluginConfiguration with a builder - private String jooqCodegenPluginConfiguration( - String databaseUrl, - DatabaseType databaseType, - String user, - String password, - String inputSchema - ) { - return String.format( - """ - - %s - %s - %s - %s - - - - %s - .* - %s - - - org.jooq.codegen - target/generated-sources/jooq - - - """, - databaseDriver(databaseType), - databaseUrl, - user, - password, - databaseJooqName(databaseType), - inputSchema - ); - } - - private static String databaseJooqName(DatabaseType databaseType) { - return switch (databaseType) { - case POSTGRESQL -> "org.jooq.meta.postgres.PostgresDatabase"; - case MYSQL -> "org.jooq.meta.mysql.MySQLDatabase"; - case MARIADB -> "org.jooq.meta.mariadb.MariaDBDatabase"; - case MSSQL -> "org.jooq.meta.sqlserver.SQLServerDatabase"; - }; - } - - private static String databaseDriver(DatabaseType databaseType) { - return switch (databaseType) { - case POSTGRESQL -> "org.postgresql.Driver"; - case MARIADB -> "org.mariadb.jdbc.Driver"; - case MYSQL -> "com.mysql.jdbc.Driver"; - case MSSQL -> "com.microsoft.sqlserver.jdbc.SQLServerDriver"; - }; - } } diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jooq/infrastructure/primary/JooqModuleConfiguration.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jooq/infrastructure/primary/JooqModuleConfiguration.java index b21ddcec1ca..52925550e19 100644 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jooq/infrastructure/primary/JooqModuleConfiguration.java +++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jooq/infrastructure/primary/JooqModuleConfiguration.java @@ -1,11 +1,7 @@ package tech.jhipster.lite.generator.server.springboot.database.jooq.infrastructure.primary; import static tech.jhipster.lite.shared.slug.domain.JHLiteFeatureSlug.JOOQ; -import static tech.jhipster.lite.shared.slug.domain.JHLiteModuleSlug.JOOQ_MARIADB; -import static tech.jhipster.lite.shared.slug.domain.JHLiteModuleSlug.JOOQ_MSSQL; -import static tech.jhipster.lite.shared.slug.domain.JHLiteModuleSlug.JOOQ_MYSQL; -import static tech.jhipster.lite.shared.slug.domain.JHLiteModuleSlug.JOOQ_POSTGRESQL; -import static tech.jhipster.lite.shared.slug.domain.JHLiteModuleSlug.SPRING_BOOT; +import static tech.jhipster.lite.shared.slug.domain.JHLiteModuleSlug.*; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -73,7 +69,7 @@ private static JHipsterModulePropertiesDefinition properties() { } private static JHipsterModuleOrganization organization() { - return JHipsterModuleOrganization.builder().feature(JOOQ).addDependency(SPRING_BOOT).build(); + return JHipsterModuleOrganization.builder().feature(JOOQ).addDependency(SPRING_BOOT).addDependency(MAVEN_JAVA).build(); } private static String[] tags() { diff --git a/src/main/resources/generator/dependencies/pom.xml b/src/main/resources/generator/dependencies/pom.xml index c1fd0decc51..08cfdebc85a 100644 --- a/src/main/resources/generator/dependencies/pom.xml +++ b/src/main/resources/generator/dependencies/pom.xml @@ -70,6 +70,7 @@ 3.1.0 0.0.21 1.0.3 + 0.35.0 3.19.13 diff --git a/src/test/features/server/springboot/database/jooq-mariadb.feature b/src/test/features/server/springboot/database/jooq-mariadb.feature index 7fcf3cc0f4d..e455c4d4895 100644 --- a/src/test/features/server/springboot/database/jooq-mariadb.feature +++ b/src/test/features/server/springboot/database/jooq-mariadb.feature @@ -35,7 +35,6 @@ Feature: Jooq MariaDB module | mariadb.md | And I should have files in "src/main/docker" | mariadb.yml | - And I should have files in "src/main/resources/config" - | application.yml | + And I should have "jdbc:mariadb://localhost:3306/" in "src/main/resources/config/application.yml" And I should have files in "src/test/resources/config" | application-test.yml | diff --git a/src/test/features/server/springboot/database/jooq-mssql.feature b/src/test/features/server/springboot/database/jooq-mssql.feature index 5c430b284fc..3d0526d90f9 100644 --- a/src/test/features/server/springboot/database/jooq-mssql.feature +++ b/src/test/features/server/springboot/database/jooq-mssql.feature @@ -15,8 +15,7 @@ Feature: Jooq MsSql module | MsSQLTestContainerExtension.java | And I should have files in "src/test/resources" | container-license-acceptance.txt | - And I should have files in "src/main/resources/config" - | application.yml | + And I should have "jdbc:sqlserver://localhost:1433/" in "src/main/resources/config/application.yml" And I should have files in "src/test/resources/config" | application-test.yml | diff --git a/src/test/features/server/springboot/database/jooq-mysql.feature b/src/test/features/server/springboot/database/jooq-mysql.feature index 3d597ef3eec..9cbc0561ad6 100644 --- a/src/test/features/server/springboot/database/jooq-mysql.feature +++ b/src/test/features/server/springboot/database/jooq-mysql.feature @@ -11,8 +11,7 @@ Feature: Jooq MySQL module | mysql.md | And I should have files in "src/main/docker" | mysql.yml | - And I should have files in "src/main/resources/config" - | application.yml | + And I should have "jdbc:mysql://localhost:3306/" in "src/main/resources/config/application.yml" And I should have files in "src/test/resources/config" | application-test.yml | diff --git a/src/test/features/server/springboot/database/jooq-postgresql.feature b/src/test/features/server/springboot/database/jooq-postgresql.feature index d4b6c46fdb8..16d872cb06c 100644 --- a/src/test/features/server/springboot/database/jooq-postgresql.feature +++ b/src/test/features/server/springboot/database/jooq-postgresql.feature @@ -11,8 +11,7 @@ Feature: Jooq PostgreSQL module | postgresql.md | And I should have files in "src/main/docker" | postgresql.yml | - And I should have files in "src/main/resources/config" - | application.yml | + And I should have "jdbc:postgresql://localhost:5432/" in "src/main/resources/config/application.yml" And I should have files in "src/test/resources/config" | application-test.yml | diff --git a/src/test/java/tech/jhipster/lite/generator/server/springboot/database/jooq/domain/JooqModuleFactoryTest.java b/src/test/java/tech/jhipster/lite/generator/server/springboot/database/jooq/domain/JooqModuleFactoryTest.java index 258ea11b624..0773df34c68 100644 --- a/src/test/java/tech/jhipster/lite/generator/server/springboot/database/jooq/domain/JooqModuleFactoryTest.java +++ b/src/test/java/tech/jhipster/lite/generator/server/springboot/database/jooq/domain/JooqModuleFactoryTest.java @@ -19,7 +19,7 @@ @UnitTest @ExtendWith(MockitoExtension.class) -public class JooqModuleFactoryTest { +class JooqModuleFactoryTest { @Mock private DockerImages dockerImages;