Intellij will not recognize antlr generated source code

oillio picture oillio · Sep 18, 2014 · Viewed 8.6k times · Source

I am having trouble getting Intellij to recognize the generated source code from antlr4. Any reference to the generated code appears as errors, code completion doesn't work, etc.

I am using maven and the antlr4-maven-plugin to generate the code. My code, referencing the generated code compiles and builds fine under maven. The generated code is under /target/generated-sources/antlr4, which is what Intellij expects.

I have tried the usual fixes such as reimport maven projects, update folders, invalidate cache, etc. None of it seems to work.

Anyone seen this before? Is there a way to point to the generated sources directly within Intellij?

Answer

Vlastimil Ovčáčík picture Vlastimil Ovčáčík · Mar 11, 2016

The problem

target/generated-sources/antlr4 is not automatically marked as source dir, instead its direct subdir com.example is. Intellij Idea fails to detect proper package for classes inside target/generated-sources/antlr4/com.example.

The cause

The source file *.g4 is in src/main/antlr4/com.example, but it actually it should be src/main/antlr4/com/example. Note the /. You probably forgot to mark src/main/antlr4 as source dir in Idea, and when you thought you are creating package structure, you actually just created single dir called com.example.

The fix

Mark src/main/antlr4 as source dir, create proper directory structure src/main/antlr4/com/example. Rebuild.

Alternative fix

Go to Project Structure - Modules - Source Folders and find the target/generated-sources/antlr4/com.example - click Edit properties and set Package prefix to com.example.


Different but related problem here