Unable to use Intellij with a generated sources folder
Asked Answered
M

14

151

Related question How to configure IntelliJ IDEA and/or Maven to automatically add directories with Java source code generated using jaxb2-maven-plugin?

I have a custom plugin that generates sources under target/generated-sources (Note no toolname here). So I get sources like target/generated-sources/com/mycompany...etc.

This format cannot be changed at all, so will I be able to configure Intellij into adding it as a source folder. As of now, I can see that Intellij has added target/generated-sources/com as the source folder.

Please note that I do not have the option of configuring the plugin !

UPDATE 1: I do not agree with the fact that I MUST put my generated sources under a tool name folder. It may be a good convention, but if I have only one generator there is no need for me to put it there? Again, in my pom.xml I have a resources section which clearly indicates that target/generated-sources should be treated as a source folder. This works perfectly fine in Eclipse so I have no idea why Intellij would not respect my settings.

TL;DR -> When I put target/generated-sources in the resource section of pom.xml why is Intellij overzealous to add target/generated-sources/com to the classpath?

Malchus answered 2/3, 2011 at 16:46 Comment(3)
Take a look at #10735880Presbyter
Try this solution, it may resolve your issue. click here for the solutionAbbottson
Try this solution, it may resolve your issue. click here for the solutionAbbottson
F
200

You can just change the project structure to add that folder as a "source" directory.

Project Structure → Modules → Click the generated-sources folder and make it a sources folder.

Or:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.4</version>
    <executions>
        <execution>
            <id>test</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>${basedir}/target/generated-sources</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>
Freud answered 2/3, 2011 at 17:20 Comment(14)
This option sounds interesting. I would lose this setting everytime I run "mvn idea:idea". Is there a way to declare a maven idea plugin with this "workaround"?Malchus
You can't add the generated sources folder if it under build because IDEA excludes the build folder.Calabria
@Calabria you can't manually, yes. But if you delete your module and then reimport it, (with this plugin set up), idea recognizes everything correctly.Kisner
This should be and usually is automatically done by Idea. Rebuilding project may reset the source status back to the problematic state.Bloomer
You can also right click any folder and use "mark directory as"Nacre
My problem was some of the files only, and it turns out the files were bigger than what IntelliJ appreciated. I could fix it by increasing that limit like explained here: https://mcmap.net/q/55291/-file-size-exceeds-configured-limit-2560000-code-insight-features-not-availableRoselynroseman
Although my pom.xml was contain this snippet, IDEA didn't see the generated sources. Running mvn idea:idea solved the issue in my case. For records.Incommodity
how to do the same with gradleGreenbelt
Solved the problem, For readers out there, go for recent version: 3.2.0Wendalyn
@PieterDeBie do you know any way to automatize this, I have several generated-sources on several projects and can't be modifying poms and doing "mark as generated sources" manually takes a few hours every time...Hamford
Weird things happen if you add a generated source folder as a source for your project. Intellij then tries to compile that class and your project then doesn't start if that source file is dependent on other source files which you don't have.Elkeelkhound
@ArsalanSiddiqui How can you compile ANY source files if you don't have all of the dependencies?Freud
@Freud So the only thing missing in this answer is that we do not need to explicitly make the "generated-sources" folder as a "source" folder. Intellij will automatically detect it and compile using all the code in it.Elkeelkhound
@Freud Why not do this with maven-compiler-plugin? pom.xml: plugin>maven-compiler-plugin>configuration>generatedSourcesDirectory>${project.build.directory}/generated-sourcesTherein
A
293

I'm using Maven (SpringBoot application) solution is:

  1. Right click project folder
  2. Select Maven
  3. Select Generate Sources And Update Folders

Then, Intellij automatically import generated sources to project.

