2

I am using the OpenAPI generator maven plugin with kotlin-spring generator to generate interfaces for my API based on the specification.

As an example, I used the specification by this blog post and the following plugin configuration:

<plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>5.1.0</version>
                <executions>
                    <execution>*
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>
                                ${project.basedir}/src/main/resources/petstore.yml
                            </inputSpec>
                            <generatorName>kotlin-spring</generatorName>
                            <modelNameSuffix>Dto</modelNameSuffix>
                            <configOptions>
                                <basePackage>com.example</basePackage>
                                <apiPackage>com.example.api</apiPackage>
                                <modelPackage>com.example.model</modelPackage>
                                <configPackage>com.example.config</configPackage>
                                <delegatePattern>true</delegatePattern>
                                <interfaceOnly>true</interfaceOnly>
                                <supportingFilesToGenerate>
                                    ApiUtil.kt
                                </supportingFilesToGenerate>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

When I run mvn clean generate-sources then the files are properly generated in target/generated-sources/openapi/....

I then create an implementation of the delegate in my src folder where I can override the methods of the generated interface:

package com.example.api

class PetsApiDelegateImpl : PetsApiDelegate {

}

Up to now, everything is good and IntelliJ is also happy with it. However, when I run mvn clean compile the target folder is deleted and re-generated again as expected but I still receive an error:

[ERROR] Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.5.31:compile (compile) on project choreographer: Compilation failure
[ERROR] /path/to/example/src/main/kotlin/com/example/api/PetsApiDelegateImpl.kt:[3,29] Unresolved reference: PetsApiDelegate

In other words, the files are generated as part of mvn clean compile but compilation still fails because the interface is not found.

How can I successfully compile this project?

Saffie
  • 445
  • 3
  • 15

1 Answers1

3

We can resolve the compilation failure by adding an execution for the compile goal to the kotlin-maven-plugin that configures the generated directory as source directory.

<plugin>
  <groupId>org.jetbrains.kotlin</groupId>
  <artifactId>kotlin-maven-plugin</artifactId>
                
  <executions>
    <execution>
      <id>compile</id>
      <goals>
        <goal>compile</goal>
      </goals>
      <configuration>
        <sourceDirs>                 
          <sourceDir>${project.build.directory}/generated-sources/kotlin/src/main/kotlin</sourceDir>
        </sourceDirs>
      </configuration>
    </execution>
  </executions>
</plugin>
Bart
  • 17,070
  • 5
  • 61
  • 80
Saffie
  • 445
  • 3
  • 15
  • Maybe it's worth mentioning that this overwrites the default source dir. If you want to keep that too, specify both, e.g.: ``` ${project.basedir}/src/main/kotlin ${project.build.directory}/generated-sources/kotlin/src/main/kotlin ``` – Tim Büthe Feb 06 '23 at 10:14