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

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…

1 Like