IntelliJ IDEA Gradle project not recognizing/locating Antlr generated sources
Asked Answered
J

3

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"
}
Jessiejessika answered 20/6, 2018 at 11:49 Comment(6)
You configured the folder as the Excluded, how IDEA recognizing/locating all the codes?Limitless
I didn't configure it that way - it's being auto-generated from a build.gradle fileJessiejessika
Maybe you can try to fix that manually to check is this helpLimitless
@ParaskevasNtsounos There is no manifest for this project.Jessiejessika
@CraigOtis have you try https://mcmap.net/q/714970/-how-do-i-get-intellij-to-recognize-gradle-generated-sources-dirGuileful
@CraigOtis have you looked inside the [PROJ_ROOT]/.idea/libraries if you can find the file referencing antlr - SprintParser etc? if it's there, open it check if you have a valid path in both the <classes> and <sources> tags. Maybe your Android Studio has lost these?Catchall
A
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
Arvind answered 28/6, 2018 at 18:56 Comment(1)
compileKotlin.dependsOn generateGrammarSource was the key line that worked for meCulminant
F
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

Flagelliform answered 25/6, 2018 at 20:58 Comment(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.Jessiejessika
@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 issueFlagelliform
G
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')
    }
}
Guileful answered 27/6, 2018 at 13:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.