-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle
162 lines (126 loc) · 5.12 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
161
162
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "org.flywaydb:flyway-database-postgresql:10.20.1" // Align with version managed by Spring Boot!
}
}
plugins {
id "org.springframework.boot" version "3.4.0"
id "io.spring.dependency-management" version "1.1.6"
id "com.bmuschko.docker-remote-api" version "9.4.0"
id "org.flywaydb.flyway" version "10.20.1" // Align with version managed by Spring Boot!
id "checkstyle"
id "java"
}
group = "com.example"
version = "0.0.1-SNAPSHOT"
repositories {
mavenCentral()
maven { url "https://repo.spring.io/milestone" }
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}
dependencies {
implementation "org.springframework.boot:spring-boot-starter-jdbc"
implementation "org.flywaydb:flyway-database-postgresql"
runtimeOnly "org.postgresql:postgresql"
testImplementation "org.springframework.boot:spring-boot-starter-test"
testImplementation "org.testcontainers:junit-jupiter"
testImplementation "org.testcontainers:postgresql"
}
tasks.withType(Test).configureEach {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
// Use at most 80% of the available CPU cores (but no less than 1) for running tests. The remaining cores can then
// be used by Docker and other processes.
maxParallelForks = Math.floor(Runtime.runtime.availableProcessors() * 0.8) ?: 1
}
// The custom Gradle tasks apply all Flyway migrations on an empty database. Then, they create a database dump that can
// be used to re-initialize the test database before every test. If you do not need that functionality, remove all
// remaining lines.
import com.bmuschko.gradle.docker.tasks.container.DockerCopyFileFromContainer
import com.bmuschko.gradle.docker.tasks.container.DockerCreateContainer
import com.bmuschko.gradle.docker.tasks.container.DockerExecContainer
import com.bmuschko.gradle.docker.tasks.container.DockerStartContainer
import com.bmuschko.gradle.docker.tasks.container.DockerStopContainer
import com.bmuschko.gradle.docker.tasks.image.DockerPullImage
import org.flywaydb.gradle.task.FlywayMigrateTask
import java.nio.file.Files
import java.nio.file.StandardOpenOption
// Path where the generated database dump will be saved. Committing it to source control is another option.
def dumpPath = project.layout.buildDirectory.asFile.get().toPath().resolve("resources/test/db.sql")
tasks.register("pullDatabaseImage", DockerPullImage) {
def stateFile = project.layout.buildDirectory.asFile.get().toPath().resolve("tmp/squash.txt")
// Declaring inputs and outputs make this task participate in incremental builds and build caching. By declaring the
// Flyway migration scripts as inputs the task will only run if something has changed that potentially affects the
// generated database dump.
inputs.files(fileTree("src/main/resources/db/migration"))
.withPropertyName("migrations")
.withPathSensitivity(PathSensitivity.RELATIVE)
outputs.file(stateFile)
.withPropertyName("stateFile")
image = "docker.io/library/postgres:17.2"
doLast {
Files.writeString(stateFile, getImage().get(), StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING)
}
}
tasks.register("createDatabaseContainer", DockerCreateContainer) {
dependsOn pullDatabaseImage
// onlyIf hackery is needed (as of v9.0.0) to make all Docker plug-in tasks play well with incremental builds.
onlyIf { pullDatabaseImage.didWork }
targetImageId pullDatabaseImage.getImage()
hostConfig.portBindings = ["5432:5432"]
hostConfig.autoRemove = true
withEnvVar("POSTGRES_USER", "flyway")
withEnvVar("POSTGRES_PASSWORD", "flyway")
withEnvVar("POSTGRES_DB", "squash")
}
tasks.register("startDatabaseContainer", DockerStartContainer) {
dependsOn createDatabaseContainer
onlyIf { createDatabaseContainer.didWork }
targetContainerId createDatabaseContainer.getContainerId()
}
tasks.register("stopDatabaseContainer", DockerStopContainer) {
dependsOn startDatabaseContainer
onlyIf { startDatabaseContainer.didWork }
targetContainerId startDatabaseContainer.getContainerId()
}
tasks.register("runFlywayMigrations", FlywayMigrateTask) {
dependsOn startDatabaseContainer
onlyIf { startDatabaseContainer.didWork }
driver = "org.postgresql.Driver"
url = "jdbc:postgresql://localhost:5432/squash"
user = "flyway"
password = "flyway"
connectRetries = 5
connectRetriesInterval=120
}
tasks.register("dumpDatabase", DockerExecContainer) {
dependsOn runFlywayMigrations
onlyIf { runFlywayMigrations.didWork }
targetContainerId startDatabaseContainer.getContainerId()
commands.add(["pg_dump", "--clean", "--if-exists", "--inserts",
"--disable-dollar-quoting", "--no-owner", "-d", "squash", "-U", "flyway",
"--file", "/tmp/db.sql"] as String[])
}
tasks.register("copyDatabaseDumpToHost", DockerCopyFileFromContainer) {
dependsOn dumpDatabase
finalizedBy stopDatabaseContainer
onlyIf { dumpDatabase.didWork }
outputs.file(dumpPath)
targetContainerId startDatabaseContainer.getContainerId()
remotePath = "/tmp/db.sql"
hostPath = dumpPath.toString()
}
// Ensure that a database dump is present before tests are run.
tasks.withType(ProcessResources).configureEach {
dependsOn copyDatabaseDumpToHost
}