You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

纯Kotlin项目中Micronaut配置Swagger/OpenAPI视图方法问询

How to Pass Micronaut OpenAPI View Spec Parameter to Pure Kotlin Projects (Groovy build.gradle)

The issue you're facing comes down to targeting the right task type: the official docs reference JavaCompile (for Java projects), but pure Kotlin projects use KotlinCompile tasks instead. Your earlier attempt using tasks.withType(compileKotlin) didn't work because you referenced the task name rather than the actual task class.

Here's the correct configuration for your Groovy-based build.gradle:

Option 1: Directly add JVM arguments to KotlinCompile tasks

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
    // Optional: Set your desired JVM target version (adjust as needed)
    kotlinOptions.jvmTarget = "17"
    
    // Pass the Micronaut OpenAPI view spec system property
    jvmArgs '-Dmicronaut.openapi.views.spec=rapidoc.enabled=true,swagger-ui.enabled=true,swagger-ui.theme=flattop'
}

Option 2: Use fork options (mirroring the JavaCompile approach)

If you prefer to mirror the exact structure from the official docs, you can enable forking and add the argument to forkOptions:

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
    options.fork = true
    options.forkOptions.jvmArgs << '-Dmicronaut.openapi.views.spec=rapidoc.enabled=true,swagger-ui.enabled=true,swagger-ui.theme=flattop'
}

Key Notes:

  • Ensure you have the Micronaut OpenAPI plugin applied in your build.gradle (along with the Kotlin plugin):
    plugins {
        id("io.micronaut.application") version "4.2.0" // Use your Micronaut version
        id("io.micronaut.openapi") version "5.3.0" // Match plugin version to your Micronaut version
        kotlin("jvm") version "1.9.20" // Use your Kotlin version
    }
    
  • After updating the configuration, run ./gradlew compileKotlin (or any task that triggers compilation) to generate the OpenAPI views. The system property will be passed to the Kotlin compiler process, which the Micronaut OpenAPI plugin uses to enable the specified UI views.

内容的提问来源于stack exchange,提问作者lathspell

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:51:49