Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maven-Plugin adds wrong source folder to eclipse project Java Build path #2437

Closed
jfiala opened this issue Mar 23, 2016 · 8 comments
Closed

Comments

@jfiala
Copy link
Contributor

jfiala commented Mar 23, 2016

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.

@jfiala
Copy link
Contributor Author

jfiala commented Mar 23, 2016

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>

@jfiala
Copy link
Contributor Author

jfiala commented Mar 23, 2016

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

@tadhgpearson
Copy link
Contributor

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

@wing328
Copy link
Contributor

wing328 commented Jun 10, 2016

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

jfiala added a commit to jfiala/swagger-codegen that referenced this issue Jul 4, 2016
@wing328
Copy link
Contributor

wing328 commented Oct 26, 2016

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

@wing328 wing328 closed this as completed Oct 26, 2016
@wing328 wing328 added this to the v2.2.2 milestone Oct 26, 2016
@IamFive
Copy link

IamFive commented Mar 14, 2017

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>

@apurvjain17
Copy link

apurvjain17 commented Nov 20, 2017

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?

@batwad
Copy link

batwad commented Nov 22, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants