Intellij will not recognize antlr generated source code
Asked Answered
M

7

16

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?

Marchelle answered 17/9, 2014 at 23:51 Comment(0)
S
4

I had what I think may have been a similar issue. I am new to IntelliJ, so I am guessing to some extent.

Is the gen folder in your project brown (i.e. a resource) or blue (source code)?

I could not get my project to recognize generated code until I managed to get the gen folder registered as one containing source code.

Sorry to say you may need to fiddle with it - I cannot recall how I did it - but all was well after I changed this.

Selfservice answered 14/10, 2014 at 16:9 Comment(1)
Yep, I have had that issue before. Clicking all the normal buttons usually gets it working. All the normal buttons being: Maven compile, reimport project, generate sources, invalidate caches, restart IDEA.Marchelle
U
20

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

Unfounded answered 11/3, 2016 at 18:2 Comment(0)
B
6

My issue was somewhat similar to @spilymp's:

I was putting the *.g4 files directly in src/main/antlr4:

.
├── src/
|   └── main/
|       ├── antlr/
|       |   ├── Main.g4
|       |   └── imported.g4
|       └── java/
|           └── com/
|               └── test/
|                   └── Test.java

which led to source being generated in default package. I changed the package structure to match that of java:

.
├── src/
|   └── main/
|       ├── antlr/
|       |   ├── com/
|       |   |   └── test/
|       |   |       └── Main.g4            
|       |   └── imports/
|       |       └── imported.g4
|       └── java/
|           └── com/
|               └── test/
|                   └── Test.java

(Note that imports need to go directly in src/main/antlr4/imports)

After this I just run the antlr4 goal from the maven menu ([Module] > Plugins > antlr4 > antlr4:antlr4), which generated the sources in the default location (target/generated-sources/antlr4), where they were already marked with blue, generated-sources icon by Intellij, and MainParser can now be imported!

Beabeach answered 18/6, 2020 at 14:40 Comment(0)
P
5

Steps that worked for me:

  1. Open Maven Projects in right tab
  2. Right click on the module that contained my antlr code
  3. Click Generate Source and Update Folders
Population answered 21/10, 2014 at 23:5 Comment(2)
Hey, that really worked ! Thanks. The generated-sources folder is marked as generated source root, and the rest of the target folder is still ignored. Perfect.Mckellar
This is the one that solved the issue for me! ThanksForbearance
S
4

I had what I think may have been a similar issue. I am new to IntelliJ, so I am guessing to some extent.

Is the gen folder in your project brown (i.e. a resource) or blue (source code)?

I could not get my project to recognize generated code until I managed to get the gen folder registered as one containing source code.

Sorry to say you may need to fiddle with it - I cannot recall how I did it - but all was well after I changed this.

Selfservice answered 14/10, 2014 at 16:9 Comment(1)
Yep, I have had that issue before. Clicking all the normal buttons usually gets it working. All the normal buttons being: Maven compile, reimport project, generate sources, invalidate caches, restart IDEA.Marchelle
M
4

It turns out that my fiddling with preferences caused this issue. If anyone else has this problem:

As v0rl0n mentioned, first make sure the gen folder is listed as a source. Go to Module Settings -> Modules -> Sources.
Look to the right for the blue Source Folders. Ensure your generated source folders are listed (they should have a [generated] flag on the row).

If your generated source directory is listed there, and they are still not being recognized, go to Preferences -> File Types. Look in the Ignore files and folders field. Make sure you don't have anything listed that will match for your generated sources directory.

In my case, I had added target to this list, which caused my problem, and was a real pain to find.

Marchelle answered 15/10, 2014 at 18:32 Comment(0)
R
1

I had a similar problem. Intellij found my generated code in the java folder but not in a subfolder of java. I solved it by putting my grammar files (*.g4) into a package (com.test):

.
├── src/
|   └── main/
|       ├── antlr/
|       |   └── com.test/
|       |       └── Test.g4
|       └── java/
|           └── test/
|               └── Test.java
└── target/
    └── generated-sources/
        └── antlr/
            └── com.test/
                ├── TestBaseListener.java
                └── ...
Rockies answered 11/1, 2016 at 11:58 Comment(0)
P
1

The easiest way to solve this issue is to Right-Click the gen directory and choose Mark Directory as -> Sources Root

enter image description here

Padraic answered 13/11, 2016 at 21:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.