int128 / gradle-swagger-generator-plugin

Gradle plugin for OpenAPI YAML validation, code generation and API document publishing
Apache License 2.0
307 stars 66 forks source link

example: usage with gradle5/kotlin dsl #121

Open jangalinski opened 5 years ago

jangalinski commented 5 years ago

I switched to gradle 5 and build.gradle.kts files ... How do I have to adopt the swaggerSources configuration block?

int128 commented 5 years ago

It is not supported yet. It will be supported in the future release.

mywill commented 5 years ago

I have it working on version 2.15.0 and gradle 5 with this

swaggerSources {
    create( "qwerservice", {
        setInputFile( file("swagger.yml") )
        code.outputDir = file( "$buildDir/client" )
        code.language = "spring"
        code.additionalProperties = mapOf( "group-id" to "com.qwer",
                                            "dateLibrary" to "java8",
                                            "java8" to "true",
                                            "modelPackage" to "com.qwer.model",
                                            "apiPackage" to "com.qwer.api",
                                            "invokerPackage" to "com.qwer.invoker" )
    } )
}
clementgpro commented 5 years ago

@mywill hi! Could you share your build.gradle.kts? I am trying to make it work with kotlindsl too.

ciscoo commented 5 years ago

@jangalinski @Clemzd @int128 and others that find this issue, here's an example:

plugins {
    // ...
    id("org.hidetake.swagger.generator") version "2.18.1"
}

// ...

dependencies {
    swaggerCodegen("io.swagger.codegen.v3:swagger-codegen-cli:3.0.8")
   // ... other Swagger dependencies like annotations
}

// ...

swaggerSources {
    // Gradle recommends `register` over `create` to avoid eagerly creating/configuring items.
    register("petstore") {
        // Part of the "petstore" SwaggerSource
        // Need to save a reference of it here because the `this` scope changes below.
        val validationTask = validation

        setInputFile(file("petstore-swagger.json"))

        // The method signiature is `code(@DelegatesTo(GenerateSwaggerCode) Closure closure)`
        // So best to use `delegateClosureOf<GenerateSwaggerCode>`
        // https://docs.gradle.org/current/userguide/kotlin_dsl.html#groovy_closures_from_kotlin
        code(delegateClosureOf<GenerateSwaggerCode> {
            language = "spring"
            components = listOf("models")
            dependsOn(validationTask)
        })
    }
}

tasks.named("compileJava").configure {
    dependsOn(tasks.named("generateSwaggerCode"))
}

sourceSets {
    val main by getting
    val petstore by swaggerSources.getting
    main.java.srcDir("${petstore.code.outputDir}/src/main/java")
}
bigpuritz commented 3 years ago

alternatively you can create a custom task like this:

import org.hidetake.gradle.swagger.generator.GenerateSwaggerCode

plugins {
    id("org.hidetake.swagger.generator") version "2.18.2"
}

dependencies {
    swaggerCodegen("io.swagger.codegen.v3:swagger-codegen-cli:3.0.23")
 }

tasks {
    register<GenerateSwaggerCode>("petstore") {
        inputFile = projectDir.resolve("petstore.yaml")
        language = "spring"
    }
 }
sreich commented 1 year ago

fyi to anyone seeing this, that "this scope changes" thing is not necessary. kotlin lets you use this@register and such, to access different scopes that are injected into the closure. it allows similar with return@ for breaking out of closures