Seldon
Seldon

Reputation: 43

Add custom codegen implementation for openapi-generator gradle plugin

I implement my custom code generation for https://github.com/OpenAPITools/openapi-generator

but i have no idea how to add this to gradle plugin. I need to add it to classpath while gradle perform openapi tasks

For maven i can easily add my custom implementation com.my.generator:customgenerator:1.0-SNAPSHOT in plugin dependency block,

<plugin>
      <groupId>org.openapitools</groupId>
      <artifactId>openapi-generator-maven-plugin</artifactId>
      <version>${openapi-generator-maven-plugin-version}</version>
      <executions>
          <execution>
              <goals>
                  <goal>generate</goal>
              </goals>
              <configuration>
                <templateDirectory>myTemplateDir</templateDirectory>
                <apiPackage>${default.package}.handler</apiPackage>
                <modelPackage>${default.package}.model</modelPackage>
            </configuration>
          </execution>
       </executions>

      <dependencies>
          <dependency>
              <groupId>com.my.generator</groupId>
              <artifactId>customgenerator</artifactId>
              <version>1.0-SNAPSHOT</version>
          </dependency>
      </dependencies>
</plugin>

but in gradle i have no idea how to do it

Upvotes: 4

Views: 2361

Answers (1)

OrdinaryNick
OrdinaryNick

Reputation: 248

The solution is simple if you know how Gradle plugins work. Here are steps how to do it:

  1. You need to add your custom generator class to the classpath of the plugin. But, you can not use there any module of the Gradle project, in which you want to use the generator plugin, because Gradle plugins are applied before the whole compilation of the project and also before dependencies are resolved. So, you must use the already compiled jar file. For example, create a new Gradle project where you place custom generator code and publish it to maven local repository (How to publish source into local maven repository with Gradle?). Then you can add it to plugins classpath like this:
buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath "org.openapitools:openapi-generator:4.3.0"
        classpath "some.custom.openapi:generator:0.0.1"
    }
}
  1. Openapi generator use Java service loader to load generators (https://docs.oracle.com/javase/8/docs/api/java/util/ServiceLoader.html). So, in your custom generator project create file org.openapitools.codegen.CodegenConfig with content
some.custom.openapi.CustomJavaCodegen

(Here must be the name of the custom generator class) and place it to folder src/main/resources/META-INF/services/.

  1. In your custom generator class override method getName with your generator name, which you will use in the configuration of openApiGenerator in the Gradle file.

I get this working with these steps. If I forget something to write it here, comment, and I will try to fill missing information.

Upvotes: 1

Related Questions