Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intellij will not recognize antlr generated source code

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?

like image 483
oillio Avatar asked Sep 17 '14 23:09

oillio


People also ask

How do I add generated sources in Intellij?

You can just change the project structure to add that folder as a "source" directory. Project Structure → Modules → Click the generated-sources folder and make it a sources folder.

How do I create an Antlr project in Intellij?

right click on the ANTLR grammar file and select Generate ANTLR Recognizer from the context menu. The Compile 'file. g4' option under the main Build menu does nothing for me. next mark the output directory (by default, the gen directory in the project root) as generated source.

What is generated sources in Java?

It's a step in the build process that generates source files from other files, e.g. generating Java source files from XML schema files (JAXB). – Andreas. Nov 13, 2020 at 22:37. Generate Java Code from XSD, JSON, or for example MapStruct etc.


1 Answers

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

like image 157
Vlastimil Ovčáčík Avatar answered Sep 20 '22 11:09

Vlastimil Ovčáčík