How to generate client code using with swagger-codegen-plugin (maven)?

rajesh reddy SR picture rajesh reddy SR · Mar 20, 2018 · Viewed 47.1k times · Source

I need to generate a server stub code in eclipse using with swagger-codegen-plugin (for maven) . can you please help how to do it ? and what configuration needed for that( in pom.xml).

Answer

rajesh reddy SR picture rajesh reddy SR · Mar 20, 2018

I found this answer. You just need to change pom.xml like below.

pom.xml.

<properties>
           <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
           <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
           <java.version>1.8</java.version>
           <version.swagger.codegen>2.2.1</version.swagger.codegen>
           <yaml.file>${project.basedir}/src/main/resources/Api.yaml</yaml.file>
           <generated-sources-path>${project.build.directory}/generated-sources</generated-sources-path>
           <generated-sources-java-path>main/java</generated-sources-java-path>
       </properties>

<build>
           <plugins>
               <plugin>
                   <groupId>org.springframework.boot</groupId>
                   <artifactId>spring-boot-maven-plugin</artifactId>
               </plugin>
               <plugin>
                   <groupId>io.swagger</groupId>
                   <artifactId>swagger-codegen-maven-plugin</artifactId>
                   <version>${version.swagger.codegen}</version>
                   <configuration>
                       <inputSpec>${yaml.file}</inputSpec>
                       <configOptions>
                           <sourceFolder>${generated-sources-java-path}</sourceFolder>
                       </configOptions>
                       <output>${generated-sources-path}</output>
                   </configuration>
                   <executions>
                       <execution>
                           <id>generate-swagger-spring</id>
                           <phase>generate-sources</phase>
                           <goals>
                               <goal>generate</goal>
                           </goals>
                           <configuration>
                               <language>spring</language>
                               <modelPackage>${project.groupId}.swagger.model</modelPackage>
                               <apiPackage>${project.groupId}.swagger.api</apiPackage>
                               <invokerPackage>${project.groupId}.swagger.invoker</invokerPackage>
                           </configuration>
                       </execution>
                   </executions>
               </plugin>        
               <plugin>
                   <groupId>org.codehaus.mojo</groupId>
                   <artifactId>build-helper-maven-plugin</artifactId>
                   <executions>
                       <execution>
                           <id>add-generated-source</id>
                           <phase>initialize</phase>
                           <goals>
                               <goal>add-source</goal>
                           </goals>
                           <configuration>
                               <sources>
                                   <source>${generated-sources-path}/${generated-sources-java-path}</source>
                               </sources>
                           </configuration>
                       </execution>
                   </executions>
               </plugin>                
           </plugins>   

       <pluginManagement>
           <plugins>
               <plugin>
                   <groupId>org.eclipse.m2e</groupId>
                   <artifactId>lifecycle-mapping</artifactId>
                   <version>1.0.0</version>
                   <configuration>
                       <lifecycleMappingMetadata>
                           <pluginExecutions>
                               <pluginExecution>
                                   <pluginExecutionFilter>
                                       <groupId>io.swagger</groupId>
                                       <artifactId>swagger-codegen-maven-plugin</artifactId>
                                       <versionRange>[${version.swagger.codegen},)</versionRange>
                                       <goals>
                                           <goal>generate</goal>
                                       </goals>
                                   </pluginExecutionFilter>
                                   <action>
                                       <execute />
                                   </action>
                               </pluginExecution>
                           </pluginExecutions>
                       </lifecycleMappingMetadata>
                   </configuration>
               </plugin>
           </plugins>
       </pluginManagement>          
       </build>