Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate generated sources in IntelliJ IDEA when using Gradle?

I'm currently using JavaCC (with the JavaCC gradle plugin from here) to generate some of my source code. The rest of the project does depend on that code. If I import the project into IDEA or clean the project, then I will get errors because classes are not found. However, building the project does work.

Is it possible to modify the gradle file, so that IntelliJ (and possibly other editors too) know to generate these sources before analysing the code?

The generated code is saved in src/gen/java/ and the location of the generated code is made known via:

sourceSets {
    gen {
        java {
            srcDir 'src/gen/java'
        }
    }
}

Since IntelliJ is building the project I thought the easiest way would have been to do:

compileJava.dependsOn <generateSourcesTask>

But adding that to the gradle file does not have an effect (probably because the JavaCC plugin is doing this already).

like image 658
TenPlusFive Avatar asked Apr 30 '15 13:04

TenPlusFive


1 Answers

Did you try to add generated sources dir to main? Like this:

sourceSets {
    main {
        java {
            srcDirs = ["src/main/java", "src/gen/java"]
        }
    } 
}

It works for me with:

compileJava.dependsOn('generateSourcesTask')
like image 71
Danny Dan Avatar answered Oct 19 '22 19:10

Danny Dan