16

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"
}
Craig Otis
  • 31,257
  • 32
  • 136
  • 234

3 Answers3

2

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
zavyrylin
  • 342
  • 3
  • 11
1

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

paranoidAndroid
  • 523
  • 5
  • 12
  • 2
    I don't think the Antlr plugin compiles the sources - it just *produces* the Java source, which your build tools/environment can then integrate however they choose. – Craig Otis Jun 26 '18 at 13:01
  • 1
    @CraigOtis that's true, but there might be an ordering issue. ANTLR might be generating the sources, and then the build tries to compile your source before the antlr ones. I worked with ANTLR before (java, not kotlin, but shouldn't matter) and I faced some similiar issues. Did you try what I suggested? I'm curios to know if it will solve the issue – paranoidAndroid Jun 26 '18 at 13:04
1

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')
    }
}
Paraskevas Ntsounos
  • 1,755
  • 2
  • 18
  • 34