Swagger-codegen: Maven-Plugin adds wrong source folder to eclipse project Java Build path

Created on 23 Mar 2016  路  8Comments  路  Source: swagger-api/swagger-codegen

version: swagger-codegen-maven-plugin-2.1.5

The option "addCompileSourceRoot" is true by default.

It adds the following path as source folder to the eclipse Java Build path
target/generated-sources/swagger

However, the generated sources are located at:
target/generated-sources/swagger/src/main/java

So after running mvn eclipse:eclipse Eclipse will show build errors, the Source folder has to be corrected manually.

CodeGenMojo.java should be corrected from

project.addCompileSourceRoot(output.toString());

to

String sourceJavaFolder = output.toString() + "/" + configOptions.get(CodegenConstants.SOURCE_FOLDER);
            project.addCompileSourceRoot(sourceJavaFolder);

However, the code above is only working if the sourceFolder is configured the plugin configuration/configOptions. To make it working without the sourceFolder configuration, the actual source folder needs to be retrieved from the Generator.

Bug Swagger Codegen Maven Plugin

Most helpful comment

Man, coming from wsdl2java and the like this plugin is a PITA to get working. I've finally figured out the required voodoo to get usuable client code:

<plugin>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>2.2.3</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/swagger/myswagger.json</inputSpec>
                <language>java</language>
                <generateApis>false</generateApis>
                <generateModels>true</generateModels>
                <generateModelDocumentation>false</generateModelDocumentation>
                <generateModelTests>false</generateModelTests>
                <generateSupportingFiles>false</generateSupportingFiles>
                <modelPackage>com.example.client</modelPackage>
                <configOptions>
                    <dateLibrary>java8</dateLibrary>
                    <sourceFolder>swagger</sourceFolder>
                </configOptions>
                <output>target/generated-sources</output>
            </configuration>
        </execution>
    </executions>
</plugin>

This yields a reasonable layout which works in eclipse and Maven CLI:

pom.xml
src/
    main/
        resources/
            myswagger.json
target/
    generated-sources/
        swagger/
            com/
                example/
                    client/
                        ModelClass.java

All 8 comments

The workaround for 2.1.5 is setting addCompileSourceRoot to "false" and using the build-helper-maven-plugin to add the source folder ourselves:

...
<configOptions>
    <sourceFolder>src/gen/java/main</sourceFolder>
...
</configOptions>
<addCompileSourceRoot>false</addCompileSourceRoot>                      
<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals><goal>add-source</goal></goals>
                        <configuration>
                            <sources>
                                <source>${project.build.directory}/generated-sources/swagger/src/gen/java/main</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

Pls let me know if I should add a PR for this.

Seeing the same issue - thanks for posting the workaround :)

@jfiala please file a PR when you've time. Thanks!

The issue should be fixed. Please pull the latest master to give it a try.

It seems still not fixed? Version 2.2.2, eclipse still not auto add generate source folder as source folder

<plugin>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-codegen-maven-plugin</artifactId>
                <version>2.2.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <!-- specify the swagger yaml -->
                            <inputSpec>src/main/resources/swagger.yaml</inputSpec>
                            <!-- target to generate -->
                            <language>java</language>
                            <!-- pass any necessary config options -->
                            <configOptions>
                                <dateLibrary>joda</dateLibrary>
                            </configOptions>
                            <!-- override the default library to jersey2
                            <library>jersey2</library>
                             -->
                        </configuration>
                    </execution>
                </executions>
            </plugin>

Any specific reason for doing this?
I mean why not put <source>src/main/java</source> and then adding the packages generated by swagger-codegen to .gitignore?

Man, coming from wsdl2java and the like this plugin is a PITA to get working. I've finally figured out the required voodoo to get usuable client code:

<plugin>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>2.2.3</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/swagger/myswagger.json</inputSpec>
                <language>java</language>
                <generateApis>false</generateApis>
                <generateModels>true</generateModels>
                <generateModelDocumentation>false</generateModelDocumentation>
                <generateModelTests>false</generateModelTests>
                <generateSupportingFiles>false</generateSupportingFiles>
                <modelPackage>com.example.client</modelPackage>
                <configOptions>
                    <dateLibrary>java8</dateLibrary>
                    <sourceFolder>swagger</sourceFolder>
                </configOptions>
                <output>target/generated-sources</output>
            </configuration>
        </execution>
    </executions>
</plugin>

This yields a reasonable layout which works in eclipse and Maven CLI:

pom.xml
src/
    main/
        resources/
            myswagger.json
target/
    generated-sources/
        swagger/
            com/
                example/
                    client/
                        ModelClass.java
Was this page helpful?
0 / 5 - 0 ratings