10

How do i generate code for multiple swagger files from within the same module/project in one pom.xml.

In my application client had provided a swagger and we have one of the backend API to be called for which they provided swagger. I want to generate code for both of these in the same project. One way i was thinking is create separate module and execute the plugin separately and have those dependencies called out in main module.

How do i generate code from one build plugin? Please point me to existing one if it is a repost. I couldn't find any.

Here is the plugin i configured in pom.xml

 <plugin>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-codegen-maven-plugin</artifactId>
            <version>2.2.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                      <inputSpec>${project.basedir}/src/main/ resources/Service.json</inputSpec><inputSpec>${project.basedir}/src /main/resources/Client.json</inputSpec>
                        <language>java</language>
                        <configOptions>
                            <dateLibrary>java8</dateLibrary>
                        </configOptions>
                        <modelPackage>com.service.model</modelPackage>
                        <environmentVariables>
                            <models/>
                            <supportingFiles>false</supportingFiles>
                        </environmentVariables>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Also tried *.json. At anytime it is taking only one json file and generating the code.

rajanikanth
  • 123
  • 1
  • 2
  • 8

1 Answers1

13

In order to do this you could declare a different execution for each json file, each one should have a unique id.

Here is an example with two executions, execution-first-json for the file first.json and execution-second-json for the file second.json

<plugin>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>2.2.1</version>
    <executions>
        <execution>
            <id>execution-first-json</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
              <inputSpec>${project.basedir}/src/main/resources/first.json</inputSpec>
                <language>java</language>
                <configOptions>
                    <dateLibrary>java8</dateLibrary>
                </configOptions>
                <modelPackage>com.service.model</modelPackage>
                <environmentVariables>
                    <models/>
                    <supportingFiles>false</supportingFiles>
                </environmentVariables>
            </configuration>
        </execution>
        <execution>
            <id>execution-second-json</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
              <inputSpec>${project.basedir}/src/main/resources/second.json</inputSpec>
                <language>java</language>
                <configOptions>
                    <dateLibrary>java8</dateLibrary>
                </configOptions>
                <modelPackage>com.service.model</modelPackage>
                <environmentVariables>
                    <models/>
                    <supportingFiles>false</supportingFiles>
                </environmentVariables>
            </configuration>
        </execution>
    </executions>
</plugin>
moondaisy
  • 4,303
  • 6
  • 41
  • 70
  • the problem i am facing is both the apis classes generated has same io.swagger.model package and i have same class names. how to generate them with package being custom say com.src.api1.model and com.src.api2.model for example RequestCreate.java is created in both sourcefolders but when i try to import them in code both have package io.swagger.model; – JackAss Aug 23 '19 at 08:05
  • 1
    @JackAss , you can specify a separate package for each Task by property "modelPackage", for example: modelPackage = "com.src.api1.model" – NHS Dec 20 '19 at 13:15