Accordion answered 18/10, 2017 at 14:28 Comment(6)
Works without messing up the folder structure yourself!Tarrasa
This answer deserves to be higher - much more convenient than the accepted answerAyesha
Notice that in the latest version of Intellij 2019.1, you can click the second button in the maven menu (on the right, expand the maven sidebar, on the top, second button)Chauvinism
I also had to check my maven settings.xml that was pointing to the wrong not default repoRosierosily
Works like charm!Yonita
The word "automatically" imply that Intellj do it without user interaction. This isn't true. There is always the need to click the "Generate Sources And Update Folders" button each time IntelliJ decide to wipe the sources or/and resources. Wiping them work automatically, but reimporting the project not. This behavior is really annoying.Cultch
F
200

You can just change the project structure to add that folder as a "source" directory.

Project Structure → Modules → Click the generated-sources folder and make it a sources folder.

Or:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.4</version>
    <executions>
        <execution>
            <id>test</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>${basedir}/target/generated-sources</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>
Freud answered 2/3, 2011 at 17:20 Comment(14)
This option sounds interesting. I would lose this setting everytime I run "mvn idea:idea". Is there a way to declare a maven idea plugin with this "workaround"?Malchus
You can't add the generated sources folder if it under build because IDEA excludes the build folder.Calabria
@Calabria you can't manually, yes. But if you delete your module and then reimport it, (with this plugin set up), idea recognizes everything correctly.Kisner
This should be and usually is automatically done by Idea. Rebuilding project may reset the source status back to the problematic state.Bloomer
You can also right click any folder and use "mark directory as"Nacre
My problem was some of the files only, and it turns out the files were bigger than what IntelliJ appreciated. I could fix it by increasing that limit like explained here: https://mcmap.net/q/55291/-file-size-exceeds-configured-limit-2560000-code-insight-features-not-availableRoselynroseman
Although my pom.xml was contain this snippet, IDEA didn't see the generated sources. Running mvn idea:idea solved the issue in my case. For records.Incommodity
how to do the same with gradleGreenbelt
Solved the problem, For readers out there, go for recent version: 3.2.0Wendalyn
@PieterDeBie do you know any way to automatize this, I have several generated-sources on several projects and can't be modifying poms and doing "mark as generated sources" manually takes a few hours every time...Hamford
Weird things happen if you add a generated source folder as a source for your project. Intellij then tries to compile that class and your project then doesn't start if that source file is dependent on other source files which you don't have.Elkeelkhound
@ArsalanSiddiqui How can you compile ANY source files if you don't have all of the dependencies?Freud
@Freud So the only thing missing in this answer is that we do not need to explicitly make the "generated-sources" folder as a "source" folder. Intellij will automatically detect it and compile using all the code in it.Elkeelkhound
@Freud Why not do this with maven-compiler-plugin? pom.xml: plugin>maven-compiler-plugin>configuration>generatedSourcesDirectory>${project.build.directory}/generated-sourcesTherein
W
33

With gradle, the project settings will be cleared whenever you refresh the gradle settings. Instead you need to add the following lines (or similar) in your build.gradle, I'm using kotlin so:

sourceSets {
    main {
        java {
            srcDir "${buildDir.absolutePath}/generated/source/kapt/main"
        }
    }
}
Wyler answered 30/11, 2016 at 14:8 Comment(2)
This worked for me for both IntelliJ's project import and gradle idea. Thanks! :)Ithnan
This only works if I don't use annotationProcessor in my dependencies.Arable
H
26

Its very simple:

Just right click on the directory and mark it as generated sources root. See the below screenshot.

enter image description here

Hooded answered 1/7, 2022 at 7:19 Comment(2)
Which directory? (to click)Lawyer
@Lawyer Once you have a generated source folder, right click on it and then select "Mark Directory As" --> "Generated Sources Root". This solution worked for me.Elkeelkhound
G
21

The fix

Go to Project Structure - Modules - Source Folders and find the target/generated-sources/antlr4/com/mycompany - click Edit properties and set Package prefix to com.mycompany.

This is exactly the reason why we can set Package prefix on source dirs.


Different but related problem here

