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

Add jooq module with maven #11107

Merged
merged 2 commits into from
Nov 14, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package tech.jhipster.lite.generator.server.springboot.database.jooq.application;

import org.springframework.stereotype.Service;
import tech.jhipster.lite.generator.server.springboot.database.jooq.domain.JooqModuleFactory;
import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.docker.DockerImages;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;

@Service
public class JooqApplicationService {

private final JooqModuleFactory factory;

public JooqApplicationService(DockerImages dockerImages) {
factory = new JooqModuleFactory(dockerImages);
}

public JHipsterModule buildPostgresql(JHipsterModuleProperties properties) {
return factory.buildPostgresql(properties);
}

public JHipsterModule buildMariaDB(JHipsterModuleProperties properties) {
return factory.buildMariaDB(properties);
}

public JHipsterModule buildMsSQL(JHipsterModuleProperties properties) {
return factory.buildMsSQL(properties);
}

public JHipsterModule buildMySQL(JHipsterModuleProperties properties) {
return factory.buildMySQL(properties);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package tech.jhipster.lite.generator.server.springboot.database.jooq.domain;

import static tech.jhipster.lite.module.domain.JHipsterModule.*;

import tech.jhipster.lite.module.domain.DocumentationTitle;
import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.docker.DockerImageVersion;
import tech.jhipster.lite.module.domain.file.JHipsterSource;
import tech.jhipster.lite.module.domain.javabuild.ArtifactId;
import tech.jhipster.lite.module.domain.javadependency.JavaDependency;
import tech.jhipster.lite.module.domain.javadependency.JavaDependencyScope;
import tech.jhipster.lite.module.domain.javaproperties.PropertyValue;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;
import tech.jhipster.lite.shared.error.domain.Assert;

final class CommonModuleBuilder {

private static final PropertyValue FALSE = propertyValue(false);

private CommonModuleBuilder() {}

public static JHipsterModule.JHipsterModuleBuilder commonModuleBuilder(
JHipsterModuleProperties properties,
DatabaseType databaseType,
DockerImageVersion dockerImage,
DocumentationTitle documentationTitle,
ArtifactId testContainerArtifactId
) {
Assert.notNull("properties", properties);
Assert.notNull("databaseType", databaseType);
Assert.notNull("dockerImage", dockerImage);
Assert.notNull("documentationTitle", documentationTitle);
Assert.notNull("testContainerArtifactId", testContainerArtifactId);

String databaseId = databaseType.id();
JHipsterSource source = from("server/springboot/database/common");

//@formatter:off
return moduleBuilder(properties)
.context()
.put("srcMainDocker", "src/main/docker") // To be used in <databaseId>>.md file
.put("databaseType", databaseId)
.put(databaseId + "DockerImageWithVersion", dockerImage.fullName()) // To be used in <databaseId>.yml docker-compose file
.and()
.documentation(documentationTitle, source.template("databaseType.md"))
.startupCommands()
.dockerCompose(startupCommand(databaseId))
.and()
.files()
.add(source.append("docker").template(databaseId + ".yml"), toSrcMainDocker().append(databaseId + ".yml"))
.and()
.javaDependencies()
.addDependency(groupId("org.springframework.boot"), artifactId("spring-boot-starter-jooq"))
.addDependency(groupId("com.zaxxer"), artifactId("HikariCP"))
.addDependency(testContainer(testContainerArtifactId))
.and()
.springMainProperties()
.set(propertyKey("spring.datasource.password"), propertyValue(""))
.set(propertyKey("spring.datasource.type"), propertyValue("com.zaxxer.hikari.HikariDataSource"))
.set(propertyKey("spring.datasource.hikari.poolName"), propertyValue("Hikari"))
.set(propertyKey("spring.datasource.hikari.auto-commit"), FALSE)
.and()
.springTestProperties()
.set(
propertyKey("spring.datasource.url"),
propertyValue("jdbc:tc:" + dockerImage.fullName() + ":///" + properties.projectBaseName().name())
)
.set(propertyKey("spring.datasource.username"), propertyValue(properties.projectBaseName().name()))
.set(propertyKey("spring.datasource.password"), propertyValue(""))
.set(propertyKey("spring.datasource.driver-class-name"), propertyValue("org.testcontainers.jdbc.ContainerDatabaseDriver"))
.set(propertyKey("spring.datasource.hikari.maximum-pool-size"), propertyValue(2))
.and();
//@formatter:on
}

private static String startupCommand(String databaseId) {
return "src/main/docker/" + databaseId + ".yml";
}

private static JavaDependency testContainer(ArtifactId testContainerArtifactId) {
return javaDependency()
.groupId("org.testcontainers")
.artifactId(testContainerArtifactId)
.dependencySlug("%s-%s".formatted("testcontainers", testContainerArtifactId))
.versionSlug("testcontainers")
.scope(JavaDependencyScope.TEST)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package tech.jhipster.lite.generator.server.springboot.database.jooq.domain;

enum DatabaseType {
POSTGRESQL("postgresql"),
MYSQL("mysql"),
MARIADB("mariadb"),
MSSQL("mssql");

private final String id;

DatabaseType(String id) {
this.id = id;
}

public String id() {
return id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
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.module.domain.JHipsterModule.*;
import static tech.jhipster.lite.module.domain.JHipsterModule.lineBeforeText;

import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.LogLevel;
import tech.jhipster.lite.module.domain.docker.DockerImageVersion;
import tech.jhipster.lite.module.domain.docker.DockerImages;
import tech.jhipster.lite.module.domain.file.JHipsterSource;
import tech.jhipster.lite.module.domain.javadependency.JavaDependency;
import tech.jhipster.lite.module.domain.javadependency.JavaDependencyScope;
import tech.jhipster.lite.module.domain.mavenplugin.MavenBuildPhase;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;

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_GROUP_ID = "com.mysql";
private static final String MYSQL_ARTIFACT_ID = "mysql-connector-j";

private static final String SPRING_DATASOURCE_URL = "spring.datasource.url";
private static final String SPRING_DATASOURCE_USERNAME = "spring.datasource.username";
private static final String SPRING_DATASOURCE_DRIVER_CLASS_NAME = "spring.datasource.driver-class-name";

private final DockerImages dockerImages;

public JooqModuleFactory(DockerImages dockerImages) {
this.dockerImages = dockerImages;
}

public JHipsterModule buildPostgresql(JHipsterModuleProperties properties) {
DockerImageVersion dockerImage = dockerImages.get("postgres");

//@formatter:off
return commonModuleBuilder(
properties,
DatabaseType.POSTGRESQL,
dockerImage,
documentationTitle("Postgresql"),
artifactId("postgresql")
)
.javaDependencies()
.addDependency(
JavaDependency.builder()
.groupId(groupId(ORG_POSTGRESQL))
.artifactId(artifactId("postgresql"))
.scope(JavaDependencyScope.RUNTIME)
.build()
)
.and()
.mavenPlugins()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NB : gradle should be supported too, by configuration the gradle plugin. See https://www.jooq.org/doc/latest/manual/code-generation/codegen-gradle/
But this must be done in a second PR. Meanwhile you can require maven as a dependency of all jooq module in JooqModuleConfiguration to prevent them from being used with gradle.

.plugin(mavenPlugin()
.groupId(ORG_JOOQ)
.artifactId(JOOQ_CODEGEN_MAVEN)
.versionSlug("jooq")
.addExecution(pluginExecution().goals(GENERATE).id(JOOQ_CODEGEN).phase(MavenBuildPhase.GENERATE_RESOURCES))
.configuration(jooqModuleCodegenConfiguration()
.database(tech.jhipster.lite.module.domain.jooqplugin.DatabaseType.POSTGRESQL)
.databaseUrl("jdbc:postgresql://localhost:5432/" + properties.projectBaseName().name())
.user(properties.projectBaseName().name())
.inputSchema("public")
.build()
.getConfiguration())
.build())
.and()
.springMainProperties()
.set(propertyKey(SPRING_DATASOURCE_URL), propertyValue("jdbc:postgresql://localhost:5432/" + properties.projectBaseName().name()))
.set(propertyKey(SPRING_DATASOURCE_USERNAME), propertyValue(properties.projectBaseName().name()))
.set(propertyKey(SPRING_DATASOURCE_DRIVER_CLASS_NAME), propertyValue("org.postgresql.Driver"))
.and()
.springTestProperties()
.set(
propertyKey(SPRING_DATASOURCE_URL),
propertyValue(
"jdbc:tc:postgresql:" + dockerImage.version().get() + ":///" + properties.projectBaseName().name() + "?TC_TMPFS=/testtmpfs:rw"
)
)
.and()
.springMainLogger(ORG_POSTGRESQL, LogLevel.WARN)
.springTestLogger(ORG_POSTGRESQL, LogLevel.WARN)
.springTestLogger("org.jboss.logging", LogLevel.WARN)
.build();
//@formatter:on
}

public JHipsterModule buildMariaDB(JHipsterModuleProperties properties) {
//@formatter:off
return commonModuleBuilder(
properties,
DatabaseType.MARIADB,
dockerImages.get("mariadb"),
documentationTitle("MariaDB"),
artifactId("mariadb")
)
.javaDependencies()
.addDependency(
javaDependency().groupId("org.mariadb.jdbc").artifactId("mariadb-java-client").scope(JavaDependencyScope.RUNTIME).build()
)
.and()
.mavenPlugins()
.plugin(mavenPlugin()
.groupId(ORG_JOOQ)
.artifactId(JOOQ_CODEGEN_MAVEN)
.versionSlug("jooq")
.addExecution(pluginExecution().goals(GENERATE).id(JOOQ_CODEGEN).phase(MavenBuildPhase.GENERATE_RESOURCES))
.configuration(jooqModuleCodegenConfiguration()
.database(tech.jhipster.lite.module.domain.jooqplugin.DatabaseType.MARIADB)
.databaseUrl("jdbc:mariadb://localhost:3306/" + properties.projectBaseName().name())
.user("root")
.inputSchema("properties.projectBaseName().name()")
.build()
.getConfiguration())
.build())
.and()
.springMainProperties()
.set(propertyKey(SPRING_DATASOURCE_URL), propertyValue("jdbc:mariadb://localhost:3306/" + properties.projectBaseName().name()))
.set(propertyKey(SPRING_DATASOURCE_USERNAME), propertyValue("root"))
.set(propertyKey(SPRING_DATASOURCE_DRIVER_CLASS_NAME), propertyValue("org.mariadb.jdbc.Driver"))
.and()
.build();
//@formatter:on
}

public JHipsterModule buildMySQL(JHipsterModuleProperties properties) {
//@formatter:off
return commonModuleBuilder(
properties,
DatabaseType.MYSQL,
dockerImages.get(MYSQL),
documentationTitle("MySQL"),
artifactId(MYSQL)
)
.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)
.versionSlug("jooq")
.addExecution(pluginExecution().goals(GENERATE).id(JOOQ_CODEGEN).phase(MavenBuildPhase.GENERATE_RESOURCES))
.configuration(jooqModuleCodegenConfiguration()
.database(tech.jhipster.lite.module.domain.jooqplugin.DatabaseType.MYSQL)
.databaseUrl("jdbc:mysql://localhost:3306/" + properties.projectBaseName().name())
.user("root")
.inputSchema("properties.projectBaseName().name()")
.build()
.getConfiguration())
.build())
.and()
.springMainProperties()
.set(propertyKey(SPRING_DATASOURCE_URL), propertyValue("jdbc:mysql://localhost:3306/" + properties.projectBaseName().name()))
.set(propertyKey(SPRING_DATASOURCE_USERNAME), propertyValue("root"))
.set(propertyKey(SPRING_DATASOURCE_DRIVER_CLASS_NAME), propertyValue("com.mysql.cj.jdbc.Driver"))
.and()
.build();
//@formatter:on
}

public JHipsterModule buildMsSQL(JHipsterModuleProperties properties) {
DockerImageVersion dockerImage = dockerImages.get("mcr.microsoft.com/mssql/server");
JHipsterSource source = from("server/springboot/database/common");

//@formatter:off
return commonModuleBuilder(
properties,
DatabaseType.MSSQL,
dockerImage,
documentationTitle("MsSQL"),
artifactId("mssqlserver")
)
.files()
.add(source.append("docker").template("container-license-acceptance.txt"), to("src/test/resources/container-license-acceptance.txt"))
.add(
source.template("MsSQLTestContainerExtension.java"),
toSrcTestJava().append(properties.basePackage().path()).append("MsSQLTestContainerExtension.java")
)
.and()
.javaDependencies()
.addDependency(javaDependency().groupId("com.microsoft.sqlserver").artifactId("mssql-jdbc").scope(JavaDependencyScope.RUNTIME).build())
.and()
.mavenPlugins()
.plugin(mavenPlugin()
.groupId(ORG_JOOQ)
.artifactId(JOOQ_CODEGEN_MAVEN)
.versionSlug("jooq")
.addExecution(pluginExecution().goals(GENERATE).id(JOOQ_CODEGEN).phase(MavenBuildPhase.GENERATE_RESOURCES))
.configuration(jooqModuleCodegenConfiguration()
.database(tech.jhipster.lite.module.domain.jooqplugin.DatabaseType.MSSQL)
.databaseUrl("jdbc:sqlserver://localhost:1433;database=" + properties.projectBaseName().name() + ";trustServerCertificate=true")
.user("SA")
.inputSchema("model")
.password(MSSQL_PASSWORD)
.getConfiguration())
.build())
.and()
.springMainProperties()
.set(
propertyKey(SPRING_DATASOURCE_URL),
propertyValue("jdbc:sqlserver://localhost:1433;database=" + properties.projectBaseName().name() + ";trustServerCertificate=true")
)
.set(propertyKey(SPRING_DATASOURCE_USERNAME), propertyValue("SA"))
.set(propertyKey("spring.datasource.password"), propertyValue(MSSQL_PASSWORD))
.set(propertyKey(SPRING_DATASOURCE_DRIVER_CLASS_NAME), propertyValue("com.microsoft.sqlserver.jdbc.SQLServerDriver"))
.and()
.springTestProperties()
.set(
propertyKey(SPRING_DATASOURCE_URL),
propertyValue(
"jdbc:tc:sqlserver:///;database=" + properties.projectBaseName().name() + ";trustServerCertificate=true?TC_TMPFS=/testtmpfs:rw"
)
)
.set(propertyKey(SPRING_DATASOURCE_USERNAME), propertyValue("SA"))
.set(propertyKey("spring.datasource.password"), propertyValue(MSSQL_PASSWORD))
.and()
.mandatoryReplacements()
.in(path("src/test/java").append(properties.basePackage().path()).append("IntegrationTest.java"))
.add(
lineBeforeText("import org.springframework.boot.test.context.SpringBootTest;"),
"import org.junit.jupiter.api.extension.ExtendWith;"
)
.add(lineBeforeText("public @interface"), "@ExtendWith(MsSQLTestContainerExtension.class)")
.and()
.and()
.springMainLogger("com.microsoft.sqlserver", LogLevel.WARN)
.springMainLogger("org.reflections", LogLevel.WARN)
.build();
//@formatter:on
}
}
Loading