How do I get IntelliJ to recognize gradle generated sources dir?


(Jim C.) #1

I have a bug to report regarding the idea plug-in.

Go here:
https://docs.gradle.org/current/dsl/org.gradle.plugins.ide.idea.model.IdeaModule.html
Search the page for: generatedSourceDirs and you wont find it in the properties box towards the bottom. Also it doesn’t seem to work. Suspect it got left out somehow.


Idiomatic way to generate source code that works in IDEs automatically without using specific IDE Gradle plugin
How to report a bug?
(Sterling Greene) #2

generatedSourceDirs was missing from the DSL guide. If you take a look at the Groovydoc, it’s there.

Have you tried adding your generated source to the main sourceSet along with marking the directory as generated?

e.g.,

sourceSets.main.java.srcDir new File(buildDir, "generated-src/java")

(Jim C.) #3

If you are going to add them to the main sourceSet, then would you even need the idea plug-in? I’ll give it a whirl and report back.


(Jim C.) #4

Well it appears I was correct. The icon indicates that “generated-src/java” is being picked up as a regular source set. That’s a blue square icon. When you mark it as generated, there’s an asterisk. Also shows as regular source, rather than generated, in the module settings. Something would appear to be amiss.


(Daniel Dekany) #5

This worked for me (Gradle 3.5, IDEA 2017.1.2, and the code generator happens to be javacc, but this last certainly doesn’t mater):

apply plugin: "idea"
...
sourceSets.main.java.srcDir new File(buildDir, 'generated/javacc')
idea {
    module {
        // Marks the already(!) added srcDir as "generated"
        generatedSourceDirs += file('build/generated/javacc')
    }
}

If I don’t use generatedSourceDirs then the directory will be still marked as source directory in IDEA, but it won’t be flagged as “generated sources root”. So you need to use both.

BTW, from the command line the build works even without adding the directory to the sourceSet manually, which is strange. If Gradle knew that there are sources there (or else the build should have failed), why is it not automatically in the sourceSet? Maybe just some glitch specific to the JavaCC plugin, I don’t know…