当前位置: 首页 > 知识库问答 >
问题:

如何使用springdoc-openapi-maven-plugin和swagger-codegen-maven-plugin生成客户机代码?

倪棋
2023-03-14

我的目标是使用OpenAPI3.0生成Spring Boot REST客户端。

我希望首先生成API的OpenAPI规范文件(springdoc-openapi-maven-plugin),然后使用Maven从该文件(swagger-codegen-maven-plugin)生成客户机代码。

我的问题是swagger-codegen-maven-plugin在springdoc-openapi-maven-plugin之前执行。因此,当swagger-codegen-maven-plugin执行时,springdoc-openapi-maven-plugin生成的输出文件不存在。

给定以下Maven构建插件配置,如何在swagger-codegen-maven-plugin之前执行springdoc-openapi-maven-plugin?

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.4.1</version>
                <executions>
                    <execution>
                        <id>pre-integration-test</id>
                        <goals>
                            <goal>start</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>post-integration-test</id>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.springdoc</groupId>
                <artifactId>springdoc-openapi-maven-plugin</artifactId>
                <version>1.1</version>
                <executions>
                    <execution>
                        <id>integration-test</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <apiDocsUrl>myServerUrl:myPort/v3/api-docs</apiDocsUrl>
                    <outputFileName>openApiFile.json</outputFileName>
                    <outputDir>${project.basedir}/src/main/resources</outputDir>
                    <skip>false</skip>
                </configuration>
            </plugin>

            <plugin>
                <groupId>io.swagger.codegen.v3</groupId>
                <artifactId>swagger-codegen-maven-plugin</artifactId>
                <version>3.0.24</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/src/main/resources/openApiFile.json</inputSpec>
                            <language>typescript-angular</language>
                            <configOptions>
                                <sourceFolder>src/gen/java/main</sourceFolder>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

共有1个答案

邹涵畅
2023-03-14

问题是,springdoc-openapi-maven-plugin在integration-test阶段执行,而swagger-codegen-maven-plugin默认阶段是generate-sources,在构建生命周期中integration-test之前执行。

我只是为swagger-codegen-maven-plugin指定了一个阶段,它在integration-test: post-integration-test 之后。

 类似资料: