Random Notes by agilob

JavaGradle run two tasks of the same type

Jump to Section

I have this setup in a single project which handles backend and frontend generation of server and client code. This requies to run openapi-generator twice, once for backend with spring generator and once for frontend with typescript-angular generator. I need backend code to be generated to build directory - so it is not committed to version control. TypeScript code needs to be reformatted and committed to git.

TypeScript code also requires additional mapping due to non-standard structure of my specification.

task generateOpenAPIJava(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
    generatorName = "spring"
    inputSpec = "$rootDir/src/main/resources/swagger/api.yml"
    outputDir = "$buildDir/openapi"
    apiPackage = "net.agileb.web.api"
    modelPackage = "net.agileb.web.api.model"
    apiFilesConstrainedTo = [""]
    modelFilesConstrainedTo = [""]
    supportingFilesConstrainedTo = ["ApiUtil.java"]
    configOptions = [
        delegatePattern: "true",
        title: "agileb",
        dateLibrary: "java8",
        additionalModelTypeAnnotations: "@com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL)",
    ]
    validateSpec = true
}

task generateOpenAPITypeScript(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
    generatorName = "typescript-angular"
    inputSpec = "$rootDir/src/main/resources/swagger/api.yml".toString()
    outputDir = "$rootDir/src/main/webapp/app/models".toString()
    apiFilesConstrainedTo = [""]
    modelFilesConstrainedTo = [""]
    supportingFilesConstrainedTo = ["ApiUtil.java"]
    importMappings = [
        "MemberDTO": "app/shared/model/member.model"
    ]
    configOptions = [
        "taggedUnions": "true",
        "withInterfaces": "false",
        "stringEnums": "true",
        "enumPropertyNaming": "UPPERCASE"
    ]
    validateSpec = true
}

Now that we have two tasks specified, I need to run them as one, creating third task, that does nothing but depents on the other two:

task generateOpenAPI {
    description = "Generate OpenAPI files"
    dependsOn generateOpenAPIJava, generateOpenAPITypeScript
}

And run these tasks when I compile code to always verify the compatibility. This is probably excessive as it could run on demand, rather than automatically, so the dependency below might not be right for you.

compileJava.dependsOn("generateOpenAPI")

You can skip the last snippet and run your code generators with the command below, it will run two generators.

./gradlew generateOpenAPI

PS. You also might need something like this on top of the file to apply the plugin and resolve dependencies:


buildscript {
    repositories {
        maven { url "https://repo1.maven.org/maven2" }

    }
    dependencies {
        // Updated version can be passed via command line arg as -PopenApiGeneratorVersion=VERSION
        classpath "org.openapitools:openapi-generator-gradle-plugin:$openapi_plugin_version" 
    }
}

apply plugin: 'org.openapi.generator'