如何编译实现了OpenAPI Generator生成源码目录中接口的项目
解决方案
该报错的核心原因是:默认配置下,openapi生成的target/generated-sources/openapi目录没有被加入到Kotlin编译器的源码扫描路径中,同时插件执行顺序可能存在优先级问题,导致编译自定义实现类时,生成的接口文件还未被识别。
按以下两步修改pom.xml即可解决:
1. 显式指定OpenAPI生成插件的执行阶段
给openapi-generator-maven-plugin的execution节点添加<phase>generate-sources</phase>配置,确保代码生成逻辑在编译阶段之前执行,修改后的插件配置如下:
<plugin> <groupId>org.openapitools</groupId> <artifactId>openapi-generator-maven-plugin</artifactId> <version>5.1.0</version> <executions> <execution> <phase>generate-sources</phase> <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>
2. 添加生成目录到编译路径
引入build-helper-maven-plugin,将OpenAPI生成的源码目录添加到项目编译路径中,配置如下:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>3.3.0</version> <executions> <execution> <phase>generate-sources</phase> <goals> <goal>add-source</goal> </goals> <configuration> <sources> <source>${project.build.directory}/generated-sources/openapi/src/main/kotlin</source> </sources> </configuration> </execution> </executions> </plugin>
修改完成后执行mvn clean compile即可正常编译。
内容的提问来源于stack exchange,提问作者Saffie
相关产品推荐
相关产品推荐

