OpenApi Generator Maven插件无法生成工件求助
OpenAPI Generator Maven插件未生成预期工件的解决方法
问题根源
mvn generate-sources命令仅触发源码生成阶段,OpenAPI Generator插件的generate目标默认不会自动打包生成独立工件(如pom、jar文件)。你之前使用的Swagger插件可能默认集成了打包逻辑,但OpenAPI Generator采用更模块化的设计,需要额外配置才能生成并发布独立工件。
解决步骤
- 启用工件附加配置
在插件的configuration中添加<attach>true</attach>,让生成的代码作为附加工件绑定到Maven构建流程中。 - 启用独立POM生成(可选)
如果你需要生成独立的pom文件,添加<generatePom>true</generatePom>配置。 - 执行完整构建命令
替换mvn generate-sources为mvn clean install,触发源码生成、编译、打包、安装的完整流程。
修改后的完整插件配置
<plugin> <groupId>org.openapitools</groupId> <artifactId>openapi-generator-maven-plugin</artifactId> <version>${openapi-generator.version}</version> <executions> <execution> <goals> <goal>generate</goal> </goals> <configuration> <inputSpec> ${project.basedir}/src/main/resources/petstore.yml </inputSpec> <generatorName>spring</generatorName> <apiPackage>com.baeldung.openapi.api</apiPackage> <modelPackage>com.baeldung.openapi.model</modelPackage> <generateApis>true</generateApis> <generateModels>true</generateModels> <generateSupportingFiles>true</generateSupportingFiles> <!-- 新增配置:启用工件附加和POM生成 --> <attach>true</attach> <generatePom>true</generatePom> <groupId>com.test.api.v1</groupId> <artifactId>test-api-schema</artifactId> <artifactVersion>0.0.1-SNAPSHOT</artifactVersion> <includeCollapsedSpecInArtifacts>true</includeCollapsedSpecInArtifacts> <supportingFilesToGenerate> ApiUtil.java </supportingFilesToGenerate> <configOptions> <delegatePattern>false</delegatePattern> <useJakartaEe>true</useJakartaEe> </configOptions> </configuration> </execution> </executions> </plugin>
验证结果
执行mvn clean install后,你会在target目录下看到生成的test-api-schema-0.0.1-SNAPSHOT.jar和对应的pom文件,同时工件会被安装到本地Maven仓库。
内容的提问来源于stack exchange,提问作者user2188158
相关产品推荐
相关产品推荐

