Craig Otis
Craig Otis

Reputation: 32054

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"
}

Upvotes: 16

Views: 3708

Answers (3)

Paraskevas Ntsounos
Paraskevas Ntsounos

Reputation: 1777

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')
    }
}

Upvotes: 1

zavyrylin
zavyrylin

Reputation: 342

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

Upvotes: 2

paranoidAndroid
paranoidAndroid

Reputation: 583

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

Upvotes: 1

Related Questions