1

My requirement is that I have to generate the springboot server code from a swagger definition. I have generated the code with the help of below-mentioned command (by using swagger-codegen-cli-2.3.1.jar).

java -jar swagger-codegen-cli-2.3.1.jar generate ^
     -i nycemoves.yml ^
     --api-package com.nyce.moves.api ^
     --model-package com.nyce.moves.model ^
     --invoker-package com.nyce.moves.invoker ^
     --group-id com.nyce.moves ^
     --artifact-id nyce-moves ^
     --artifact-version 0.0.1-SNAPSHOT ^
     -l spring ^
     --library spring-boot ^
     -o nyce-moves

Now, we have updated the swagger definition and want to re-generate the resulting models and api-invokers, for this we would like to use a maven plugin. After looking through various answers on the internet, we came across the following build plugins which we have added in our pom.xml.

<plugin>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-codegen-maven-plugin</artifactId>
            <version>2.3.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <inputSpec>src/main/resources/nycemoves.yml</inputSpec>
                        <language>spring</language>
                        <basePackage>${default.package}</basePackage>
                        <modelPackage>${default.package}.model</modelPackage>
                        <apiPackage>${default.package}.api</apiPackage>
                        <invokerPackage>${default.package}.invoker</invokerPackage>
                        <configOptions>
                            <interfaceOnly>true</interfaceOnly>
                            <dateLibrary>java8</dateLibrary>
                            <java8>true</java8>
                            <library>spring-boot</library>
                            <serializableModel>true</serializableModel>
                            <sourceFolder>src/main/java</sourceFolder>                              
                        </configOptions>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Now, when we do mvn clean package, we want it to update / override all the models, api and invokers. But, this is not working. We are seeing the below mentioned exception.

[INFO] 16 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.516 s
[INFO] Finished at: 2019-03-06T15:05:58+05:30
[INFO] Final Memory: 34M/448M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project nyce-moves: Compilation failure: Compilation failure:
[ERROR] /nyce-moves/src/main/java/com/nyce/moves/api/CommentsApi.java:[29,8] duplicate class: com.nyce.moves.api.CommentsApi
[ERROR] /nyce-moves/target/generated-sources/swagger/src/main/java/com/nyce/moves/model/PostRequest.java:[19,8] duplicate class: com.nyce.moves.model.PostRequest
...

So, we actually need the plugin to do the following things, but we are not able to figure it out. a) When we run mvn clean package it should regenerate all the source-code for the server stub in source folder as well as target folder. b) In case of the class already exists, it should override them. We would like to handle the override later with the help of .swagger-codegen-ignore.

I know there are lots of discussion threads on this on stackoverflow, but I could not find any solution which can help me here. Please help me on this or direct me to the relevant thread.

Mohit Mehrotra
  • 201
  • 1
  • 6
  • 12

1 Answers1

0

I had the same problem and I needed to define also the output directory. Here is a snippet:

<configuration>
    <language>spring</language>
    <library>spring-cloud</library>
    <configOptions>
        <sourceFolder>swagger</sourceFolder>
        <interfaceOnly>true</interfaceOnly>
        <dateLibrary>java8</dateLibrary>
    </configOptions>
    <output>${project.build.directory}/generated-sources</output>
</configuration>
Ricardo Veloso
  • 131
  • 1
  • 4