Home > Net >  build.gradle.kts - how to set compiler args correctly?
build.gradle.kts - how to set compiler args correctly?

Time:02-16

Got the following content:

val commonCompilerArgs = listOfNotNull(
    "-Dmicronaut.openapi.views.spec=rapidoc.enabled=true,swagger-ui.enabled=true,swagger-ui.theme=flattop",
)




tasks {
    compileKotlin {
        kotlinOptions {
            jvmTarget = "11"
            freeCompilerArgs = commonCompilerArgs
        }
    }
    compileTestKotlin {
        kotlinOptions {
            jvmTarget = "11"
        }
    }
}

But got this error:

Invalid argument: -Dmicronaut.openapi.views.spec=rapidoc.enabled=true,swagger-ui.enabled=true,swagger-ui.theme=flattop

I guess it is simple, but I don't know how to set this compiler arguments correctly.

Any help appreciated!

CodePudding user response:

If you are using kapt in your Micronaut application gradle scripts then you can set it this way:

kapt {
    arguments {
        arg(
            "micronaut.openapi.views.spec",
            "rapidoc.enabled=true,swagger-ui.enabled=true,swagger-ui.theme=flattop"
        )
    }
}

Open API definition files and Swagger UI are generated during annotation processing phase, where source code and resource files are generated. It means that it is before compileKotlin task.

Part of example build output:

...
> Task :my-app:kaptGenerateStubsKotlin
> Task :my-app:processResources
> Task :my-app:detekt
> Task :my-app:processTestResources NO-SOURCE
> Task :my-app:kaptKotlin
Note: Generating OpenAPI Documentation
Note: Writing OpenAPI YAML to destination: /home/me/prj/my-app/backend/build/tmp/kapt3/classes/main/META-INF/swagger/my-app-1.0.yml
Note: Writing OpenAPI View to destination: /home/me/prj/my-app/backend/build/tmp/kapt3/classes/main/META-INF/swagger/views/swagger-ui/index.html
Note: Creating bean classes for 9 type elements

> Task :my-app:compileKotlin
> Task :my-app:compileJava NO-SOURCE
> Task :my-app:classes
...

Update: You must also add micronaut.router.static-resources section for Swagger into application.yaml configuration file to map swagger static routes like this:

micronaut:
  router:
    static-resources:
      swagger:
        paths: classpath:META-INF/swagger
        mapping: /swagger/**
      swagger-ui:
        paths: classpath:META-INF/swagger/views/swagger-ui
        mapping: /swagger-ui/**

Then you can access OpenAPI definition file on http://localhost:8080/swagger/my-app-1.0.yml and Swagger UI should be available on http://localhost:8080/swagger-ui .

  • Related