-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'steklopod-Spring_Boot_3'
- Loading branch information
Showing
9 changed files
with
162 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
name: Test CI | ||
on: [ push, fork ] | ||
|
||
jobs: | ||
TEST_ALL: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
java: [ '11', '17' ] | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- uses: actions/cache@v3 | ||
with: | ||
path: | | ||
~/.gradle/caches | ||
~/.gradle/wrapper | ||
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | ||
restore-keys: | | ||
${{ runner.os }}-gradle- | ||
- name: 🪜 Setup java ${{ matrix.java }} | ||
uses: actions/setup-java@v3 | ||
with: | ||
java-version: ${{ matrix.java }} | ||
distribution: temurin | ||
|
||
- name: 🦞 chmod /gradlew | ||
run: chmod +x ./gradlew | ||
|
||
- name: 🔦 Test | ||
run: ./gradlew test --info |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<component name="ProjectRunConfigurationManager"> | ||
<configuration default="false" name="TEST" type="JUnit" factoryName="JUnit"> | ||
<module name="springdoc-openapi-gradle-plugin.test" /> | ||
<shortenClasspath name="ARGS_FILE" /> | ||
<useClassPathOnly /> | ||
<extension name="coverage"> | ||
<pattern> | ||
<option name="PATTERN" value="org.springdoc.openapi.gradle.plugin.*" /> | ||
<option name="ENABLED" value="true" /> | ||
</pattern> | ||
</extension> | ||
<extension name="net.ashald.envfile"> | ||
<option name="IS_ENABLED" value="false" /> | ||
<option name="IS_SUBST" value="false" /> | ||
<option name="IS_PATH_MACRO_SUPPORTED" value="false" /> | ||
<option name="IS_IGNORE_MISSING_FILES" value="false" /> | ||
<option name="IS_ENABLE_EXPERIMENTAL_INTEGRATIONS" value="false" /> | ||
<ENTRIES> | ||
<ENTRY IS_ENABLED="true" PARSER="runconfig" /> | ||
</ENTRIES> | ||
</extension> | ||
<option name="ALTERNATIVE_JRE_PATH" value="corretto-19" /> | ||
<option name="PACKAGE_NAME" value="org.springdoc.openapi.gradle.plugin" /> | ||
<option name="MAIN_CLASS_NAME" value="org.springdoc.openapi.gradle.plugin.OpenApiGradlePluginTest" /> | ||
<option name="METHOD_NAME" value="" /> | ||
<option name="TEST_OBJECT" value="class" /> | ||
<option name="PARAMETERS" value="" /> | ||
<option name="WORKING_DIRECTORY" value="%MODULE_WORKING_DIR%" /> | ||
<method v="2"> | ||
<option name="Make" enabled="true" /> | ||
</method> | ||
</configuration> | ||
</component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ plugins { | |
} | ||
|
||
group = "org.springdoc" | ||
version = "1.5.0" | ||
version = "1.6.0" | ||
|
||
sonarqube { | ||
properties { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
rootProject.name = "springdoc-openapi-gradle-plugin" | ||
|
||
pluginManagement { | ||
repositories { | ||
mavenCentral() | ||
gradlePluginPortal() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 76 additions & 65 deletions
141
src/main/kotlin/org/springdoc/openapi/gradle/plugin/OpenApiGradlePlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,95 @@ | ||
@file:Suppress("unused") | ||
|
||
package org.springdoc.openapi.gradle.plugin | ||
|
||
import com.github.psxpaul.task.JavaExecFork | ||
import org.gradle.api.Plugin | ||
import org.gradle.api.Project | ||
import org.gradle.api.Task | ||
import org.gradle.api.logging.Logging | ||
import org.gradle.api.tasks.TaskProvider | ||
import org.gradle.internal.jvm.Jvm | ||
import org.springframework.boot.gradle.tasks.run.BootRun | ||
|
||
open class OpenApiGradlePlugin : Plugin<Project> { | ||
private val logger = Logging.getLogger(OpenApiGradlePlugin::class.java) | ||
private val logger = Logging.getLogger(OpenApiGradlePlugin::class.java) | ||
|
||
override fun apply(project: Project) { | ||
with(project) { | ||
// Run time dependency on the following plugins | ||
plugins.apply(SPRING_BOOT_PLUGIN) | ||
plugins.apply(EXEC_FORK_PLUGIN) | ||
|
||
extensions.create(EXTENSION_NAME, OpenApiExtension::class.java, this) | ||
|
||
afterEvaluate { generate(this) } | ||
} | ||
} | ||
|
||
override fun apply(project: Project) { | ||
// Run time dependency on the following plugins | ||
project.plugins.apply(SPRING_BOOT_PLUGIN) | ||
project.plugins.apply(EXEC_FORK_PLUGIN) | ||
private fun generate(project: Project) = project.run { | ||
springBoot3CompatibilityCheck() | ||
|
||
project.extensions.create(EXTENSION_NAME, OpenApiExtension::class.java, project) | ||
// The task, used to run the Spring Boot application (`bootRun`) | ||
val bootRunTask = tasks.named(SPRING_BOOT_RUN_TASK_NAME) | ||
// The task, used to resolve the application's main class (`bootRunMainClassName`) | ||
val bootRunMainClassNameTask = tasks.find { it.name == SPRING_BOOT_RUN_MAIN_CLASS_NAME_TASK_NAME} | ||
?:tasks.named(SPRING_BOOT_3_RUN_MAIN_CLASS_NAME_TASK_NAME) | ||
|
||
project.afterEvaluate { | ||
// The task, used to run the Spring Boot application (`bootRun`) | ||
val bootRunTask = project.tasks.named(SPRING_BOOT_RUN_TASK_NAME) | ||
// The task, used to resolve the application's main class (`bootRunMainClassName`) | ||
val bootRunMainClassNameTask = | ||
project.tasks.named(SPRING_BOOT_RUN_MAIN_CLASS_NAME_TASK_NAME) | ||
val extension = extensions.findByName(EXTENSION_NAME) as OpenApiExtension | ||
val customBootRun = extension.customBootRun | ||
// Create a forked version spring boot run task | ||
val forkedSpringBoot = tasks.register(FORKED_SPRING_BOOT_RUN_TASK_NAME, JavaExecFork::class.java) { fork -> | ||
fork.dependsOn(bootRunMainClassNameTask) | ||
fork.onlyIf { needToFork(bootRunTask, customBootRun, fork) } | ||
} | ||
|
||
val extension = project.extensions.findByName(EXTENSION_NAME) as OpenApiExtension | ||
val customBootRun = extension.customBootRun | ||
// Create a forked version spring boot run task | ||
val forkedSpringBoot = | ||
project.tasks.register( | ||
FORKED_SPRING_BOOT_RUN_TASK_NAME, | ||
JavaExecFork::class.java | ||
) { fork -> | ||
fork.dependsOn(bootRunMainClassNameTask) | ||
// This is my task. Before I can run it, I have to run the dependent tasks | ||
tasks.register(OPEN_API_TASK_NAME, OpenApiGeneratorTask::class.java) { | ||
it.dependsOn(forkedSpringBoot) | ||
} | ||
|
||
fork.onlyIf { | ||
val bootRun = bootRunTask.get() as BootRun | ||
// The forked task need to be terminated as soon as my task is finished | ||
forkedSpringBoot.get().stopAfter = tasks.named(OPEN_API_TASK_NAME) | ||
} | ||
|
||
val baseSystemProperties = customBootRun.systemProperties.orNull?.takeIf { it.isNotEmpty() } | ||
?: bootRun.systemProperties | ||
// copy all system properties, excluding those starting with `java.class.path` | ||
fork.systemProperties = baseSystemProperties.filter { | ||
!it.key.startsWith( | ||
CLASS_PATH_PROPERTY_NAME | ||
) | ||
} | ||
private fun Project.springBoot3CompatibilityCheck() { | ||
val tasksNames = tasks.names | ||
val boot2TaskName = "bootRunMainClassName" | ||
val boot3TaskName = "resolveMainClassName" | ||
if (!tasksNames.contains(boot2TaskName) && tasksNames.contains(boot3TaskName)) | ||
tasks.register(boot2TaskName) { it.dependsOn(tasks.named(boot3TaskName)) } | ||
} | ||
|
||
// use original bootRun parameter if the list-type customBootRun properties is empty | ||
fork.workingDir = customBootRun.workingDir.asFile.orNull | ||
?: bootRun.workingDir | ||
fork.args = customBootRun.args.orNull?.takeIf { it.isNotEmpty() }?.toMutableList() | ||
?: bootRun.args?.toMutableList() ?: mutableListOf() | ||
fork.classpath = customBootRun.classpath.takeIf { !it.isEmpty } | ||
?: bootRun.classpath | ||
fork.main = customBootRun.mainClass.orNull | ||
?: bootRun.mainClass.get() | ||
fork.jvmArgs = customBootRun.jvmArgs.orNull?.takeIf { it.isNotEmpty() } | ||
?: bootRun.jvmArgs | ||
fork.environment = customBootRun.environment.orNull?.takeIf { it.isNotEmpty() } | ||
?: bootRun.environment | ||
if (org.gradle.internal.jvm.Jvm.current().toString() | ||
.startsWith("1.8") | ||
) { | ||
fork.killDescendants = false | ||
} | ||
true | ||
} | ||
} | ||
private fun needToFork( | ||
bootRunTask: TaskProvider<Task>, | ||
customBootRun: CustomBootRunAction, | ||
fork: JavaExecFork | ||
): Boolean { | ||
val bootRun = bootRunTask.get() as BootRun | ||
|
||
// This is my task. Before I can run it I have to run the dependent tasks | ||
project.tasks.register( | ||
OPEN_API_TASK_NAME, | ||
OpenApiGeneratorTask::class.java | ||
) { openApiGenTask -> | ||
openApiGenTask.dependsOn(forkedSpringBoot) | ||
} | ||
val baseSystemProperties = customBootRun.systemProperties.orNull?.takeIf { it.isNotEmpty() } | ||
?: bootRun.systemProperties | ||
with(fork) { | ||
// copy all system properties, excluding those starting with `java.class.path` | ||
systemProperties = baseSystemProperties.filter { | ||
!it.key.startsWith(CLASS_PATH_PROPERTY_NAME) | ||
} | ||
|
||
// The forked task need to be terminated as soon as my task is finished | ||
forkedSpringBoot.get().stopAfter = project.tasks.named(OPEN_API_TASK_NAME) | ||
} | ||
} | ||
// use original bootRun parameter if the list-type customBootRun properties are empty | ||
workingDir = customBootRun.workingDir.asFile.orNull | ||
?: bootRun.workingDir | ||
args = customBootRun.args.orNull?.takeIf { it.isNotEmpty() }?.toMutableList() | ||
?: bootRun.args?.toMutableList() ?: mutableListOf() | ||
classpath = customBootRun.classpath.takeIf { !it.isEmpty } | ||
?: bootRun.classpath | ||
main = customBootRun.mainClass.orNull | ||
?: bootRun.mainClass.get() | ||
jvmArgs = customBootRun.jvmArgs.orNull?.takeIf { it.isNotEmpty() } | ||
?: bootRun.jvmArgs | ||
environment = customBootRun.environment.orNull?.takeIf { it.isNotEmpty() } | ||
?: bootRun.environment | ||
if (Jvm.current().toString().startsWith("1.8")) { | ||
killDescendants = false | ||
} | ||
} | ||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 6 additions & 5 deletions
11
src/test/resources/acceptance-project/src/main/java/com/example/demo/DemoApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters