项目中用到 ANTLR 做语法解析,由于语法规则复杂,生成的 Parser 文件非常大(45k 行),用 javac 编译时直接出现 code too large (代码过长)错误无法正常处理。后来经过试验,发现 Eclipse Compiler 可以正常编译,因此考虑在 maven 中设置 Eclipse Compiler 为默认 java 编译器。在 pom.xml 中加入如下设置即可:
<project> ... <build> ... <plugins> ... <plugin> <artifactId>maven-compiler-plugin</artifactId> ... <configuration> ... <compilerId>eclipse</compilerId> </configuration> <dependencies> ... <dependency> <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-compiler-eclipse</artifactId> <version>2.1</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
这里有一点需要注意,如果 plexus-compiler-eclipse 插件版本较低(1.x),在 maven 3.x 上编译时将抛出如下错误:java.lang.NoSuchMethodError: org.codehaus.plexus.compiler.CompilerError: method <init>(Ljava/lang/String;ZIIIILjava/lang/String;)V not found