OpenAPI Generator Maven Plugin 6.6.0构建失败,需自动生成API Spec至resources目录
解决步骤
问题根源是你当前的插件配置默认读取本地src/main/resources/api.yaml文件,但你需要的是从springdoc提供的HTTP端点获取OpenAPI规范并生成到指定目录,而非读取本地文件。以下是正确的Maven配置流程:
1. 调整插件执行顺序
需要先临时启动Spring应用,获取到localhost:8080/api-doc的规范后,再执行OpenAPI生成逻辑,最后停止临时应用。
2. 完整pom.xml配置片段
在<build><plugins>中添加以下配置:
配置Spring Boot插件(临时启停应用)
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <id>start-spring-app</id> <phase>generate-resources</phase> <goals> <goal>start</goal> </goals> <configuration> <!-- 可选:指定端口避免冲突 --> <jvmArguments>-Dserver.port=8080</jvmArguments> <fork>true</fork> </configuration> </execution> <execution> <id>stop-spring-app</id> <phase>generate-resources</phase> <goals> <goal>stop</goal> </goals> </execution> </executions> </plugin>
配置OpenAPI Generator Maven Plugin
<plugin> <groupId>org.openapitools</groupId> <artifactId>openapi-generator-maven-plugin</artifactId> <version>6.6.0</version> <executions> <execution> <id>generate-openapi-spec</id> <phase>generate-resources</phase> <goals> <goal>generate</goal> </goals> <configuration> <!-- 从springdoc的HTTP端点获取规范 --> <inputSpec>http://localhost:8080/api-doc</inputSpec> <!-- 指定输出目录 --> <output>${project.basedir}/src/main/resources</output> <!-- 选择生成格式:openapi-yaml 或 openapi-json --> <generatorName>openapi-yaml</generatorName> <!-- 可选:自定义生成的文件名 --> <configOptions> <outputFileName>api.yaml</outputFileName> </configOptions> </configuration> </execution> </executions> </plugin>
3. 关键配置说明
inputSpec:直接指向springdoc的规范端点,替代本地文件路径,解决找不到api.yaml的报错。phase:将所有插件绑定到generate-resources阶段,确保mvn clean install时按「启动应用→生成规范→停止应用」的顺序执行。generatorName:按需选择openapi-yaml或openapi-json,对应生成yaml/json格式的规范文件。outputFileName:可自定义生成后的文件名,默认是openapi.yaml或openapi.json。
4. 验证执行
执行mvn clean install,插件会自动完成:启动Spring应用→拉取规范→写入src/main/resources目录→停止临时应用。
内容的提问来源于stack exchange,提问作者Sameer Choudhary
相关产品推荐
相关产品推荐

