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

1

7

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).

Laurentian answered 30/4, 2015 at 13:24 Comment(6)
Do you have to run a task for the JavaCC plugin to do its work? If so, can you not add that as a dependency for compileJava ?Coper
By analysis, do you mean running inspections?Chiaki
@Chiaki I mean the automatic inspection when opening a java file for exampleLaurentian
@MrWiggles the JavaCC plugin does that by itself (and thats what I meant with the last paragraph)Laurentian
Ah, I get you, so its plugged into the normal gradle lifecycle? How do you import into IntelliJ? If it's with gradle idea you could make that have a dependency on the correct targetCoper
Yes, its in the normal gradle lifecycle. I imported it with the File -> New -> Project from existing sources. I've found that I can configure IntelliJ to run the task before a refresh, by adding it in the "Task Activation" list to run before syncing.Laurentian
J
2

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')
Jobber answered 15/6, 2015 at 13:29 Comment(3)
what's the point of adding a new sourcesSet (generated) in that case ?Tarazi
to make intellij idea see itJobber
But it is a bad solution to adapt our code to the IDE. Ins't it supposed to be "intelligent" ? :)Tarazi

© 2022 - 2024 — McMap. All rights reserved.