Skip to content

Commit d37f32f

Browse files
committed
Merge branch 'litols-customBootRun'
2 parents 610b0e1 + a0f7a99 commit d37f32f

File tree

4 files changed

+103
-30
lines changed

4 files changed

+103
-30
lines changed

README.md

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,29 +71,36 @@ openApi {
7171
outputDir.set(file("$buildDir/docs"))
7272
outputFileName.set("swagger.json")
7373
waitTimeInSeconds.set(10)
74-
forkProperties.set("-Dspring.profiles.active=special")
7574
groupedApiMappings.set(["https://localhost:8080/v3/api-docs/groupA" to "swagger-groupA.json",
7675
"https://localhost:8080/v3/api-docs/groupB" to "swagger-groupB.json"])
76+
customBootRun {
77+
args.set(["--spring.profiles.active=special"])
78+
}
7779
}
7880
```
7981

80-
Parameter | Description | Required | Default
81-
--------- | ----------- | -------- | -------
82-
`apiDocsUrl` | The URL from where the OpenAPI doc can be downloaded | No | http://localhost:8080/v3/api-docs
83-
`outputDir` | The output directory for the generated OpenAPI file | No | $buildDir - Your project's build dir
84-
`outputFileName` | The name of the output file with extension | No | openapi.json
85-
`waitTimeInSeconds` | Time to wait in seconds for your Spring Boot application to start, before we make calls to `apiDocsUrl` to download the OpenAPI doc | No | 30 seconds
86-
`forkProperties` | Any system property that you would normal need to start your spring boot application. Can either be a static string or a java Properties object | No | ""
87-
`groupedApiMappings` | A map of URLs (from where the OpenAPI docs can be downloaded) to output file names | No | []
88-
89-
### Fork properties examples
90-
Fork properties allows you to send in anything that might be necessary to allow for the forked spring boot application that gets started
91-
to be able to start (profiles, other custom properties, etc etc)
92-
93-
#### Static string
82+
| Parameter | Description | Required | Default |
83+
|----------------------|-------------------------------------------------------------------------------------------------------------------------------------|----------|--------------------------------------|
84+
| `apiDocsUrl` | The URL from where the OpenAPI doc can be downloaded | No | http://localhost:8080/v3/api-docs |
85+
| `outputDir` | The output directory for the generated OpenAPI file | No | $buildDir - Your project's build dir |
86+
| `outputFileName` | The name of the output file with extension | No | openapi.json |
87+
| `waitTimeInSeconds` | Time to wait in seconds for your Spring Boot application to start, before we make calls to `apiDocsUrl` to download the OpenAPI doc | No | 30 seconds |
88+
| `groupedApiMappings` | A map of URLs (from where the OpenAPI docs can be downloaded) to output file names | No | [] |
89+
| `customBootRun` | Any bootRun property that you would normal need to start your spring boot application. | No | (N/A) |
90+
91+
### `customBootRun` properties examples
92+
`customBootRun` allows you to send in the properties that might be necessary to allow for the forked spring boot application that gets started
93+
to be able to start (profiles, other custom properties, etc.)
94+
`customBootRun` allows you can specify bootRun style parameter, such as `args`, `jvmArgs`, `systemProperties` and `workingDir`.
95+
If you don't specify `customBootRun` parameter, this plugin uses the parameter specified to `bootRun` in Spring Boot Gradle Plugin.
96+
97+
#### Passing static args
98+
This allows for you to be able to just send the static properties when executing Spring application in `generateOpenApiDocs`.
9499
```
95100
openApi {
96-
forkProperties = "-Dspring.profiles.active=special -DstringPassedInForkProperites=true"
101+
customBootRun {
102+
args = ["--spring.profiles.active=special"]
103+
}
97104
}
98105
```
99106

@@ -105,7 +112,9 @@ This allows for you to be able to just send in whatever you need when you genera
105112
and as long as the config looks as follows that value will be passed into the forked spring boot application.
106113
```
107114
openApi {
108-
forkProperties = System.properties
115+
customBootRun {
116+
systemProperties = System.properties
117+
}
109118
}
110119
```
111120

src/main/kotlin/org/springdoc/openapi/gradle/plugin/OpenApiExtension.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package org.springdoc.openapi.gradle.plugin
22

3+
import org.gradle.api.Action
34
import org.gradle.api.Project
5+
import org.gradle.api.file.ConfigurableFileCollection
46
import org.gradle.api.file.DirectoryProperty
7+
import org.gradle.api.file.RegularFileProperty
8+
import org.gradle.api.provider.ListProperty
59
import org.gradle.api.provider.MapProperty
610
import org.gradle.api.provider.Property
711
import javax.inject.Inject
@@ -12,4 +16,20 @@ open class OpenApiExtension @Inject constructor(project: Project) {
1216
val outputDir: DirectoryProperty = project.objects.directoryProperty()
1317
val waitTimeInSeconds: Property<Int> = project.objects.property(Int::class.java)
1418
val groupedApiMappings: MapProperty<String, String> = project.objects.mapProperty(String::class.java, String::class.java)
19+
val customBootRun: CustomBootRunAction = project.objects.newInstance(CustomBootRunAction::class.java, project)
20+
fun customBootRun(action: Action<CustomBootRunAction>) {
21+
action.execute(customBootRun)
22+
}
23+
}
24+
25+
open class CustomBootRunAction @Inject constructor(
26+
project: Project,
27+
) {
28+
val systemProperties: MapProperty<String, Any> = project.objects.mapProperty(String::class.java, Any::class.java)
29+
val workingDir: RegularFileProperty = project.objects.fileProperty()
30+
val mainClass: Property<String> = project.objects.property(String::class.java)
31+
val args: ListProperty<String> = project.objects.listProperty(String::class.java)
32+
val classpath: ConfigurableFileCollection = project.objects.fileCollection()
33+
val jvmArgs: ListProperty<String> = project.objects.listProperty(String::class.java)
34+
val environment: MapProperty<String, Any> = project.objects.mapProperty(String::class.java, Any::class.java)
1535
}

src/main/kotlin/org/springdoc/openapi/gradle/plugin/OpenApiGradlePlugin.kt

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ open class OpenApiGradlePlugin : Plugin<Project> {
2525
val bootRunMainClassNameTask =
2626
project.tasks.named(SPRING_BOOT_RUN_MAIN_CLASS_NAME_TASK_NAME)
2727

28+
val extension = project.extensions.findByName(EXTENSION_NAME) as OpenApiExtension
29+
val customBootRun = extension.customBootRun
2830
// Create a forked version spring boot run task
2931
val forkedSpringBoot =
3032
project.tasks.register(
@@ -36,20 +38,28 @@ open class OpenApiGradlePlugin : Plugin<Project> {
3638
fork.onlyIf {
3739
val bootRun = bootRunTask.get() as BootRun
3840

41+
val baseSystemProperties = customBootRun.systemProperties.orNull?.takeIf { it.isNotEmpty() }
42+
?: bootRun.systemProperties
3943
// copy all system properties, excluding those starting with `java.class.path`
40-
fork.systemProperties =
41-
bootRun.systemProperties.filter {
42-
!it.key.startsWith(
43-
CLASS_PATH_PROPERTY_NAME
44-
)
45-
}
44+
fork.systemProperties = baseSystemProperties.filter {
45+
!it.key.startsWith(
46+
CLASS_PATH_PROPERTY_NAME
47+
)
48+
}
4649

47-
fork.workingDir = bootRun.workingDir
48-
fork.args = bootRun.args?.toMutableList() ?: mutableListOf()
49-
fork.classpath = bootRun.classpath
50-
fork.main = bootRun.mainClass.get()
51-
fork.jvmArgs = bootRun.jvmArgs
52-
fork.environment = bootRun.environment
50+
// use original bootRun parameter if the list-type customBootRun properties is empty
51+
fork.workingDir = customBootRun.workingDir.asFile.orNull
52+
?: bootRun.workingDir
53+
fork.args = customBootRun.args.orNull?.takeIf { it.isNotEmpty() }?.toMutableList()
54+
?: bootRun.args?.toMutableList() ?: mutableListOf()
55+
fork.classpath = customBootRun.classpath.takeIf { !it.isEmpty }
56+
?: bootRun.classpath
57+
fork.main = customBootRun.mainClass.orNull
58+
?: bootRun.mainClass.get()
59+
fork.jvmArgs = customBootRun.jvmArgs.orNull?.takeIf { it.isNotEmpty() }
60+
?: bootRun.jvmArgs
61+
fork.environment = customBootRun.environment.orNull?.takeIf { it.isNotEmpty() }
62+
?: bootRun.environment
5363
if (org.gradle.internal.jvm.Jvm.current().toString()
5464
.startsWith("1.8")
5565
) {

src/test/kotlin/org/springdoc/openapi/gradle/plugin/OpenApiGradlePluginTest.kt

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package org.springdoc.openapi.gradle.plugin
33
import com.beust.klaxon.JsonObject
44
import com.beust.klaxon.Parser
55
import com.fasterxml.jackson.databind.ObjectMapper
6-
import com.fasterxml.jackson.databind.node.TextNode
76
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
87
import com.fasterxml.jackson.module.kotlin.KotlinModule
98
import org.gradle.internal.impldep.org.apache.commons.lang.RandomStringUtils
@@ -122,6 +121,23 @@ class OpenApiGradlePluginTest {
122121
assertOpenApiJsonFile(2)
123122
}
124123

124+
@Test
125+
fun `using forked properties via System properties with customBootRun`() {
126+
buildFile.writeText(
127+
"""$baseBuildGradle
128+
openApi {
129+
customBootRun {
130+
systemProperties = System.properties
131+
}
132+
}
133+
""".trimMargin()
134+
)
135+
136+
assertEquals(TaskOutcome.SUCCESS, openApiDocsTask(runTheBuild("-Dspring.profiles.active=multiple-endpoints")).outcome)
137+
assertOpenApiJsonFile(2)
138+
}
139+
140+
125141
@Test
126142
fun `configurable wait time`() {
127143
buildFile.writeText(
@@ -156,6 +172,24 @@ class OpenApiGradlePluginTest {
156172
assertOpenApiJsonFile(1)
157173
}
158174

175+
@Test
176+
fun `using different api url via customBootRun`() {
177+
buildFile.writeText(
178+
"""$baseBuildGradle
179+
openApi{
180+
apiDocsUrl = "http://localhost:8080/secret-api-docs"
181+
customBootRun {
182+
args = ["--spring.profiles.active=different-url"]
183+
}
184+
}
185+
""".trimMargin()
186+
)
187+
188+
assertEquals(TaskOutcome.SUCCESS, openApiDocsTask(runTheBuild()).outcome)
189+
assertOpenApiJsonFile(1)
190+
}
191+
192+
159193
@Test
160194
fun `yaml generation`() {
161195
val outputYamlFileName = "openapi.yaml"

0 commit comments

Comments
 (0)