记一次antlr错误:ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.2ANTLR
场景:重构spark 2.1版本的sql语法。因此 需要使用antlr;
前期准备:idea安装了antlr插件(antlr的4.7.2版本)
因此在maven工程中添加了antlr的依赖:
<dependency> <groupId>org.antlr</groupId> <artifactId>antlr4-runtime</artifactId> <version>4.7.2</version> </dependency>
同事在mac的环境下也配置了antlr4.7.2的环境变量
#export CLASSPATH=".:/usr/local/lib/antlr-4.7-complete.jar:$CLASSPATH"
#export CLASSPATH=".;%JAVA_HOME%\jre\lib\rt.jar;/usr/local/lib/antlr-4.7-complete.jar:$CLASSPATH"
#alias antlr4='java -jar /usr/local/lib/antlr-4.7-complete.jar'
#alias grun='java org.antlr.v4.gui.TestRig'
一切工作准备就绪,使用idea运行重构好的spark sql,在编译期出现如下错误:
ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.2ANTLR:
错误的认为是:
<dependency> <groupId>org.antlr</groupId> <artifactId>antlr4-runtime</artifactId> </dependency>
点击进入依赖,看当前antlr4的底层的依赖:
<plugin> <groupId>org.antlr</groupId> <artifactId>antlr4-maven-plugin</artifactId> <version>4.5.3</version> <!-- use older version to process XPathLexer.g4, avoiding cyclic build dependency --> <executions> <execution> <id>antlr</id> <configuration> <sourceDirectory>src</sourceDirectory> </configuration> <goals> <goal>antlr4</goal> </goals> </execution> </executions> </plugin>
这样就能对上错误问题了,因为使用4.7.2生成的代码。但是在编译期间是4.5.3;所以出错了
错误的解决:
将4.5.3改成4.7即可
##############################真实的原因######################################################
spark 2.1版本,支持的antlr是4.5.3.可以下载spark源码,查看maven依赖
由以上可以推断出错误的原因大致是因为spark 2.1版本目前还不支持antlr4.7.2
解决:
修改antlr环境变量,改成antlr4.5.3版本
export CLASSPATH=".:/usr/local/lib/antlr-4.5.3-complete.jar:$CLASSPATH"
alias antlr4='java -Xmx500M -cp "/usr/local/lib/antlr-4.5.3-complete.jar:$CLASSPATH" org.antlr.v4.Tool'
alias grun='java org.antlr.v4.runtime.misc.TestRig'
然后卸载掉idea的antlr插件;
(https://www.cnblogs.com/niutao/)
这样重新基于antlr生成代码后,就可以正常运行了