📌

Spring BootにjOOQとFlywayを導入する

2024/09/12に公開

前回jOOQとFlywayを使う準備ができたので、Spring Bootに導入を試す

https://zenn.dev/kentama/articles/ce9eba904e62f1

環境

  • Spring Boot: 3.3.3
  • jOOQ: 3.19.11
  • Flyway: 10.10.0
  • PostgreSQL: 15
  • Kotlin: 1.9.25

準備

Spring Initializrでプロジェクト作成する

https://start.spring.io/

作成する際にDependenciesに以下を追加する

  • JOOQ Access Layer
  • Flyway Migration
  • PostgreSQL Driver

jOOQとFlywayのGradleプラグインを追加

作成されたプロジェクトのbuild.gradle.ktsを書き換えて、jOOQとFlywayのGradleプラグインを使えるようにする

build.gradle.kts
+buildscript {
+    dependencies {
+        classpath("org.flywaydb:flyway-database-postgresql:10.10.0")
+    }
+}

plugins {
    kotlin("jvm") version "1.9.25"
    kotlin("plugin.spring") version "1.9.25"
    id("org.springframework.boot") version "3.3.3"
    id("io.spring.dependency-management") version "1.1.6"
+    id("org.jooq.jooq-codegen-gradle") version  "3.19.11"
+    id("org.flywaydb.flyway") version "10.10.0"
}

group = "org.example"
version = "0.0.1-SNAPSHOT"

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-jooq")
    implementation("org.flywaydb:flyway-core")
    implementation("org.flywaydb:flyway-database-postgresql")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    runtimeOnly("org.postgresql:postgresql")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
    testRuntimeOnly("org.junit.platform:junit-platform-launcher")

+    implementation("org.jooq:jooq-meta")
+    implementation("org.jooq:jooq-codegen")
+    implementation("org.jooq:jooq-postgres-extensions:3.19.11")
+    jooqCodegen("org.postgresql:postgresql:42.7.3")
}

kotlin {
    compilerOptions {
        freeCompilerArgs.addAll("-Xjsr305=strict")
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}


+jooq {
+    configuration {
+
+        jdbc {
+            driver = "org.postgresql.Driver"
+            url = "jdbc:postgresql://localhost:5432/postgres"
+            user = "postgres"
+            password = "password"
+        }
+        generator {
+            database {
+                name = "org.jooq.meta.postgres.PostgresDatabase"
+                inputSchema = "public"
+                includes = ".*"
+                excludes = "flyway_schema_history"
+            }
+
+            target {
+                packageName = "org.example.db"
+            }
+        }
+    }
+}
+
+sourceSets.main {
+    java.srcDirs("build/generated-sources/jooq")
+}
+
+tasks.named("compileKotlin") {
+    dependsOn(tasks.named("jooqCodegen"))
+}
+
+flyway {
+    driver = "org.postgresql.Driver"
+    url = "jdbc:postgresql://localhost:5432/postgres"
+    user = "postgres"
+    password = "password"
+    schemas = arrayOf("public")
+    cleanDisabled = false
+}
+
+tasks.named("jooqCodegen") {
+    dependsOn(tasks.named("flywayMigrate"))
+    inputs.files(fileTree("src/main/resources/db/migration"))
+}

Spring Bootの設定

application.propertiesに以下の設定を追加する

application.properties
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=password
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.hikari.auto-commit=false

spring.flyway.url=jdbc:postgresql://localhost:5432/postgres
spring.flyway.user=postgres
spring.flyway.password=password
spring.flyway.driver-class-name=org.postgresql.Driver

起動確認

ここまでできたらSpring Bootを起動させてエラーが発生しなければOK

./gradlew bootRun

Discussion