I have a java spring backend and a js-react frontend.
The openapi-gradle-plugin starts the compiled spring application and generates an openapi document (OpenApiGeneratorTask
)
The org.openapi.generator-plugin can generate a js library from this openapi document.
As described in the docs I’ve created a configuration and added an artifact in the backend project:
val openApi: Configuration by configurations.creating {
isCanBeConsumed = true
isCanBeResolved = false
}
afterEvaluate {
val openApiGeneratorTask by tasks.named<OpenApiGeneratorTask>("generateOpenApiDocs")
artifacts {
// outputDir and outputFileName are properties
add("openApi", openApiGeneratorTask.outputDir.file(openApiGeneratorTask.outputFileName)) {
builtBy(openApiGeneratorTask)
}
}
}
The frontend consumes it using the following code:
openApiGenerate {
inputSpec.set(provider { openApiConf.singleFile.path })
}
val openApiConf by configurations.creating {
isCanBeConsumed = false
isCanBeResolved = true
}
dependencies {
openApiConf(project(mapOf(
"path" to ":backend",
"configuration" to "openApi")))
}
This only works, if I first build the backend project manually. Otherwise I get the following error:
A problem was found with the configuration of task ':frontend:openApiGenerate' (type 'GenerateTask').
- In plugin 'org.openapi.generator' type 'org.openapitools.generator.gradle.plugin.tasks.GenerateTask' property 'inputSpec' specifies file 'D:\tmp\2021-12\gradle\proj\backend\build\openapi.json' which doesn't exist.
Maybe related is another problem / warning I get:
Execution optimizations have been disabled for task ':backend:jar' to ensure correctness due to the following reasons:
- Gradle detected a problem with the following location: 'D:\tmp\2021-12\gradle\proj\backend\build\classes\java\main'. Reason: Task ':backend:jar' uses this output of task ':backend:generateOpenApiDocs' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. Please refer to https://docs.gradle.org/7.2/userguide/validation_problems.html#implicit_dependency for more details about this problem.
- Gradle detected a problem with the following location: 'D:\tmp\2021-12\gradle\proj\backend\build\libs\backend-plain.jar'. Reason: Task ':backend:forkedSpringBootRun' uses this output of task ':backend:jar' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. Please refer to https://docs.gradle.org/7.2/userguide/validation_problems.html#implicit_dependency for more details about this problem.
- Gradle detected a problem with the following location: 'D:\tmp\2021-12\gradle\proj\backend\build\resources\main'. Reason: Task ':backend:jar' uses this output of task ':backend:generateOpenApiDocs' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. Please refer to https://docs.gradle.org/7.2/userguide/validation_problems.html#implicit_dependency for more details about this problem.
- Gradle detected a problem with the following location: 'D:\tmp\2021-12\gradle\proj\backend\build\tmp\jar\MANIFEST.MF'. Reason: Task ':backend:jar' uses this output of task ':backend:generateOpenApiDocs' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. Please refer to https://docs.gradle.org/7.2/userguide/validation_problems.html#implicit_dependency for more details about this problem.
I can of course manually set a dependencies between the tasks, but don’t understand why this is necessary.
Explicitly calling the generateOpenApiDocs
task automatically builds the java code and generates the openapi document without any warnings.
Other questions:
- The openapi-gradle-plugin creates the “generateOpenApiDocs” task in an
afterEvaluate
function: OpenApiGradlePlugin.kt
Is my usage ofafterEvaluate
the correct way to add the artifact?
- Do I have to convert the input configuration to a provider?
inputSpec.set(provider { openApiConf.singleFile.path })