Java – Referencing external files from the gradle plugin (openapitools generator) (openapi specification)

Referencing external files from the gradle plugin (openapitools generator) (openapi specification)… here is a solution to the problem.

Referencing external files from the gradle plugin (openapitools generator) (openapi specification)

I’m trying to build an application that references the OpenAPI specification that has been published in Artifactory. This means I’m going to bring foo.yaml as a dependency, but I can’t seem to figure out how to actually reference the file via the openapitools generator plugin.

Given that the OpenAPI specification can be used to generate both server and client code, it makes perfect sense to publish it separately and simply introduce it and be referenced by the implementation.

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

I’ve added dependencies to the top layer of the build.gradle.kts file and added them as 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'
    }
}

The results I get:
* What went wrong:
Task “:generateFooCode” failed to execute.

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

Solution

After a lot of Google searches, I found 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 can get inputSpec to reference $buildDir/api/spec.yaml

Related Problems and Solutions