-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.gradle
More file actions
115 lines (99 loc) · 3.26 KB
/
Copy pathbuild.gradle
File metadata and controls
115 lines (99 loc) · 3.26 KB
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
plugins {
id 'java'
id 'org.springframework.boot' version '4.0.3'
id 'io.spring.dependency-management' version '1.1.7'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
description = 'Learning repo for Java, Spring Boot, and Gradle'
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-webmvc'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:3.0.0'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.boot:spring-boot-starter-webmvc-test'
testImplementation 'org.springframework.security:spring-security-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
tasks.named('test') {
useJUnitPlatform()
}
tasks.register('exportOpenApiYaml') {
group = 'documentation'
description = 'Starts the app temporarily and exports the generated OpenAPI YAML to openapi.yaml in the project root.'
dependsOn tasks.named('bootJar')
doLast {
def outputFile = layout.projectDirectory.file('openapi.yaml').asFile
def jarFile = tasks.named('bootJar').get().archiveFile.get().asFile
def port = 9091
def processOutput = new StringBuffer()
def process = new ProcessBuilder(
'java',
'-jar',
jarFile.absolutePath,
"--server.port=${port}",
'--spring.main.banner-mode=off',
'--app.courses.seed.enabled=false'
)
.directory(project.projectDir)
.redirectErrorStream(true)
.start()
def processLogThread = Thread.startDaemon('openapi-export-log') {
try {
process.inputStream.withReader('UTF-8') { reader ->
char[] buffer = new char[1024]
int read
while ((read = reader.read(buffer)) != -1) {
processOutput.append(buffer, 0, read)
}
}
} catch (IOException ignored) {
// The process stream closes during shutdown.
}
}
try {
def yamlUrl = new URL("http://127.0.0.1:${port}/v3/api-docs.yaml")
def deadline = System.currentTimeMillis() + 30000
def exported = false
while (System.currentTimeMillis() < deadline) {
if (!process.isAlive()) {
throw new GradleException("Application exited before OpenAPI YAML export completed.\n${processOutput}")
}
try {
def connection = yamlUrl.openConnection()
connection.connectTimeout = 1000
connection.readTimeout = 1000
connection.setRequestProperty('Accept', 'application/vnd.oai.openapi')
connection.inputStream.withCloseable { stream ->
outputFile.setText(stream.getText('UTF-8'), 'UTF-8')
}
exported = true
break
} catch (IOException ignored) {
sleep(500)
}
}
if (!exported) {
throw new GradleException("Timed out waiting for ${yamlUrl}.\n${processOutput}")
}
logger.lifecycle("OpenAPI YAML exported to ${outputFile}")
} finally {
process.destroy()
if (!process.waitFor(5, java.util.concurrent.TimeUnit.SECONDS)) {
process.destroyForcibly()
}
processLogThread.join(1000)
}
}
}