![](/img/trans.png)
[英]IntelliJ IDEA Gradle project not recognizing/locating Antlr generated sources
[英]Why can't Idea not find sources generated by ANTLR4?
我有一个项目,我想在一段 Kotlin 代码中使用由 ANTLR4 生成的类。
在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 package
, mvn clean assembly
,以及在Idea中重建项目导致以下错误:
请注意,错误仅出现在 Kotlin class Transpiler.kt 中,而不是在测试中。
我该如何解决这个问题(确保 ANTLR4 生成的类可以在 Kotlin 代码中使用)?
更新 1:按照@Bart Kiers 的建议移动语法文件并执行mvn clean antlr4:antlr4
后,Idea 中的错误消失了。 但是mvn clean antlr4:antlr4 install
仍然会导致构建错误:
[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
如果您执行以下操作,它应该可以工作:
Deplorable.g4
移动到src/main/antlr4/com/dpisarenko/deplorable/
(注意你把它放在了src/main/antlr4/com.dpisarenko.deplorable/
!)mvn clean antlr4:antlr4
target/generated-sources/antlr4
为“Generated Sources Root”(在您的 IDE 和 select Mark Directory as
右键单击它) 如果没有,请尝试使用最新的 ANTLR4 版本: 4.9.1
(不仅是工具和运行时,还适用于antlr4-maven-plugin
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.