![](/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.