【问题标题】:Why can't Idea not find sources generated by ANTLR4?为什么 Idea 找不到 ANTLR4 生成的源?
【发布时间】:2021-01-19 20:05:24
【问题描述】:

我有一个project,我想在其中使用由 ANTLR4 在一段 Kotlin 代码中生成的类。

pom.xml中,ANTLR4配置如下。

<dependencies>
    <dependency>
        <groupId>org.antlr</groupId>
        <artifactId>antlr4-runtime</artifactId>
        <version>4.7.1</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.antlr</groupId>
            <artifactId>antlr4-maven-plugin</artifactId>
            <version>4.7.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>antlr4</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

生成的类放入target/generated-sources/antlr4:

mvn clean packagemvn clean assembly,以及在Idea中重建项目导致如下错误:

请注意,错误仅发生在 Kotlin 类 Transpiler.kt 中,而不发生在 test 中。

我该如何解决这个问题(确保 ANTLR4 生成的类可以在 Kotlin 代码中使用)?

更新 1: 按照@Bart Kiers 的建议移动语法文件并执行mvn clean antlr4:antlr4 后,Idea 中的错误消失了。但是mvn clean antlr4:antlr4 install 仍然会导致build errors

[ERROR] Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.4.21:compile (compile) on project elispt: Compilation failure: Compilation failure: 
[ERROR] /Users/dp118m/dev/misc/elispt/src/main/kotlin/com/dpisarenko/deplorable/Transpiler.kt:[9,21] Unresolved reference: DeplorableLexer
[ERROR] /Users/dp118m/dev/misc/elispt/src/main/kotlin/com/dpisarenko/deplorable/Transpiler.kt:[11,22] Unresolved reference: DeplorableParser
[ERROR] /Users/dp118m/dev/misc/elispt/src/main/kotlin/com/dpisarenko/deplorable/Transpiler.kt:[12,21] Unresolved reference: DeplorableParser

【问题讨论】:

  • 假设 Maven 构建也失败了,我不认为这是一个 IDE 问题。看起来 Maven 需要与现在不同的配置。也许 Kotlin 源配置不正确。

标签: java maven kotlin intellij-idea antlr4


【解决方案1】:

如果您执行以下操作,它应该会起作用:

  1. Deplorable.g4 移动到src/main/antlr4/com/dpisarenko/deplorable/(请注意,您将它放在src/main/antlr4/com.dpisarenko.deplorable/ 中!)
  2. 运行mvn clean antlr4:antlr4
  3. 如果尚未完成,请将 target/generated-sources/antlr4 标记为“Generated Sources Root”(在 IDE 中右键单击它并选择 Mark Directory as

如果没有,请尝试使用最新的 ANTLR4 版本:4.9.1(不仅是工具和运行时,还适用于 antlr4-maven-plugin)。

【讨论】:

  • 谢谢。问题是关于想法的,你的回答解决了这部分问题。如果您对如何解决 Update 1 中的问题有任何想法,如果您能分享这些想法,我将不胜感激。再次感谢您的回答。
  • 不确定:可能是 Kotlin 编译器期望生成的 Java 源代码驻留在 src/main/java/com/dpisarenko/deplorable/ 而不是 target/generated-sources/antlr4/com/dpisarenko/deplorable/ 中?尝试手动将它们移到那里一次,然后再试一次mvn clean antlr4:antlr4 install
猜你喜欢
  • 2012-04-08
  • 2017-12-21
  • 1970-01-01
  • 2015-05-31
  • 2020-11-13
  • 1970-01-01
  • 2015-03-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多