-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.gradle
160 lines (135 loc) · 4.45 KB
/
build.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import org.testcontainers.containers.PostgreSQLContainer
import org.gradle.api.services.BuildServiceParameters
import org.gradle.api.services.BuildService
buildscript {
dependencies {
// Required dependencies to start testcontainers and do the migration during the build.
classpath libs.testcontainers.postgres
classpath libs.flyway.postgresql
}
}
plugins {
id 'java-library'
// Gradle plugins for jOOQ and Flyway.
alias(libs.plugins.flyway)
alias(libs.plugins.jooq)
}
repositories {
mavenLocal()
mavenCentral()
}
group = 'com.sergeylappo.testcontainers'
version = '1.0.0-SNAPSHOT'
description = 'tc-guide-working-with-jooq-flyway-using-testcontainers-gradle'
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}
dependencies {
implementation libs.bundles.spring.boot
// Dependencies required for the application to use jooq and flyway in runtime.
implementation libs.flyway.postgresql
implementation libs.jooq.core
// Your database driver
runtimeOnly libs.postgresql
// Database driver for the build time.
jooqCodegen libs.postgresql
testImplementation libs.bundles.spring.boot.test
// Testcontainers dependencies for the test tasks.
testImplementation libs.bundles.testcontainers
}
// Here we register service for providing our database during the build.
Provider<PostgresService> dbContainerProvider = project.getGradle()
.getSharedServices()
.registerIfAbsent("postgres", PostgresService.class, {})
tasks.withType(JavaCompile).configureEach {
options.encoding = 'UTF-8'
dependsOn jooqCodegen
}
tasks.withType(Javadoc).configureEach {
options.encoding = 'UTF-8'
}
test {
useJUnitPlatform()
// This is only necessary if you want to reuse container for the tests.
usesService dbContainerProvider
doFirst {
def dbContainer = dbContainerProvider.get().container
systemProperty('spring.datasource.url', dbContainer.jdbcUrl)
systemProperty('spring.datasource.username', dbContainer.username)
systemProperty('spring.datasource.password', dbContainer.password)
}
}
flywayMigrate {
// We need access to our database during the build.
usesService dbContainerProvider
// Define location of the migrations.
locations = ["filesystem:src/main/resources/db/migration"]
// Flyway plugin won't define input files for us, so to follow Gradle convention define them.
inputs.files(fileTree("src/main/resources/db/migration"))
doFirst {
def dbContainer = dbContainerProvider.get().container
// Set up the flyway config.
url = dbContainer.jdbcUrl
user = dbContainer.username
password = dbContainer.password
}
}
afterEvaluate {
// For jOOQ to run we always need for flyway to be completed before.
jooqCodegen.dependsOn flywayMigrate
jooqCodegen {
doFirst {
def dbContainer = dbContainerProvider.get().container
jooq {
configuration {
jdbc {
driver = "org.postgresql.Driver"
url = dbContainer.jdbcUrl
user = dbContainer.username
password = dbContainer.password
}
}
}
}
}
}
jooq {
// jOOQ generation config
configuration {
logging = org.jooq.meta.jaxb.Logging.WARN
generator {
name = "org.jooq.codegen.DefaultGenerator"
database {
name = "org.jooq.meta.postgres.PostgresDatabase"
includes = ".*"
excludes = 'flyway_schema_history'
inputSchema = 'public'
}
target {
packageName = 'com.testcontainers.demo.jooq'
directory = 'target/generated-sources/jooq'
}
}
}
}
/**
* Build service for providing database container.
*/
abstract class PostgresService implements BuildService<BuildServiceParameters.None>, AutoCloseable {
private final PostgreSQLContainer container;
PostgresService() {
// Services are initialized lazily, on first request to them, so we start container immediately.
container = new PostgreSQLContainer("postgres:16-alpine")
container.start()
}
@Override
void close() {
// Ensure to stop container in the end
container.stop()
}
PostgreSQLContainer getContainer() {
return container
}
}