Search code examples
swagger-codegen-maven-plugin

How to generate maven-jar-plugin 3.1.2 version in pom by swagger-codegen-maven-plugin


I am using swagger-codegen-maven-plugin 2.4.7 version to generate project. it generates maven-jar-plugin 2.6 version, but I need 3.1.2 version.

is there a way to generate custom plugin version?

generated 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>

wanted 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>

Solution

  • Swagger Codegen's output is based on Mustache templates. For example, the pom.xml template for the Java client + jersey2 library is available here:
    https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/Java/libraries/jersey2/pom.mustache

    Swagger Codegen lets you override the built-in templates with custom templates. You can do this as follows:

    1. Create a folder (say, codegen-templates) in your project.

    2. Download the pom.mustache template from this link to that folder.
      https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/Java/libraries/jersey2/pom.mustache

    3. Edit the downloaded pom.mustache and update the maven-jar-plugin version and configuration as required.

    4. In your pom.xml, specify the path to the codegen-templates folder in the <templateDirectory> parameter:

      <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>
      

    Now Swagger Codegen will generate pom.xml based on your custom pom.mustache template.