英文:
build.gradle.kts build openAPI classes from different interfaces
问题
I use build.gradle.kts构建文件来从openApi描述的yaml文件中创建客户端存根。
这只适用于一个接口(interface1.yaml)。
要创建存根,我使用:
gradlew openApiGenerate
从我的build.gradle.kts文件中的代码片段:
<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->
<!-- 语言: lang-java -->
openApiGenerate {
generatorName.set("kotlin")
inputSpec.set("$rootDir/sharedSDK/openApi/interface1.yaml")
outputDir.set("$rootDir/sharedSDK/")
configOptions.set(mapOf( "dateLibrary" to "java8" ))
modelPackage.set("package.inteface1.model")
library.set("multiplatform")
generateModelTests.set(false)
generateApiTests.set(false)
generateModelDocumentation.set(false)
generateApiDocumentation.set(false)
modelNamePrefix.set("V2")
globalProperties.set(
mapOf(
// "apis" to "false",
// "models" to "true"
"models" to "" // 只生成模型相关的内容
))
}
<!-- 结束代码片段 -->
现在我需要从不同的yaml描述(interface2.yaml)中生成第二个接口的子类。
如何做到这一点?
一个文件中的2个openApiGenerate { ... }任务不起作用。
是否有一种方法可以将我的openApiGenerate { ... }定义传递给一个自定义任务?
谢谢
Stephan
英文:
I use build.gradle.kts build file to create the client stub from an openApi description yaml file.
This work well for only one interface (interface1.yaml)
To create the stub I use :
gradlew openApiGenerate
Snippend form my build.gradle.kts file :
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-java -->
openApiGenerate {
generatorName.set("kotlin")
inputSpec.set("$rootDir/sharedSDK/openApi/interface1.yaml")
outputDir.set("$rootDir/sharedSDK/")
configOptions.set(mapOf( "dateLibrary" to "java8" ))
modelPackage.set("package.inteface1.model")
library.set("multiplatform")
generateModelTests.set(false)
generateApiTests.set(false)
generateModelDocumentation.set(false)
generateApiDocumentation.set(false)
modelNamePrefix.set("V2")
globalProperties.set(
mapOf(
//"apis" to "false",
//"models" to "true"
"models" to "" // generate only model stuff
))
}
<!-- end snippet -->
Now I need to generate the sub classes for a second interface form a different yaml description (interface2.yaml)
How to do this ?
2 openApiGenerate { ... } task in one file didn't work
Is there a way to pass my openApiGenerate { ... } definition to an customer task
Thanks
Stephan
答案1
得分: 2
定义任务类型
Gradle允许您定义相同类型的多个任务。对于Kotlin,您可以按照以下方式操作:
tasks.register<GenerateTask>("GenerateInterface1"){
generatorName.set("kotlin")
inputSpec.set("$rootDir/sharedSDK/openApi/interface1.yaml")
outputDir.set("$rootDir/sharedSDK/")
configOptions.set(mapOf( "dateLibrary" to "java8" ))
modelPackage.set("package.inteface1.model")
library.set("multiplatform")
generateModelTests.set(false)
generateApiTests.set(false)
generateModelDocumentation.set(false)
generateApiDocumentation.set(false)
modelNamePrefix.set("V2")
globalProperties.set(
mapOf(
//"apis" to "false",
//"models" to "true"
"models" to "" // 仅生成模型内容
))
}
共享配置
如果您使用了许多相同的配置选项,可以将它们移到自己的文件中。这样做的额外好处是使您的任务更加简洁,但您仍然需要为每个任务定义一些特定的属性。
tasks.register<GenerateTask>("GenerateInterface1"){
generatorName.set("kotlin")
inputSpec.set("$rootDir/sharedSDK/openApi/interface1.yaml")
outputDir.set("$rootDir/sharedSDK/")
configFile.set("$rootDir/sharedSDK/generatorConfig.yml")
modelPackage.set("package.interface1.model")
}
tasks.register<GenerateTask>("GenerateInterface2"){
generatorName.set("kotlin")
inputSpec.set("$rootDir/sharedSDK/openApi/interface2.yaml")
outputDir.set("$rootDir/sharedSDK/")
configFile.set("$rootDir/sharedSDK/generatorConfig.yml")
modelPackage.set("package.interface2.model")
}
现在,您可以将所有配置设置移动到您的yaml文件中,以供两个生成器共享使用。
英文:
Define your tasks by type
Gradle allows you to define multiple tasks of the same type. For kotlin, you can do this as follow:
tasks.register<GenerateTask>("GenerateInterface1"){
generatorName.set("kotlin")
inputSpec.set("$rootDir/sharedSDK/openApi/interface1.yaml")
outputDir.set("$rootDir/sharedSDK/")
configOptions.set(mapOf( "dateLibrary" to "java8" ))
modelPackage.set("package.inteface1.model")
library.set("multiplatform")
generateModelTests.set(false)
generateApiTests.set(false)
generateModelDocumentation.set(false)
generateApiDocumentation.set(false)
modelNamePrefix.set("V2")
globalProperties.set(
mapOf(
//"apis" to "false",
//"models" to "true"
"models" to "" // generate only model stuff
))
}
Shared Config
If you are using a lot of the same config options, you can move them to their own file. This has the added benefit of making your tasks much more concise, but you will still need to define a few properties specific to each task.
tasks.register<GenerateTask>("GenerateInterface1"){
generatorName.set("kotlin")
inputSpec.set("$rootDir/sharedSDK/openApi/interface1.yaml")
outputDir.set("$rootDir/sharedSDK/")
configFile.set("$rootDir/sharedSDK/generatorConfig.yml")
modelPackage.set("package.interface1.model")
}
tasks.register<GenerateTask>("GenerateInterface2"){
generatorName.set("kotlin")
inputSpec.set("$rootDir/sharedSDK/openApi/interface2.yaml")
outputDir.set("$rootDir/sharedSDK/")
configFile.set("$rootDir/sharedSDK/generatorConfig.yml")
modelPackage.set("package.interface2.model")
}
Now you can move all your configuration settings to your yaml file to be shared by both generators.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论