This accompanies PR: OpenAPITools/openapi-generator#14909
It is expected that prior to running the below steps, you have first run:
$ git clone git@github.com:dimber-cais/openapi-generator.git openapi-generator-custom-templates
$ cd openapi-generator-custom-templates
$ git checkout feature/gradle-classpath-templates
$ cd modules/openapi-generator-gradle-plugin
$ /gradlew publishToMavenLocalThis will publish version 6.5.0-SNAPSHOT of the gradle plugin to your Maven local repository.
The root project is setup to utilize Gradle plugins within the local Maven
repository. This is achieved via the following in the settings.gradle.kts file:
pluginManagement {
repositories {
mavenLocal()
}
}$ cd templates-module
$ gradle publishToMavenLocalThis will build the templates-module as a JAR and publish it to the local
Maven repository. The JAR includes file custom-templates/README.mustache which
will override the default template with this custom template.
The generator-module is setup to generate as:
openApiGenerate {
generatorName.set("kotlin")
library.set("jvm-retrofit2")
packageName.set("com.example")
inputSpec.set("${project.projectDir}/spec.yaml")
generateApiDocumentation.set(false)
generateApiTests.set(false)
generateModelDocumentation.set(false)
generateModelTests.set(false)
outputDir.set("$buildDir/generated/sources/client")
templateResourcePath.set("custom-templates")
}The key setting here is templateResourcePath.set("custom-templates") which
will cause the generate task to use the custom templates.
We also configure it to ensure that the custom templates JAR is available to the plugin's classpath:
buildscript {
repositories {
mavenLocal()
}
dependencies {
classpath("com.example:templates-module:1.0")
}
}We can now execute the generate task:
$ cd generator-module
$ gradle openApiGenerateWe can see that the generated README.md has been changed to contain customized content:
$ cat build/generated/sources/client/README.md
# CUSTOMIZED CLASSPATH TEMPLATE OUTPUT