build.gradle.kts 从不同接口构建 openAPI 类。

huangapple go评论56阅读模式
英文:

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(&quot;kotlin&quot;)
 
    inputSpec.set(&quot;$rootDir/sharedSDK/openApi/interface1.yaml&quot;)
 
    outputDir.set(&quot;$rootDir/sharedSDK/&quot;)
    configOptions.set(mapOf( &quot;dateLibrary&quot; to &quot;java8&quot; ))

    modelPackage.set(&quot;package.inteface1.model&quot;)
  
    library.set(&quot;multiplatform&quot;) 
    
    generateModelTests.set(false)
    generateApiTests.set(false)
    generateModelDocumentation.set(false)
    generateApiDocumentation.set(false)
    modelNamePrefix.set(&quot;V2&quot;)  

    globalProperties.set(
        mapOf(
            //&quot;apis&quot; to &quot;false&quot;,
            //&quot;models&quot; to &quot;true&quot;
            &quot;models&quot; to &quot;&quot; // 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&lt;GenerateTask&gt;(&quot;GenerateInterface1&quot;){
    generatorName.set(&quot;kotlin&quot;)
 
    inputSpec.set(&quot;$rootDir/sharedSDK/openApi/interface1.yaml&quot;)
 
    outputDir.set(&quot;$rootDir/sharedSDK/&quot;)
    configOptions.set(mapOf( &quot;dateLibrary&quot; to &quot;java8&quot; ))

    modelPackage.set(&quot;package.inteface1.model&quot;)
  
    library.set(&quot;multiplatform&quot;) 
    
    generateModelTests.set(false)
    generateApiTests.set(false)
    generateModelDocumentation.set(false)
    generateApiDocumentation.set(false)
    modelNamePrefix.set(&quot;V2&quot;)  

    globalProperties.set(
        mapOf(
            //&quot;apis&quot; to &quot;false&quot;,
            //&quot;models&quot; to &quot;true&quot;
            &quot;models&quot; to &quot;&quot; // 仅生成模型内容
        ))
}

共享配置

如果您使用了许多相同的配置选项,可以将它们移到自己的文件中。这样做的额外好处是使您的任务更加简洁,但您仍然需要为每个任务定义一些特定的属性。

tasks.register&lt;GenerateTask&gt;(&quot;GenerateInterface1&quot;){
    generatorName.set(&quot;kotlin&quot;)
    inputSpec.set(&quot;$rootDir/sharedSDK/openApi/interface1.yaml&quot;)
    outputDir.set(&quot;$rootDir/sharedSDK/&quot;)
    configFile.set(&quot;$rootDir/sharedSDK/generatorConfig.yml&quot;)
    modelPackage.set(&quot;package.interface1.model&quot;)
}

tasks.register&lt;GenerateTask&gt;(&quot;GenerateInterface2&quot;){
    generatorName.set(&quot;kotlin&quot;)
    inputSpec.set(&quot;$rootDir/sharedSDK/openApi/interface2.yaml&quot;)
    outputDir.set(&quot;$rootDir/sharedSDK/&quot;)
    configFile.set(&quot;$rootDir/sharedSDK/generatorConfig.yml&quot;)
    modelPackage.set(&quot;package.interface2.model&quot;)
}

现在,您可以将所有配置设置移动到您的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&lt;GenerateTask&gt;(&quot;GenerateInterface1&quot;){
    generatorName.set(&quot;kotlin&quot;)
 
    inputSpec.set(&quot;$rootDir/sharedSDK/openApi/interface1.yaml&quot;)
 
    outputDir.set(&quot;$rootDir/sharedSDK/&quot;)
    configOptions.set(mapOf( &quot;dateLibrary&quot; to &quot;java8&quot; ))

    modelPackage.set(&quot;package.inteface1.model&quot;)
  
    library.set(&quot;multiplatform&quot;) 
    
    generateModelTests.set(false)
    generateApiTests.set(false)
    generateModelDocumentation.set(false)
    generateApiDocumentation.set(false)
    modelNamePrefix.set(&quot;V2&quot;)  

    globalProperties.set(
        mapOf(
            //&quot;apis&quot; to &quot;false&quot;,
            //&quot;models&quot; to &quot;true&quot;
            &quot;models&quot; to &quot;&quot; // 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&lt;GenerateTask&gt;(&quot;GenerateInterface1&quot;){
    generatorName.set(&quot;kotlin&quot;)
    inputSpec.set(&quot;$rootDir/sharedSDK/openApi/interface1.yaml&quot;)
    outputDir.set(&quot;$rootDir/sharedSDK/&quot;)
    configFile.set(&quot;$rootDir/sharedSDK/generatorConfig.yml&quot;)
    modelPackage.set(&quot;package.interface1.model&quot;)
}

tasks.register&lt;GenerateTask&gt;(&quot;GenerateInterface2&quot;){
    generatorName.set(&quot;kotlin&quot;)
    inputSpec.set(&quot;$rootDir/sharedSDK/openApi/interface2.yaml&quot;)
    outputDir.set(&quot;$rootDir/sharedSDK/&quot;)
    configFile.set(&quot;$rootDir/sharedSDK/generatorConfig.yml&quot;)
    modelPackage.set(&quot;package.interface2.model&quot;)
}

Now you can move all your configuration settings to your yaml file to be shared by both generators.

huangapple
  • 本文由 发表于 2023年4月19日 22:16:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055588.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定