Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ IDEA Gradle project not recognizing/locating Antlr generated sources

I'm using Antlr in a simple Kotlin/Gradle project, and while my Gradle build is generating Antlr sources, they are not available for importing into the project.

As you can see (on the left), the classes (Lexer/Parser, etc.) are being generated. I have also configured this generated-src/antlr/main directory as a Source Root. Most questions I see list this as a solution, but I've already done it.

The issue persists after multiple rebuilds (both in IDEA and on the CLI), and following all the usual "Invalidate Cache and Restart" issues.

Further, the import issue is listed in the Gradle build on the CLI so it doesn't seem isolated to IDEA.

What am I missing here?

enter image description here

Here's the build.gradle file produced by IDEA when I was creating the project initially, and which IDEA is using for project/workspace synchronization.

plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.2.50'
}

group 'com.craigotis'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

apply plugin: 'antlr'

dependencies {
    antlr "org.antlr:antlr4:4.5"
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.2.0'
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
like image 642
Craig Otis Avatar asked Jun 20 '18 11:06

Craig Otis


People also ask

How does IntelliJ recognize Gradle project?

In the Gradle tool window, right-click a linked project. From the context menu, select Open Gradle config F4 . IntelliJ IDEA navigates to the appropriate Gradle configuration file and the related build.

How do I refresh the Gradle cache in IntelliJ?

We can do this by: Selecting one of the suggestions in the message; Pressing on the Refresh Gradle icon in the top right, Using the keyboard shortcut ⇧⌘I (macOS), or Ctrl+Shift+O (Windows/Linux).

How do I sync Gradle dependencies in IntelliJ?

We can configure the settings for how IntelliJ IDEA syncs with Gradle by pressing the settings icon in the Gradle tool window, and selecting Auto-Reload Settings. We can set IntelliJ IDEA to automatically reload the project after "Any changes" in the build script files, so changes are automatically reloaded.

How do I sync Gradle project in IntelliJ?

If you have some custom plugins that require you to import your project from the IntelliJ IDEA model, press Ctrl+Shift+A and search for the Project from Existing Sources action. In the dialog that opens, select a directory containing a Gradle project and click OK. IntelliJ IDEA opens and syncs the project in the IDE.


3 Answers

Shouldn't it locate the compiled classes and not the sources? Do you see the antlr generated classes in the target directory?

Try this: first build the project without referencing or using any ANTLR generated classes, and only after the build is successful, then add the code that references them.

(In other words, what I think that happens, is that your ANTLR sources are compiled after the code that references them. They never have a chance to compile because build fails before)

Also if this is really the case, you can solve it also by splitting into two artifacts and make sure the ANTLR one is built before the one with the code that uses it

like image 134
paranoidAndroid Avatar answered Oct 17 '22 05:10

paranoidAndroid


Try adding this to your build.gradle:

sourceSets {
  main.java.srcDirs += "${project.buildDir}/generated-src/antlr/main"
}

generateGrammarSource {
  arguments += ["-visitor", "-package", "com.craigotis.sprint.core.antlr"]
  outputDirectory = file("${project.buildDir}/generated-src/antlr/main/com/craigotis/sprint/core/antlr")
}

compileKotlin.dependsOn generateGrammarSource
like image 1
zavyrylin Avatar answered Oct 17 '22 05:10

zavyrylin


Try to add generated sources in idea module like this post from Daniel Dekany here:

apply plugin: "idea"
...
sourceSets.main.java.srcDir new File(buildDir, 'generated/javacc')
idea {
    module {
        // Marks the already(!) added srcDir as "generated"
        generatedSourceDirs += file('build/generated/javacc')
    }
}
like image 1
Paraskevas Ntsounos Avatar answered Oct 17 '22 07:10

Paraskevas Ntsounos