Unable to use Intellij with a generated sources folder

JavaMaven 2Intellij Idea

Java Problem Overview


Related question https://stackoverflow.com/questions/2644609/how-to-configure-intellij-idea-and-or-maven-to-automatically-add-directories-with

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?

Java Solutions


Solution 1 - Java

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>

Solution 2 - Java

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.

Solution 3 - Java

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"
        }
    }
}

Solution 4 - Java

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

Solution 5 - Java

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

Solution 6 - Java

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.

Solution 7 - Java

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

Solution 8 - Java

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.

Solution 9 - Java

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

Solution 10 - Java

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

Solution 11 - Java

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

Solution 12 - Java

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>

Solution 13 - Java

I wanted to ask that sometimes even if the pom is correct, Intellij does not see the directory. One way to solve this problem is to close the project, remove the .idea directory and reopen the project. The 'generated-sources' is added to the classpath (the directory is in blue (it was in red) and we see a star ('*') on the left).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKannan EkanathView Question on Stackoverflow
Solution 1 - JavaDaShaunView Answer on Stackoverflow
Solution 2 - JavaMehmet HanoğluView Answer on Stackoverflow
Solution 3 - JavazhywuView Answer on Stackoverflow
Solution 4 - JavaVlastimil OvčáčíkView Answer on Stackoverflow
Solution 5 - JavaJ.PView Answer on Stackoverflow
Solution 6 - JavaSean Patrick FloydView Answer on Stackoverflow
Solution 7 - JavaSebastianXView Answer on Stackoverflow
Solution 8 - JavaPreeti JoshiView Answer on Stackoverflow
Solution 9 - JavaChristoffer HammarströmView Answer on Stackoverflow
Solution 10 - Javauser9934722View Answer on Stackoverflow
Solution 11 - JavaJonathanView Answer on Stackoverflow
Solution 12 - JavaanupamView Answer on Stackoverflow
Solution 13 - JavaRudy VissersView Answer on Stackoverflow