kuzznya

Gradle testcontainers + liquibase + jooq

Aug 22nd, 2022
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. import nu.studer.gradle.jooq.JooqGenerate
  2. import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
  3. import org.testcontainers.containers.PostgreSQLContainer
  4.  
  5. buildscript {
  6. dependencies {
  7. classpath("org.testcontainers:postgresql:1.17.3")
  8. }
  9. }
  10.  
  11. plugins {
  12. id("org.springframework.boot") version "2.7.2"
  13. id("io.spring.dependency-management") version "1.0.12.RELEASE"
  14. id("nu.studer.jooq") version "7.1.1"
  15. id("org.liquibase.gradle") version "2.1.0"
  16. kotlin("jvm") version "1.6.21"
  17. kotlin("plugin.spring") version "1.6.21"
  18. }
  19.  
  20. group = "some.group.id"
  21. version = "0.0.1-SNAPSHOT"
  22. java.sourceCompatibility = JavaVersion.VERSION_17
  23.  
  24. extra["jooq.version"] = "3.16.9"
  25. extra["db.username"] = "postgres"
  26. extra["db.password"] = "postgres"
  27. extra["db.name"] = "build_db"
  28.  
  29. configurations {
  30. compileOnly {
  31. extendsFrom(configurations.annotationProcessor.get())
  32. }
  33. }
  34.  
  35. repositories {
  36. mavenCentral()
  37. }
  38.  
  39. dependencies {
  40. implementation("org.springframework.boot:spring-boot-starter-jdbc")
  41. implementation("org.springframework.boot:spring-boot-starter-jooq")
  42. implementation("org.springframework.boot:spring-boot-starter-web")
  43. implementation("org.jetbrains.kotlin:kotlin-reflect")
  44. implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
  45. implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
  46. implementation("org.liquibase:liquibase-core")
  47. implementation("org.jooq:jooq:${project.extra["jooq.version"]}") // override Spring Boot managed dependency
  48. runtimeOnly("org.postgresql:postgresql")
  49. annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
  50. testImplementation("org.springframework.boot:spring-boot-starter-test")
  51.  
  52. liquibaseRuntime("org.liquibase:liquibase-core:${dependencyManagement.importedProperties["liquibase.version"]}")
  53. liquibaseRuntime("info.picocli:picocli:4.6.1")
  54. liquibaseRuntime("org.postgresql:postgresql:${dependencyManagement.importedProperties["postgresql.version"]}")
  55. liquibaseRuntime("javax.xml.bind:jaxb-api:2.3.1")
  56. jooqGenerator("org.postgresql:postgresql:${dependencyManagement.importedProperties["postgresql.version"]}")
  57. jooqGenerator("jakarta.xml.bind:jakarta.xml.bind-api:3.0.1")
  58. }
  59.  
  60. liquibase {
  61. activities.register("main") {
  62. arguments = mapOf(
  63. "changeLogFile" to "src/main/resources/db/changelog/db.changelog-master.xml",
  64. "url" to project.extra["db.url"],
  65. "username" to project.extra["db.username"],
  66. "password" to project.extra["db.password"],
  67. "driver" to "org.postgresql.Driver",
  68. "logLevel" to "info"
  69. )
  70. }
  71. }
  72.  
  73. jooq {
  74. version.set(project.extra["jooq.version"] as String)
  75. edition.set(nu.studer.gradle.jooq.JooqEdition.OSS)
  76.  
  77. configurations {
  78. create("main") {
  79. jooqConfiguration.apply {
  80. logging = org.jooq.meta.jaxb.Logging.INFO
  81. jdbc.apply {
  82. driver = "org.postgresql.Driver"
  83. url = "WILL_BE_REPLACED"
  84. user = project.extra["db.username"] as String
  85. password = project.extra["db.password"] as String
  86. }
  87. generator.apply {
  88. name = "org.jooq.codegen.KotlinGenerator"
  89. database.apply {
  90. name = "org.jooq.meta.postgres.PostgresDatabase"
  91. excludes = "information_schema.*|pg_catalog.*|databasechangelog.*"
  92. }
  93. generate.apply {
  94. isDeprecated = false // do not generate deprecated code used for backwards compatibility
  95. isFluentSetters = true
  96. isGeneratedAnnotation = true
  97. isConstructorPropertiesAnnotation = true
  98. isDaos = true
  99. isSpringAnnotations = true
  100. isPojosEqualsAndHashCode = true
  101. }
  102. target.apply {
  103. packageName = "some.group.id.jooq"
  104. directory = "${buildDir}/generated/sources/jooq/main/kotlin"
  105.  
  106. }
  107. strategy.name = "org.jooq.codegen.DefaultGeneratorStrategy"
  108. }
  109. }
  110. }
  111. }
  112. }
  113.  
  114. class MyPostgreSQLContainer(image: String) : PostgreSQLContainer<MyPostgreSQLContainer>(image)
  115.  
  116. tasks.register("postgresContainer") {
  117. doLast {
  118. val postgres = MyPostgreSQLContainer("postgres:14")
  119. .withUsername(project.extra["db.username"] as String)
  120. .withDatabaseName(project.extra["db.name"] as String)
  121. .withPassword(project.extra["db.password"] as String)
  122. postgres.start()
  123. project.extra["db.url"] = postgres.jdbcUrl
  124. this.extra["instance"] = postgres
  125.  
  126. jooq.configurations.getByName("main").jooqConfiguration.jdbc.url = postgres.jdbcUrl
  127.  
  128. println("PostgreSQL container started with URL ${postgres.jdbcUrl}")
  129. }
  130. }
  131.  
  132. tasks.register("stopPostgresContainer") {
  133. doLast {
  134. (tasks["postgresContainer"].extra["instance"] as MyPostgreSQLContainer).stop()
  135. }
  136. }
  137.  
  138. tasks.register("stopPostgresContainerOnError") {
  139. doLast {
  140. if (tasks["update"].state.failure != null)
  141. (tasks["postgresContainer"].extra["instance"] as MyPostgreSQLContainer).stop()
  142. }
  143. }
  144.  
  145. tasks.named("update") {
  146. dependsOn("postgresContainer")
  147. finalizedBy("stopPostgresContainerOnError")
  148. }
  149.  
  150. tasks.withType<JooqGenerate> {
  151. dependsOn("update")
  152. finalizedBy("stopPostgresContainer")
  153. }
  154.  
  155. tasks.withType<KotlinCompile> {
  156. dependsOn("generateJooq")
  157. kotlinOptions {
  158. freeCompilerArgs = listOf("-Xjsr305=strict")
  159. jvmTarget = "17"
  160. }
  161. }
  162.  
  163. tasks.withType<Test> {
  164. useJUnitPlatform()
  165. }
  166.  
Add Comment
Please, Sign In to add comment