Incremental build support for a task

I am using some task (Swagger codegen Gradle plugin & OpenAPI Style Validator) in my Gradle build that do not support it seems the incremental build feature.

Googling a bit (and checking the Gradle doc) I understood that it seems possible to extend the tasks in order to add some properties, so I gave it a try, but unfortunately it does not seem to work.
Here’s the build.gradle extension I added:

swaggerSources {
    jaxrsServer {
        inputFile = file('path/to/spec.yaml')
        code {
            language = 'jaxrs'
            library = 'jersey2'
            outputDir = file('build/generated/sources/swagger')
            templateDir = file('path/to/templates')
        }
    }
    jaxrsClient {
        inputFile = file('path/to/spec.yaml')
        code {
            language = 'java'
            library = 'jersey2'
            outputDir = file('build/generated/sources/swagger')
        }
    }
}

tasks.withType(GenerateSwaggerCode) {
    inputs.file('path/to/spec.yaml')
    outputs.dir('build/generated/sources/swagger')
}

[...]

openAPIStyleValidator {
    inputFile = "$projectDir/path/to/spec.yaml"
    validateInfoLicense = false
}

tasks.withType(org.openapitools.openapistylevalidator.gradle.OpenAPIStyleValidatorTask) {
    inputs.file("$projectDir/path/to/spec.yaml")
}

And the (truncated) build output:

> Task :module:openAPIStyleValidator
Validating spec: /.../path/to/spec.yaml
[...]
> Task :module:resolveSwaggerTemplate NO-SOURCE
> Task :module:generateSwaggerCodeJaxrsClient
> Task :module:generateSwaggerCodeJaxrsServer
> Task :module:generateSwaggerCode NO-SOURCE
> Task :module:compileJava

Is the task extension wrong? Is there any other way to achieve this?

This Swagger plugin does support incremental builds without your additions to GenerateSwaggerCode.

However, you will likely have problems with the configuration in your example given that the destination is the same for both generateSwaggerCodeJaxrsClient and generateSwaggerCodeJaxrsServer task. This is generally not preferred, but even worse in this case because there’s logic in the plugin to delete the outputDir by default if the task runs.

Therefore, generateSwaggerCodeJaxrsClient deletes the directory and executes, then generateSwaggerCodeJaxrsServer deletes the directory and executes. You should be able to remove

tasks.withType(GenerateSwaggerCode) {
    inputs.file('path/to/spec.yaml')
    outputs.dir('build/generated/sources/swagger')
}

and run just one task at a time ( generateSwaggerCodeJaxrsClient or generateSwaggerCodeJaxrsServer) to see the incremental behavior. You’ll need to change your actual configuration to use this for real though.

1 Like

Oh gosh, it seems obvious now!
I had to do this kind of setup with Maven initially, and didn’t really change that as I’m migrating to Open API Generator.
I’ll adapt this way!