Intellij
IDE
generated sources
development
troubleshooting

IntelliJ not recognizing a generated sources folder: how to fix it

Codemia Knowledge Hub ·

If IntelliJ IDEA ignores your generated sources folder, mark it as a source folder (Mark Directory as > Generated Sources Root in Project Structure), make sure Maven or Gradle writes the generated code to that location, and then reload and rebuild the project. If references are still unresolved, reimport the project or invalidate caches.

Generated sources are files produced by tools such as the Maven build or annotation processors. Common reasons for using them:

  • Code generation: JPA annotation processors or AutoValue generate code to reduce boilerplate.
  • Testing: test suite configurations can generate mock classes.
  • Resource bundling: build tools package multiple resources together.

When the IDE is not configured for them, you get unresolved references or missing classes.

Why IntelliJ misses generated sources

  1. Incorrect configuration: IntelliJ does not recognize some folders as source directories if the project configuration is wrong.
  2. Build differences: the build may succeed on the command line but not in the IDE.
  3. Indexing problems: IntelliJ may not index the generated files, which breaks autocompletion and analysis.

Configuring IntelliJ to use generated sources

Step 1: Mark the folder

Tell IntelliJ that the folder contains source files:

  1. Open Project Structure:
    • Go to File > Project Structure.
  2. Mark the directory:
    • Open Modules and select the module that contains the generated sources.
    • In the Sources tab, find the generated folder in the directory tree.
    • Right-click it and select Mark Directory as > Generated Sources Root.

Step 2: Configure the build tool

With Maven or Gradle, generate sources into a location IntelliJ recognizes:

  • Maven:
    • Configure the plugin correctly in pom.xml, for example:
xml
1    <build>
2      <plugins>
3        <plugin>
4          <groupId>org.apache.maven.plugins</groupId>
5          <artifactId>maven-compiler-plugin</artifactId>
6          <version>3.8.1</version>
7          <configuration>
8            <source>1.8</source>
9            <target>1.8</target>
10            <generatedSourcesDirectory>${project.build.directory}/generated-sources</generatedSourcesDirectory>
11          </configuration>
12        </plugin>
13      </plugins>
14    </build>
  • Gradle:
    • Add the generated sources path in build.gradle:
gradle
    sourceSets.main.java.srcDirs += 'build/generated/sources'

Step 3: Refresh the IDE

Force IntelliJ to pick up the new sources:

  • Run Build > Rebuild Project.
  • Right-click pom.xml or build.gradle and select Reload.

If the sources are still not detected

  • Reimport the project: a full reimport as a Maven or Gradle project sometimes fixes it.
  • Invalidate caches: go to File > Invalidate Caches / Restart… and select Invalidate and Restart.
  • Check output paths: make sure the build's output path matches the structure IntelliJ expects.
  • Read the error messages: the IDE's Event Log or Build Output gives detailed information about specific errors.
Issue/TaskSolution/Configuration
Recognizing Generated SourcesMark Directory as Generated Sources Root in Project Structure.
Maven ConfigurationAdd <generatedSourcesDirectory> in pom.xml.
Gradle ConfigurationModify sourceSets.main.java.srcDirs in build.gradle.
IntelliJ Not Recognizing ChangesRebuild Project, Reload Maven/Gradle, or Reimport Project.
Unresolved References or CompilationCheck for correct folder marking and plugin errors in the build tool.
IDE Indexing IssuesInvalidate Caches and Restart IntelliJ, then Rebuild Project.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Interview Questions practice on Codemia

Over 11,000 interview practice questions for 300+ companies, searchable by company and role.

Browse interview questions