Reference external file (openapi spec) from gradle plugin (openapitools generator)

核能气质少年 提交于 2021-01-01 06:24:52

问题


I am attempting to build an application that references an openapi spec that is already published in artifactory. That means I'll be pulling the foo.yaml in as a dependency, but I can't seem to figure out how to actually reference that file by the openapitools generator plugin.

Given that openapi specs can be used to generate both server code and client code, it makes perfect sense that it is published separately and simply pulled in and referenced by implementations.

com.company.bar-1.0.10 contains foo.yaml at the top level of the jar.

I've added the dependency at the top level of the build.gradle.kts file and I've also added it as a part of the plugin task itself.

task generateFooCode(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {

generatorName = "java"
apiPackage = 'com.ehi.gbo.openapiconnect.api.foo'
modelPackage = 'com.ehi.gbo.openapiconnect.model.foo'
invokerPackage = 'com.ehi.gbo.openapiconnect.common.invoker'
inputSpec = "foo.yaml".toString()
outputDir = "$buildDir/generated-sources/foo".toString()
configOptions = [
        dateLibrary          : "java8",
        useTags              : true,
        interfaceOnly        : true,
        delegatePattern      : false,
        useBeanValidation    : false,
        performBeanValidation: false,
        useOptional          : false,
        serviceImplementation: false,
        serviceInterface     : false,
        java8                : false,
        serializableModel    : true,
        skipDefaultInterface : true,
        reactive             : false,
]
configurations {
    dependencies {
        implementation 'com.company.bar:foo-api:1.0.10'
    }
}

}

Results I'm getting: * What went wrong: Execution failed for task ':generateFooCode'.

There were issues with the specification. The option can be disabled via validateSpec (Maven/Gradle) or --skip-validate-spec (CLI). | Error count: 1, Warning count: 0 Errors: -unable to read location foo.yaml


回答1:


After a lot of googling, I came across a very elegant solution.

configurations {
    api
}
  dependencies {
    api 'somegroup:someArtifact:someVersion'
}
  task extractApi(type: Sync) {
    dependsOn configurations.api

    from { // use of closure defers evaluation until execution time
        configurations.api.collect { zipTree(it) }
    }
    into "$buildDir/api/"
}

Then I could just make the inputSpec reference $buildDir/api/spec.yaml



来源:https://stackoverflow.com/questions/58564783/reference-external-file-openapi-spec-from-gradle-plugin-openapitools-generato

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!