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

TenPlusFive
  • 431
  • 4
  • 9
  • 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 ? – tddmonkey Apr 30 '15 at 13:30
  • By analysis, do you mean running inspections? – fge Apr 30 '15 at 13:31
  • @fge I mean the automatic inspection when opening a java file for example – TenPlusFive Apr 30 '15 at 13:35
  • @MrWiggles the JavaCC plugin does that by itself (and thats what I meant with the last paragraph) – TenPlusFive Apr 30 '15 at 13:39
  • 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 target – tddmonkey Apr 30 '15 at 14:04
  • 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. – TenPlusFive May 04 '15 at 08:36

1 Answers1

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')
Danny Dan
  • 1,215
  • 1
  • 16
  • 32