Unable to use Intellij with a generated sources folder
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When working with Java projects in IntelliJ IDEA, developers often deal with generated sources. These are files produced by tools like the Maven build system or annotation processors. However, integrating these generated sources into the IntelliJ project workflow sometimes poses challenges. This article explores the common issues, provides technical explanations, offers solutions, and includes helpful examples to effectively utilize IntelliJ IDEA with a generated sources folder.
Understanding the Problem
Why Use Generated Sources?
Generated sources are common in Java development for several reasons:
- Code Generation: Tools like JPA annotation processors or
AutoValuecan automatically generate code to reduce boilerplate. - Testing: Test suite configurations can generate mock classes.
- Resource Bundling: Build tools package multiple resources together.
These generated files might not be immediately usable by your code editor due to configuration issues, causing unresolved references or missing classes.
Common Challenges
- Incorrect Configuration: IntelliJ doesn't automatically recognize certain folders as source directories if the project configuration isn't correct.
- Build Execution Issues: The build might be successful in the command line but not in the IDE.
- Indexing Problems: IntelliJ might not index the generated files, causing autocompletion or analysis issues.
Configuring IntelliJ to Use Generated Sources
Step-by-Step Guide
Step 1: Recognize the Folder
First, you need to tell IntelliJ that a folder contains source files:
- Open the Project Structure:
- Go to
File > Project Structure.
- Mark Directory as Sources:
- Navigate to
Modules, then select the module that contains the generated sources. - In the
Sourcestab, find your generated folder in the directory structure. - Right-click on the folder and select
Mark Directory as > Generated Sources Root.
Step 2: Configure Build Tools
If you are using Maven or Gradle, configure them to generate sources in a location recognized by IntelliJ:
- Maven:
- Ensure that your
pom.xmlis configured correctly with the plugin. - A typical configuration could look like this:
- Gradle:
- Update
build.gradleto include the path for generated sources:
Step 3: Trigger IDE Refresh
Force IntelliJ to refresh and recognize the new sources:
- Run
Build > Rebuild Project. - Right-click on
pom.xmlorbuild.gradleand selectReload.
Troubleshooting Common Issues
Problem: Sources Still Not Detected
- Reimport the Project: Sometimes a full reimport of the project as a Maven or Gradle project fixes the issue.
- Invalidate Caches: Go to
File > Invalidate Caches / Restart…, and selectInvalidate and Restart. - Check the Output Paths: Ensure your build’s output path matches IntelliJ's expected structure.
Problem: IDE Error Messages
Analyze any specific error messages from the IDE’s Event Log or Build Output to obtain detailed information.
Summary Table
Here’s a concise recap of the solutions discussed:
| Issue/Task | Solution/Configuration |
| Recognizing Generated Sources | Mark Directory as Generated Sources Root in Project Structure. |
| Maven Configuration | Add <generatedSourcesDirectory> in pom.xml. |
| Gradle Configuration | Modify sourceSets.main.java.srcDirs in build.gradle. |
| IntelliJ Not Recognizing Changes | Rebuild Project, Reload Maven/Gradle, or Reimport Project. |
| Unresolved References or Compilation | Check for correct folder marking and plugin errors in the build tool. |
| IDE Indexing Issues | Invalidate Caches and Restart IntelliJ, then Rebuild Project. |
Conclusion
By understanding and properly configuring IntelliJ IDEA to work with generated sources, developers can streamline their development process, reducing the need for manual interventions and improving overall productivity. Ensuring that your IDE context is correctly set up will minimize issues, and the solutions presented allow for smoother project building and management.
Incorporating these changes can fundamentally improve how IntelliJ IDEA handles generated sources, leading to a more efficient and less error-prone development environment.

