Maven

A Maven plugin to support the OpenAPI generator project

Example

Add to your build->plugins section (default phase is generate-sources phase)

  1. <plugin>
  2. <groupId>org.openapitools</groupId>
  3. <artifactId>openapi-generator-maven-plugin</artifactId>
  4. <version>3.3.4</version>
  5. <executions>
  6. <execution>
  7. <goals>
  8. <goal>generate</goal>
  9. </goals>
  10. <configuration>
  11. <inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
  12. <generatorName>java</generatorName>
  13. <configOptions>
  14. <sourceFolder>src/gen/java/main</sourceFolder>
  15. </configOptions>
  16. </configuration>
  17. </execution>
  18. </executions>
  19. </plugin>

Followed by:

  1. mvn clean compile

For full details of all options, see the plugin README.

Gradle

This gradle plugin offers a declarative DSL via extensions (these are Gradle project extensions). These map almost fully 1:1 with the options you’d pass to the CLI or Maven plugin. The plugin maps the extensions to a task of the same name to provide a clean API. If you’re interested in the extension/task mapping concept from a high-level, you can check out Gradle’s docs.

To include in your project, add the following to build.gradle:

  1. buildscript {
  2. repositories {
  3. mavenLocal()
  4. mavenCentral()
  5. }
  6. dependencies {
  7. classpath "org.openapitools:openapi-generator-gradle-plugin:3.3.4"
  8. }
  9. }
  10. apply plugin: 'org.openapi.generator'

This gives access to the following tasks:

TaskDescription
openApiGenerateGenerate code via Open API Tools Generator for Open API 2.0 or 3.x specification documents.
openApiGeneratorsLists generators available via Open API Generators.
openApiMetaGenerates a new generator to be consumed via Open API Generator.
openApiValidateValidates an Open API 2.0 or 3.x specification document.

The plugin implements the above tasks as project extensions of the same name. If you’d like to declare these tasks as dependencies to other tasks (using dependsOn), you’ll need a task reference. e.g.:

  1. compileJava.dependsOn tasks.openApiGenerate

For full details of all options, see the plugin README.

Example

An example task for generating a kotlin client:

  1. openApiGenerate {
  2. generatorName = "kotlin"
  3. inputSpec = "$rootDir/specs/petstore-v3.0.yaml".toString()
  4. outputDir = "$buildDir/generated".toString()
  5. apiPackage = "org.openapi.example.api"
  6. invokerPackage = "org.openapi.example.invoker"
  7. modelPackage = "org.openapi.example.model"
  8. modelFilesConstrainedTo = [
  9. "Error"
  10. ]
  11. configOptions = [
  12. dateLibrary: "java8"
  13. ]
  14. }