Guaiacum answered 11/3, 2016 at 18:11 Comment(4)
This fix is the only one that's worked for me in DAYS of looking. Thanks!Gillespie
Oh thanks, I just updated intellij and they've changed their icons. I didn't thought that the orange folder would mean that it's not a source.Nacre
half a day I have been looking for this. Worked for me with antlr4. Thanks!Malva
For ANTLR >=4.5.3 (maybe earlier, too), as long as my grammar was located in src/main/antlr4/<path-to-package>/, then it would appear in target/generated-sources/antlr4/<path-to-package>. IntelliJ automatically marked the correct directory as a sources root and no package prefix was necessary. I suspect this fix is needed when people place their grammars directly in src/main/antlr4, which is often recommended for some reason.Card
H
10

Solved it by removing the "Excluded" in the module setting (right click on project, "Open module settings"). enter image description here

Holoenzyme answered 20/2, 2020 at 8:21 Comment(0)
I
7

Whoever wrote that plugin screwed up big time. That's not the way to do it!

Any workaround would be a huge hack, make the plugin developer aware of his bug.

Sorry, that's the only thing to do.


OK here's a hack, directly after your plugin's execution, use the antrun plugin to move the directory somewhere else:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.6</version>
    <executions>
      <execution>
        <phase>process-sources</phase>
        <configuration>
          <target>
            <move todir="${project.build.directory}/generated-sources/toolname/com"
                  overwrite="true">
                <fileset dir="${project.build.directory}/generated-sources/com"/>
            </move>
          </target>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
</plugin>

In this example, toolname should be replaced by anything that uniquely identifies the plugin that created the code and com stands for the root of the created packages. If you have multiple package roots, you probably need multiple <move> tasks.

But if the plugin adds the folder as source folder, then you're screwed.

Incogitant answered 2/3, 2011 at 16:58 Comment(2)
Thanks Sean. I actually observed that Intellij adds both target/generated-sources/com as well as target/generated-sources (this is added as a source directory explicitly in "resource" section). Is there any other way to prevent the first thing from happening. If No, then I will try your workaround.Malchus
Yes, the author should have use target/generated-sources/my-tool.Bloomer
J
7

For someone still struggle to fix the generated source not picking up by IntelliJ,

One reason could be the generated file size too big for it to load, then you need to put following line into your "custom IntelliJ IDEA properties" (under menu help)

idea.max.intellisense.filesize=7500
Joyance answered 28/5, 2021 at 12:48 Comment(0)
D
3

i ran mvn generate-resources and then marked the /target/generated-resources folder as "sources" (Project Structure -> Project Settings -> Modules -> Select /target/generated-resources -> Click on blue "Sources" icon.

Dada answered 27/11, 2020 at 11:20 Comment(1)
that is not a very persistent way of doing it.Muffler
H
2

I had the same issue with Eclipse a couple of months ago when importing my project. Now I had the same with intelliJ. Here is how someone helped me to solve this in IntelliJ:

Menu => View => Tools windows => Maven Project In the spring_user value => Run Configuration, choose clean install. This should do a clean install and after this you should be able to see the classes enter image description here

Haematogenesis answered 24/7, 2018 at 10:22 Comment(0)
D
2

Automatically mark directory as a generated source  When you run mvn clean install, if you want to mark directory as a generated sources you can use this solution

Dardan answered 28/6, 2022 at 13:31 Comment(0)
U
0

Maybe you can add a step to the generate-sources phase that moves the folder?

Uproar answered 2/3, 2011 at 16:59 Comment(0)
R
0

The only working condition, after several attempts, was to remove the hidden .idea folder from the root project folder and re-import it from Intellij

Rosierosily answered 29/1, 2020 at 8:54 Comment(2)
solution doesn't lastLaundryman
for me that worked, together with the build-helperBarley
T
-1

I wanted to update at the comment earlier made by DaShaun, but as it is my first time commenting, application didn't allow me.

Nonetheless, I am using eclipse and after I added the below mention code snippet to my pom.xml as suggested by Dashun and I ran the mvn clean package to generate the avro source files, but I was still getting compilation error in the workspace.

I right clicked on project_name -> maven -> update project and updated the project, which added the target/generated-sources as a source folder to my eclipse project.

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.4</version>
    <executions>
        <execution>
            <id>test</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>${basedir}/target/generated-sources</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>
Thankyou answered 26/9, 2020 at 19:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.