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

generated-sources/java is not picked up as a source path #1639

Open
gayanper opened this issue Sep 29, 2020 · 30 comments
Open

generated-sources/java is not picked up as a source path #1639

gayanper opened this issue Sep 29, 2020 · 30 comments

Comments

@gayanper
Copy link
Contributor

When importing a maven multi module which has modules which contains generated-sources/java the generated-sources/java folder is not added into the source paths. Only the generated-sources/annotations is added.

@snjeza
Copy link
Contributor

snjeza commented Sep 29, 2020

@gayanper could you attach a project example?

@testforstephen
Copy link
Collaborator

Could you share which maven plugin is generating the generated-sources/java folder?

Usually you need to configure the lifecycle-mapping-metadata for the source generation maven plugins you used in your project's pom.xml additionally.

#177 (comment) shared several workarounds to handle the extra classpath from generated source.

@gayanper
Copy link
Contributor Author

@gayanper could you attach a project example?

Unfortunately i cannot share the project since its a proprietary codebase.

@gayanper
Copy link
Contributor Author

Could you share which maven plugin is generating the generated-sources/java folder?

Usually you need to configure the lifecycle-mapping-metadata for the source generation maven plugins you used in your project's pom.xml additionally.

#177 (comment) shared several workarounds to handle the extra classpath from generated source.

The plugin is a proprietary plugin. The generation and source compilation happens without issues in maven build. The source folders are not detected in vscode. This was a problem in eclipse as well. For eclipse i wrote a m2e extension to handle the source folders.

@gayanper
Copy link
Contributor Author

Is there a way i can add my eclipse plugins into vscode java ls plugins folder and make it detect my source folders.

@gayanper
Copy link
Contributor Author

Got my plugin to work with hacking the config.ini adding my plugins into bundles. May be it would be nice if can add support for non ui plugins when we need to extend ls with private plugins which cannot be bundled by default

@fbricon
Copy link
Collaborator

fbricon commented Sep 30, 2020

you can create a small vscode extension hosting your plugins (provided it doesn't have any Eclipse UI dependencies): https://github.com/redhat-developer/vscode-java/wiki/Contribute-a-Java-Extension#modify-packagejson

@DerekBennett
Copy link

DerekBennett commented Aug 6, 2021

I am having the exact same problem. I have a custom maven plugin that is generating source code to target/generated-sources/some-proprietary-directory for several modules in a multi-module Maven project. The Maven build succeeds. The VS Code "vscjava.vscode-java-pack" extension does not see these generated sources, thus making my development experience broken and unpleasant. I do not understand the above suggested solutions. Could you please detail how I should change my project to make VS Code comprehend it properly?

@rgrunber
Copy link
Member

rgrunber commented Aug 13, 2021

I would look at #177 (comment) as that issue has a pretty comprehensive guide on the options.

Generally build-helper-maven-plugin seems to be what is working for most people.

I just tried a basic Maven project with a a folder target/generated-sources/my-extra-dir/ containing a source file in VS Code. I confirmed the sources were not on the classpath/accessible from the source files that were. I then added the code below to my build lifecycle.

<plugin>
    <!-- a hint for IDE's to add the java sources to the classpath -->
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
            <id>01-add-test-sources</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>${project.build.directory}/generated-sources/my-extra-dir/</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

Just a simple save in VS Code, and once the project rebuilt, I could confirm the my-extra-dir sources ended up in the .classpath and were accessible from the other source files.

@HerrDerb
Copy link

HerrDerb commented Sep 3, 2021

Sadly it does not even work with the build-helper-maven-plugin plugin. VSC refuses to see the generated sources and won't compile.
In my case I have a nested Project
pom
|
|--pom <-- not adding the generated sources
|
|--pom

@jackwener
Copy link

jackwener commented Apr 10, 2022

Sadly it does not even work with the build-helper-maven-plugin plugin. VSC refuses to see the generated sources and won't compile. In my case I have a nested Project pom | |--pom <-- not adding the generated sources | |--pom

I also meet this problem.

@csirvents
Copy link

csirvents commented Aug 22, 2022

Add the following line <?m2e execute onConfiguration,onIncremental?> inside the build plugin.

Example:

<plugin>
                    <groupId>org.openapitools</groupId>
                    <artifactId>openapi-generator-maven-plugin</artifactId>
                    <version>${openapi-generator-maven-plugin.version}</version>
                    <?m2e execute onConfiguration,onIncremental?>

@manuelserradev
Copy link

I'd like to point out to a simple repro project with jpa-streamer as a sources generator here.

Triggering a full compilation results in target/generated-sources/annotations/<entity>$ to be correctly generated but the sources cannot be referenced in user code (needed in this example for type safe queries, see src/main/java/com/speedment/jpastreamer/demo/quarkus/hibernate/panache/repository/FilmRepository.java).

Compiling and running the application with the maven CLI results in no problems nor warnings.

@snjeza
Copy link
Contributor

snjeza commented Feb 4, 2023

I'd like to point out to a simple repro project with jpa-streamer as a sources generator here.
Triggering a full compilation results in target/generated-sources/annotations/$ to be correctly generated but the sources cannot be referenced in user code (needed in this example for type safe queries, see src/main/java/com/speedment/jpastreamer/demo/quarkus/hibernate/panache/repository/FilmRepository.java).
Compiling and running the application with the maven CLI results in no problems nor warnings.

A related issue - eclipse-jdt/eclipse.jdt.core#670

@thre3eye
Copy link

thre3eye commented Mar 3, 2023

Hello -

A major project has been working for years with generated sources using org.codehaus.mojobuild-helper-maven-plugin.

However, I worked on something else for a couple of weeks (and updated VS Code and related Java extensions in the meantime) and now things are broken and generated sources doesn't get picked up any more. I have not yet dug into the issue but it would seem either VS Code or some extension have introduced the problem. Will update when I find out more.

Thanks.

@atanasi
Copy link

atanasi commented Mar 8, 2023

However, I worked on something else for a couple of weeks (and updated VS Code and related Java extensions in the meantime) and now things are broken and generated sources doesn't get picked up any more. I have not yet dug into the issue but it would seem either VS Code or some extension have introduced the problem. Will update when I find out more.

Updating build-helper-maven-plugin to the latest version (3.3.0) solved this issue for me.

@2m
Copy link

2m commented May 11, 2023

We also had a similar problem, where generated sources by buf.build were not visible in VS Code, but it was fine when running mvn compile. Turns out we had pointed build-helper-maven-plugin one folder too shallow. Maven probably looks in the subdirectories as well, but VS Code does not. This has fixed it:

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>3.3.0</version>
        <executions>
          <execution>
            <id>add-source-generated</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>add-source</goal>
             </goals>
             <configuration>
               <sources>
-                <source>${basedir}/target/generated-sources</source>
+                <source>${basedir}/target/generated-sources/protobuf</source>
               </sources>
             </configuration>
           </execution>

@getaceres
Copy link

I have several projects using the code generator from swagger plugin:

<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>

It generates code into target/generated-sources/swagger/src/gen/java/main

The projects compile in Maven without any problem and I don't have to add any build-helper-maven-plugin. IntelliJ also recognizes this source folders and I don't have to add anything to the POM file. However, VS Code does not recognize it as a source folder.
I've added the configuration

<plugin>
        <!-- a hint for IDE's to add the java sources to the classpath -->
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>3.3.0</version>
        <executions>
          <execution>
            <id>01-add-test-sources</id>
            <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>

to the POM file as suggested here but still I can't get this folder added to the classpath when I look into the Java Projects panel and any file using classes from this folder is still marked as having compilation errors.

@snjeza
Copy link
Contributor

snjeza commented May 18, 2023

@getaceres Could you show your .classpath file?

@getaceres
Copy link

I can't find a .classpath file in my project but I've found a Classpath Configuration screen and this is the part of the source folders:

image

@snjeza
Copy link
Contributor

snjeza commented May 18, 2023

I can't find a .classpath file in my project

@getaceres You can add

"java.import.generatesMetadataFilesAtProjectRoot": true,

@getaceres
Copy link

I've added that but I still don't get a .classpath file. Instead now I have a bin folder in which the whole project is copied and the project doesn't finish loading.

@snjeza
Copy link
Contributor

snjeza commented May 18, 2023

Could you try to clean the workspace?

  • Open the command palette (F1)
  • select Java: Clean the Java Language Server Workspace
  • select Restart and delete from the confirmation prompt

@getaceres
Copy link

I tried that. Still the same. I don't get a .classpath file but I get a bin folder that gets filled with the same files I have in my project and it never finishes loading until I get this error:

image

@snjeza
Copy link
Contributor

snjeza commented May 18, 2023

Could you try to set

"java.jdt.ls.vmargs": "-Daether.dependencyCollector.impl=df -Dlog.level=ALL -Djdt.ls.debug=true  -XX:+UseParallelGC -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -Dsun.zip.disableMemoryMapping=true -Xmx6G -Xms512m -Xlog:disable",

@getaceres
Copy link

getaceres commented May 25, 2023

Sorry for the delay. I tried adding that. Still the same. The moment I add the build-helper-maven-plugin it starts generating the /bin folder and when I add "java.import.generatesMetadataFilesAtProjectRoot": true, it never finishes loading the project and I still don't get a .classpath file. What's more, even after deleting this line, the project doesn't load anymore.

@dnhuan
Copy link

dnhuan commented Jun 14, 2023

I have several projects using the code generator from swagger plugin:

<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>

It generates code into target/generated-sources/swagger/src/gen/java/main

The projects compile in Maven without any problem and I don't have to add any build-helper-maven-plugin. IntelliJ also recognizes this source folders and I don't have to add anything to the POM file. However, VS Code does not recognize it as a source folder. I've added the configuration

<plugin>
        <!-- a hint for IDE's to add the java sources to the classpath -->
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>3.3.0</version>
        <executions>
          <execution>
            <id>01-add-test-sources</id>
            <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>

to the POM file as suggested here but still I can't get this folder added to the classpath when I look into the Java Projects panel and any file using classes from this folder is still marked as having compilation errors.

Instead of specifying the <source/> directory, adding <?m2e execute onConfiguration?> right under <execution> in pom.xml will trigger the language server to recognize the generated source files.

image

@getaceres
Copy link

<plugin>
        <!-- a hint for IDE's to add the java sources to the classpath -->
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>3.3.0</version>
        <executions>
          <execution>
            <?m2e execute onConfiguration?>
            <id>01-add-test-sources</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>add-source</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

Like this?

@nnikolay
Copy link

Hi, I have the same issue the moment I am building SAP CAP Java project with VS code. To not copy the whole post I am linking to stackoverflow.

https://stackoverflow.com/questions/78467461/cap-java-application-in-vs-code-generated-import-not-found

Someone has idea?

Many thanks!

@iookpareke
Copy link

iookpareke commented Nov 27, 2024

Add the following line <?m2e execute onConfiguration,onIncremental?> inside the build plugin.

Example:

<plugin>
                    <groupId>org.openapitools</groupId>
                    <artifactId>openapi-generator-maven-plugin</artifactId>
                    <version>${openapi-generator-maven-plugin.version}</version>
                    <?m2e execute onConfiguration,onIncremental?>

Thanks a lot, This is the fix that worked for me. However it only worked for me in VS code by breaking up onConfiguration and onIncremental, like so...

 <plugin>
   		 <groupId>io.swagger.codegen.v3</groupId>
    		<artifactId>swagger-codegen-maven-plugin</artifactId>
   		<version>3.0.64</version>
		<?m2e execute onConfiguration?>
		<?m2e execute onIncremental?>
...
 <\plugin>

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