Working with source generated by an annotation processor

I’m using an annotation processor that creates Java source. Here it is QueryDSL, but I guess it doesn’t matter what annotation processor is used.

I have mapped a custom source set to a module directory src/main/generated/annotationProcessor/java that should be recognized by IDEA as source folder.

String generatedSourcesPath = 'src/main/generated/annotationProcessor/java'

sourceSets {
    generatedSources {
        java {
            srcDirs generatedSourcesPath

idea {
    module {
        generatedSourceDirs += file (generatedSourcesPath)

compileJava { { srcDir generatedSourcesPath }
    File generatedSourceDir = project.file(generatedSourcesPath)
    options.annotationProcessorGeneratedSourcesDirectory = generatedSourceDir

clean {
    delete generatedSourcesPath

Everything works nicely after importing the Gradle project into IDEA. The default of IDEA for the setting of Preferences | Build, Execution, Deployment | Build Tools | Gradle seems to be to use Gradle for building and running.

However as I switch to IDEA for building and running, the source folder is no longer recognized as source folder and source referencing generated classes is no longer compilable.

So the question is: Is this an IDEA bug or am I missing something in my Gradle script?