Skip to content

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

Description

@jfcervera

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

Activity

  1. christarczon commented on Jun 19, 2019

    @christarczon

    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.

  2. bbeaudreault commented on Jun 20, 2019

    @bbeaudreault

    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

  3. bbeaudreault commented on Jun 20, 2019

    @bbeaudreault

    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.

  4. Deathhush commented on Dec 25, 2019

    @Deathhush

    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.

  5. testforstephen commented on Aug 12, 2020

    @testforstephen
    Collaborator

    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?

  6. fbricon commented on Aug 12, 2020

    @fbricon
    Collaborator

    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

  7. testforstephen commented on Aug 13, 2020

    @testforstephen
    Collaborator

    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:

    • Ask the corresponding maven plugin to provide embedded lifecycle-mapping-metadata.xml.

    • Configure the lifecycle-mapping-metadata for the maven plugins you used in your project's pom.xml directly.

      • Option 1: Add a dedicated <pluginManagement> section to configure m2e lifecycle mapping.
      <build>
        <pluginManagement>
          <plugins>
            <plugin>
              <groupId>org.eclipse.m2e</groupId>
              <artifactId>lifecycle-mapping</artifactId>
              <version>1.0.0</version>
              <configuration>
                <lifecycleMappingMetadata>
                  <pluginExecutions>
                    <pluginExecution>
                      <pluginExecutionFilter>
                        <groupId>org.antlr</groupId>
                        <artifactId>antlr3-maven-plugin</artifactId>
                        <versionRange>[3.5.2,)</versionRange>
                        <goals>
                          <goal>antlr</goal>
                        </goals>
                      </pluginExecutionFilter>
                      <action>
                        <execute>
                          <runOnIncremental>true</runOnIncremental>
                          <runOnConfiguration>true</runOnConfiguration>
                        </execute>
                      </action>
                    </pluginExecution>
                  </pluginExecutions>
                </lifecycleMappingMetadata>
              </configuration>
            </plugin>
          </plugins>
        </pluginManagement>
      </build>
      <build>
        <plugins>
          <plugin>
              <groupId>org.antlr</groupId>
              <artifactId>antlr3-maven-plugin</artifactId>
              <version>3.5.2</version>
              <configuration>
                <sourceDirectory>src</sourceDirectory>
                <libDirectory>src/org/stringtemplate/v4/compiler</libDirectory>
                <verbose>true</verbose>
              </configuration>
              <executions>
                <execution>
                  <?m2e execute onConfiguration,onIncremental?> // Configure an inline m2e lifecycle mapping metadata.
                  <goals>
                    <goal>antlr</goal>
                  </goals>
                </execution>
              </executions>
            </plugin>
        </plugins>
      </build>
    • Allow to set a global lifecycle-mapping-metadata.xml per machine, see a sample. This requires vscode-java to expose a new user settings for that. //cc @fbricon
      image

    • Use build-helper-maven-plugin mentioned above to explicitly mark as source folder. The downside is that m2e auto build does not automatically generate generate-sources folders, you still need to run mvn cli to generate the classes.

  8. getaceres commented on Jul 13, 2022

    @getaceres

    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?

  9. chaslain commented on Aug 13, 2023

    @chaslain

    Has this still never been addressed?

  10. getaceres commented on Aug 21, 2023

    @getaceres

    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)

  11. SAprelov commented on Nov 10, 2025

    @SAprelov

    It works now, but only with plugins

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions