Skip to content

Commit

Permalink
Merge branch 'steklopod-Spring_Boot_3'
Browse files Browse the repository at this point in the history
  • Loading branch information
bnasslahsen committed Dec 14, 2022
2 parents 2c48609 + f9a8622 commit 57097cb
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 77 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/master.yml
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
33 changes: 33 additions & 0 deletions .run/TEST.run.xml
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>
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ Gradle Groovy DSL
```groovy
plugins {
id "org.springframework.boot" version "2.7.0"
id "org.springdoc.openapi-gradle-plugin" version "1.5.0"
id "org.springdoc.openapi-gradle-plugin" version "1.6.0"
}
```

Gradle Kotlin DSL
```groovy
plugins {
id("org.springframework.boot") version "2.7.0"
id("org.springdoc.openapi-gradle-plugin") version "1.5.0"
id("org.springdoc.openapi-gradle-plugin") version "1.6.0"
}
```

Expand Down Expand Up @@ -133,7 +133,7 @@ The `groupedApiMappings` customization allows you to specify multiple URLs/file
2. Update the version for the plugin to match the current version found in `build.gradle.kts`

```
id("org.springdoc.openapi-gradle-plugin") version "1.5.0"
id("org.springdoc.openapi-gradle-plugin") version "1.6.0"
```
3. Add the following to the spring boot apps `settings.gradle`
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "org.springdoc"
version = "1.5.0"
version = "1.6.0"

sonarqube {
properties {
Expand Down
6 changes: 6 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
rootProject.name = "springdoc-openapi-gradle-plugin"

pluginManagement {
repositories {
mavenCentral()
gradlePluginPortal()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const val OPEN_API_TASK_NAME = "generateOpenApiDocs"
const val OPEN_API_TASK_DESCRIPTION = "Generates the spring doc openapi file"
const val SPRING_BOOT_RUN_TASK_NAME = "bootRun"
const val SPRING_BOOT_RUN_MAIN_CLASS_NAME_TASK_NAME = "bootRunMainClassName"
const val SPRING_BOOT_3_RUN_MAIN_CLASS_NAME_TASK_NAME = "bootRun"
const val FORKED_SPRING_BOOT_RUN_TASK_NAME = "forkedSpringBootRun"

const val DEFAULT_API_DOCS_URL = "http://localhost:8080/v3/api-docs"
Expand Down
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class OpenApiGradlePluginTest {

private val baseBuildGradle = """plugins {
id 'java'
id 'org.springframework.boot' version '2.4.5'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'org.springframework.boot' version '2.7.6'
id 'io.spring.dependency-management' version '1.1.15.RELEASE'
id 'org.springdoc.openapi-gradle-plugin'
}
Expand All @@ -43,7 +43,7 @@ class OpenApiGradlePluginTest {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springdoc:springdoc-openapi-webmvc-core:1.6.12'
implementation 'org.springdoc:springdoc-openapi-webmvc-core:1.6.13'
}
""".trimIndent()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
package com.example.demo;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.PostConstruct;
import java.time.Duration;

@SpringBootApplication
public class DemoApplication {
public class DemoApplication implements ApplicationRunner{

@Value("${slower:false}")
boolean slower;

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

@PostConstruct
public void afterBeanStuff() throws InterruptedException {
@Override
public void run(ApplicationArguments arg0) throws Exception {
System.out.println("Hello World from Application Runner");
if (slower) {
Duration waitTime = Duration.ofSeconds(40);
System.out.println("Waiting for " + waitTime + " before starting");
Expand Down

0 comments on commit 57097cb

Please sign in to comment.