redhat-developer / vscode-java

Java Language Support for Visual Studio Code
Eclipse Public License 2.0
2.08k stars 438 forks source link

Out of the box support for swagger-codegen-maven-plugin #935

Open jfcervera opened 5 years ago

jfcervera commented 5 years ago

There is no support for automatically detecting and adding to the classpath the automatically generated code by the swagger-codegen-maven-plugin.

This means that, even though the project builds without issues from the command line, all the java classes that import the auto-generated code complain that those imports don't exist from within VSCode.

A work-around is following the same advice you gave for "Annotation Processing support for Maven projects" at: https://github.com/redhat-developer/vscode-java/wiki/Annotation-Processing-support-for-Maven-projects.

i.e. you add the generated source code path with the build-helper-maven-plugin to your pom manually, but this is not ideal.

e.g. assuming <sourceFolder>src/main/java</sourceFolder>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <!-- Need to ensure the generated source folder is added to the project classpath, in jdt.ls -->
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>target/generated-sources/swagger/src/main/java</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

Any chances to get this done automatically? Or at the very least, could you add this to the documentation for others that stumble on the same issue?

Thanks

christarczon commented 5 years ago

I'm having the same problem (and using the same workaround) with jaxb2-maven-plugin so it seems like a general issue with plugins that add source folders.

bbeaudreault commented 5 years ago

Also seeing this issue with immutables.io and a custom generated sources plugin. Both output to the generated-sources directory and it's really frustrating to have to add this config to all my project pom files, especially since it's only necessary for vscode

bbeaudreault commented 5 years ago

Would it be possible to enable Add Folder to Java Source path option? This would also be non-ideal but it is a much better workaround than having to edit the pom.xml. At least this way we can keep our pom files clean and only impact vscode project files.

When I try to do this, I get the following error:

Unsupported operation. Please use pom.xml file to manage the source directories of maven project.

Deathhush commented 4 years ago

Hit the same issue with Antlr plugin. As maven don't require you to manually add source files for plugins that supports the generate-sources phase and handles it automatically, shouldn't vscode be able to do the same? Adding manual configuration makes the POM with redundant information and is very annoying.

testforstephen commented 4 years ago

Tried multiple maven projects using generate-source plugins, some of them are recognized well by vscode-java, some not.

generate-source is not recognized: antlr3-maven-plugin sample: https://github.com/antlr/stringtemplate4

generate-source is automatically added to .classpath: antlr4-maven-plugin sample: https://github.com/teverett/antlr4example jaxb2-maven-plugin sample: https://github.com/bensherriff/CaesarCipher

Open stringtemplate4 project in eclipse, and the generated antlr3 folder is not added to .classpath at first and the pom.xml reports an error about generate-source goal. not-covered-by-phase

Then eclipse prompts me to install m2e-connector for the antlr3-maven-plugin. install_antlr3

install_m2e_connector

Follow the instruction to install m2e-connector, and restart eclipse. eclipse recognized the generated antlr3 folder well.

@fbricon Is there any gap between jdt.ls and eclipse?

fbricon commented 4 years ago

jdt.ls doesn't have a mechanism for discovery and installation of remote m2e connectors. And we cannot maintain support for all m2e connectors for every Maven plugin there is. So yes there's a gap.

I think m2e needs a better, more generic solution for discovering generated source folders

testforstephen commented 4 years ago

I see. The current m2e leverages lifecycle-mapping-metadata to recognize the customized generate-sources goal from the maven plugin. Here is a wiki about how m2e lookup the lifecycle mapping from different places.

Both antlr4-maven-plugin and jaxb2-maven-plugin provide m2e lifecycle-mapping-metadata in the plugin itself, that's why vscode-java can handle them well.

I summarized the possible solutions for such kind of issue:

getaceres commented 2 years ago

I've tried adding the workaround found in the first post but I still can't get VSCode to recognize the classes generated by the swagger generation plugin. My sources are generated at target/generated-sources/swagger/src so I've added this configuration to my POM:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <executions>
    <execution>
       <!-- Need to ensure the generated source folder is added to the project classpath, in jdt.ls -->
       <id>add-source</id>
       <phase>generate-sources</phase>
       <goals>
         <goal>add-source</goal>
       </goals>
       <configuration>
         <sources>
           <source>target/generated-sources/swagger/src</source>
         </sources>
        </configuration>
       </execution>
     </executions>
 </plugin>

Still, the classes generated by swagger are not visible in VSCode and marked as errors and I don't see the source folder added to the Maven panel.

image

And in the project configuration:

image

Do I need to add anything else to the POM to get VSCode to recognize the swagger generation folder?

chaslain commented 1 year ago

Has this still never been addressed?

getaceres commented 1 year ago

No. Auto generated code is not recognized by the VS Code Java plugin and there isn't any way to edit the classpath manually. So if you need auto generated code in your Maven project, IntelliJ or Eclipse are your only choices (I don't know if Netbeans supports auto generated code or not)