Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import nu.studer.gradle.jooq.JooqGenerate
- import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
- import org.testcontainers.containers.PostgreSQLContainer
- buildscript {
- dependencies {
- classpath("org.testcontainers:postgresql:1.17.3")
- }
- }
- plugins {
- id("org.springframework.boot") version "2.7.2"
- id("io.spring.dependency-management") version "1.0.12.RELEASE"
- id("nu.studer.jooq") version "7.1.1"
- id("org.liquibase.gradle") version "2.1.0"
- kotlin("jvm") version "1.6.21"
- kotlin("plugin.spring") version "1.6.21"
- }
- group = "some.group.id"
- version = "0.0.1-SNAPSHOT"
- java.sourceCompatibility = JavaVersion.VERSION_17
- extra["jooq.version"] = "3.16.9"
- extra["db.username"] = "postgres"
- extra["db.password"] = "postgres"
- extra["db.name"] = "build_db"
- configurations {
- compileOnly {
- extendsFrom(configurations.annotationProcessor.get())
- }
- }
- repositories {
- mavenCentral()
- }
- dependencies {
- implementation("org.springframework.boot:spring-boot-starter-jdbc")
- implementation("org.springframework.boot:spring-boot-starter-jooq")
- implementation("org.springframework.boot:spring-boot-starter-web")
- implementation("org.jetbrains.kotlin:kotlin-reflect")
- implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
- implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
- implementation("org.liquibase:liquibase-core")
- implementation("org.jooq:jooq:${project.extra["jooq.version"]}") // override Spring Boot managed dependency
- runtimeOnly("org.postgresql:postgresql")
- annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
- testImplementation("org.springframework.boot:spring-boot-starter-test")
- liquibaseRuntime("org.liquibase:liquibase-core:${dependencyManagement.importedProperties["liquibase.version"]}")
- liquibaseRuntime("info.picocli:picocli:4.6.1")
- liquibaseRuntime("org.postgresql:postgresql:${dependencyManagement.importedProperties["postgresql.version"]}")
- liquibaseRuntime("javax.xml.bind:jaxb-api:2.3.1")
- jooqGenerator("org.postgresql:postgresql:${dependencyManagement.importedProperties["postgresql.version"]}")
- jooqGenerator("jakarta.xml.bind:jakarta.xml.bind-api:3.0.1")
- }
- liquibase {
- activities.register("main") {
- arguments = mapOf(
- "changeLogFile" to "src/main/resources/db/changelog/db.changelog-master.xml",
- "url" to project.extra["db.url"],
- "username" to project.extra["db.username"],
- "password" to project.extra["db.password"],
- "driver" to "org.postgresql.Driver",
- "logLevel" to "info"
- )
- }
- }
- jooq {
- version.set(project.extra["jooq.version"] as String)
- edition.set(nu.studer.gradle.jooq.JooqEdition.OSS)
- configurations {
- create("main") {
- jooqConfiguration.apply {
- logging = org.jooq.meta.jaxb.Logging.INFO
- jdbc.apply {
- driver = "org.postgresql.Driver"
- url = "WILL_BE_REPLACED"
- user = project.extra["db.username"] as String
- password = project.extra["db.password"] as String
- }
- generator.apply {
- name = "org.jooq.codegen.KotlinGenerator"
- database.apply {
- name = "org.jooq.meta.postgres.PostgresDatabase"
- excludes = "information_schema.*|pg_catalog.*|databasechangelog.*"
- }
- generate.apply {
- isDeprecated = false // do not generate deprecated code used for backwards compatibility
- isFluentSetters = true
- isGeneratedAnnotation = true
- isConstructorPropertiesAnnotation = true
- isDaos = true
- isSpringAnnotations = true
- isPojosEqualsAndHashCode = true
- }
- target.apply {
- packageName = "some.group.id.jooq"
- directory = "${buildDir}/generated/sources/jooq/main/kotlin"
- }
- strategy.name = "org.jooq.codegen.DefaultGeneratorStrategy"
- }
- }
- }
- }
- }
- class MyPostgreSQLContainer(image: String) : PostgreSQLContainer<MyPostgreSQLContainer>(image)
- tasks.register("postgresContainer") {
- doLast {
- val postgres = MyPostgreSQLContainer("postgres:14")
- .withUsername(project.extra["db.username"] as String)
- .withDatabaseName(project.extra["db.name"] as String)
- .withPassword(project.extra["db.password"] as String)
- postgres.start()
- project.extra["db.url"] = postgres.jdbcUrl
- this.extra["instance"] = postgres
- jooq.configurations.getByName("main").jooqConfiguration.jdbc.url = postgres.jdbcUrl
- println("PostgreSQL container started with URL ${postgres.jdbcUrl}")
- }
- }
- tasks.register("stopPostgresContainer") {
- doLast {
- (tasks["postgresContainer"].extra["instance"] as MyPostgreSQLContainer).stop()
- }
- }
- tasks.register("stopPostgresContainerOnError") {
- doLast {
- if (tasks["update"].state.failure != null)
- (tasks["postgresContainer"].extra["instance"] as MyPostgreSQLContainer).stop()
- }
- }
- tasks.named("update") {
- dependsOn("postgresContainer")
- finalizedBy("stopPostgresContainerOnError")
- }
- tasks.withType<JooqGenerate> {
- dependsOn("update")
- finalizedBy("stopPostgresContainer")
- }
- tasks.withType<KotlinCompile> {
- dependsOn("generateJooq")
- kotlinOptions {
- freeCompilerArgs = listOf("-Xjsr305=strict")
- jvmTarget = "17"
- }
- }
- tasks.withType<Test> {
- useJUnitPlatform()
- }
Add Comment
Please, Sign In to add comment