IntelliJ IDEA Gradle project not recognizing/locating Antlr generated sources

﹥>﹥吖頭↗ 提交于 2021-02-08 12:21:46

问题


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?

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

回答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




回答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



回答3:


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


来源:https://stackoverflow.com/questions/50947569/intellij-idea-gradle-project-not-recognizing-locating-antlr-generated-sources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!