You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OpenApi Generator Maven插件无法生成工件求助

OpenAPI Generator Maven插件未生成预期工件的解决方法

问题根源

mvn generate-sources命令仅触发源码生成阶段,OpenAPI Generator插件的generate目标默认不会自动打包生成独立工件(如pom、jar文件)。你之前使用的Swagger插件可能默认集成了打包逻辑,但OpenAPI Generator采用更模块化的设计,需要额外配置才能生成并发布独立工件。

解决步骤

  1. 启用工件附加配置
    在插件的configuration中添加<attach>true</attach>,让生成的代码作为附加工件绑定到Maven构建流程中。
  2. 启用独立POM生成(可选)
    如果你需要生成独立的pom文件,添加<generatePom>true</generatePom>配置。
  3. 执行完整构建命令
    替换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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 12:25:12