Per Huss
Per Huss

Reputation: 5095

How to configure Gradle for code generation so that IntelliJ recognises generated java source?

I'm migrating a legacy project to Gradle. One step of the build process is to generate java source code for a proprietary protocol. Unfortunately, when importing the project into IntelliJ, the generated source code is not recognised, so the project does not build with IntelliJ.

I have the following build script (stripped of non-relevant pieces):

apply plugin: "base"
apply plugin: "java"

task generate {
    description "Generates java code"
    inputs.files(fileTree("src/codegen/"))
    outputs.dir("${buildDir}/generated-src/")
    // ...
    // codegen
    // ...
}

compileJava {
    source(generate.outputs)
}

If I add the following piece, IntelliJ will add the generated source as source, but fail to identify it as generated:

sourceSets {
    main {
        java {
            srcDir "${buildDir}/generated-src/"
        }
    }
}

Is there a way to get IntelliJ to recognise the generated sources just as generated sources, so that normal warnings to prevent editing of these classes are shown?

Upvotes: 1

Views: 2362

Answers (1)

You need to tell Idea specifically that the directory contains auto-generated sources, as outlined in this post.

apply plugin: "idea"

sourceSets.main.java.srcDir new File(buildDir, '${buildDir}/generated-src/')
idea {
    module {
        // Marks the already(!) added srcDir as "generated"
        generatedSourceDirs += file('${buildDir}/generated-src/')
    }
}

Upvotes: 6

Related Questions