1

我正在使用 swagger-codegen-maven-plugin 2.4.7 版本来生成项目。它生成 maven-jar-plugin 2.6 版本,但我需要 3.1.2 版本。

有没有办法生成自定义插件版本?

生成的 pom.xml:

       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <goals>
                        <goal>jar</goal>
                        <goal>test-jar</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
            </configuration>
        </plugin>

想要 pom.xml:

       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.2</version>
            <executions>
                <execution>
                    <goals>
                        <goal>test-jar</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
            </configuration>
        </plugin>
4

1 回答 1

1

Swagger Codegen 的输出基于 Mustache 模板。例如,pom.xmlJava 客户端 + 库的模板jersey2可在此处获得:
https ://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/Java/libraries /jersey2/pom.mustache

Swagger Codegen 允许您使用自定义模板覆盖内置模板。您可以按如下方式执行此操作:

  1. codegen-templates在您的项目中创建一个文件夹(例如)。

  2. pom.mustache从此链接下载模板到该文件夹​​。
    https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/Java/libraries/jersey2/pom.mustache

  3. 编辑下载pom.mustache的并根据需要更新maven-jar-plugin版本和配置。

  4. 在您的 中,在参数中指定文件夹pom.xml的路径:codegen-templates<templateDirectory>

    <plugin>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-codegen-maven-plugin</artifactId>
        <version>2.4.7</version>
        <executions>
            <execution>
                <goals>
                    <goal>generate</goal>
                </goals>
                <configuration>
                    <inputSpec>https://petstore.swagger.io/v2/swagger.yaml</inputSpec>
                    <language>java</language>
                    <library>jersey2</library>
    
                    <!-- The folder containing your custom pom.mustache -->
                    <templateDirectory>${project.basedir}/codegen-templates</templateDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

现在 Swagger Codegen 将pom.xml根据您的自定义pom.mustache模板生成。

于 2019-07-16T13:06:24.730 回答