I have a spring boot app.
Using the org.springdoc.openapi-gradle-plugin
I am able to “build” an openapi.json
file.
The org.openapi.generator
plugin is then able to generate code with the openapi.json
file as input.
My goal is to move the following code into a plugin. Currently the sourceSet is hardcoded to integrationTest
. Ideally adding a dependency to something would be enough (integrationTestImplementation("somethingProvidedByThePlugin"
)
The current code works, but I have the impression that it could be improved a lot → constructive criticism is greatly appreciated.
// This is where the openapiSpec should be generated.
val openApiSpecDir = layout.buildDirectory
val openApiSpecName = "openapi.json"
afterEvaluate {
// The generateOpenApiDocs task is created in the plugin in an `afterEvaluate` block.
val openApiGeneratorTask by tasks.named<OpenApiGeneratorTask>("generateOpenApiDocs") {
outputDir.set(openApiSpecDir)
outputFileName.set(openApiSpecName)
doFirst {
delete(files(outputDir))
}
}
}
openApiGenerate {
generatorName.set("java")
inputSpec.set(openApiSpecDir.file(openApiSpecName).map(RegularFile::toString))
outputDir.set(layout.buildDirectory.dir("openApi").map(Directory::toString))
}
// The openApiGenerateTask creates a complete gradle project. We are only interested in the source code
val openApiGenerateTask = tasks.named<org.openapitools.generator.gradle.plugin.tasks.GenerateTask>("openApiGenerate")
val openApiSrcTask by tasks.registering(Sync::class) {
from(openApiGenerateTask)
include("src/main/java/**")
eachFile { relativePath = RelativePath(true, *relativePath.segments.drop(3).toTypedArray()) }
includeEmptyDirs = false
into(layout.buildDirectory.dir("swagger-code-gen-java"))
}
sourceSets["integrationTest"].java.srcDir(files(openApiSrcTask))
and
dependencies {
// ...
integrationTestImplementation(libs.bundles.openapi.codegen)
}