我正在尝试让 swagger codegen
在使用 gradle (kotlin) 构建的项目中工作。
我的引用是这里的这个例子:https://github.com/int128/gradle-swagger-generator-plugin这是在 Gradle groovy 版本中制作的。
现在 build.gradle.kts
如下:
repositories {
jcenter()
}
plugins {
java
id("org.springframework.boot") version "2.1.2.RELEASE"
id("io.spring.dependency-management") version "1.0.6.RELEASE"
id("org.hidetake.swagger.generator") version "2.16.0"
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation ("io.swagger:swagger-annotations:1.5.21")
swaggerCodeGen("io.swagger:swagger-codegen-cli:2.3.1")
// Use JUnit test framework
testImplementation ("junit:junit:4.12")
}
swaggerSources {
petstore {
inputFile = file('petstore.yaml')
code {
language = 'spring'
}
}
}
但是 IntelliJ 不喜欢谈论 Swagger 的台词:
我是 gradle 的新手,所以我不明白我应该做什么。 swaggerCodeGen 应该是一个函数吗?这个功能应该在哪里导入? swaggerSources
应该导入哪里?
最佳答案
import org.hidetake.gradle.swagger.generator.GenerateSwaggerCode
// plugins, repositories are same, but note import above ^^^
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation ("io.swagger:swagger-annotations:1.5.21")
"swaggerCodegen"("io.swagger:swagger-codegen-cli:2.3.1") // 1
// Use JUnit test framework
testImplementation ("junit:junit:4.12")
}
swaggerSources {
create("petstore").apply { // 2
setInputFile(file("petstore.yaml")) // 3
code(closureOf<GenerateSwaggerCode> { // 4
language = "spring"
})
}
}
1 - Kotlin 中的动态解析配置如下所示(动态地来自 Groovy,因此在编译时使用它存在问题,String 上的扩展调用运算符是我们的救星);
2 - swaggerSources
返回你NamedDomainObjectContainer<SwaggerSource>
,因此要添加新容器,我们调用 create
以它的名字作为参数;
3 - Kotlin 不如 Groovy 灵活,因此调用 setter 而不是设置字段;
4 - Groovy 的闭包远非函数式接口(interface),因此我们在插件的源代码中指定泛型类型 Closure
没有参数化。
关于kotlin - 尝试在 gradle kotlindsl 中包含 swagger-codegen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54233751/