如何编译 ANTLR4 语法作为 maven 构建的第一步?
手动从 *.g4 文件创建语法非常简单——在 Linux 上,只需java -jar /usr/local/lib/antlr-4.0-complete.jar grammarName.g4
从控制台运行即可。这会创建一些需要在下一步构建中编译的 java 文件。
是否可以指示 maven 查找所有 *.g4 文件,然后为每个文件运行上述命令,然后继续构建?如果语法编译失败,构建也应该失败。
或者也许有一个插件可以处理这个任务?
我已经看到了这个答案- 缺点是它依赖于 IDE。我希望我的构建尽可能独立于 IDE 和系统(而且我使用的是 Netbeans,所以给出的答案是不可接受的)。
编辑
我已经尝试了benzonico的建议。我添加了其他答案中引用的 pom.xml 中列出的依赖项。这是我的 pom.xml 的样子:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pl.kajman</groupId>
<artifactId>antlr-sandbox</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>antlr-sandbox</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>com.tunnelvisionlabs</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>4.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.tunnelvisionlabs</groupId>
<artifactId>antlr4</artifactId>
<version>4.0</version>
<classifier>complete</classifier>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<outputDirectory>bin</outputDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>6</source>
<target>6</target>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
<compilerArguments>
<Xlint />
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-sources/antlr4</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.tunnelvisionlabs</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.0</version>
<configuration>
<sourceDirectory>src</sourceDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Java 文件是从 g4 文件中自动生成的,但不幸的是有很多错误。它们与我在手动编译并且类路径错误时所拥有的非常相似。一些错误是:
/home/kajman/projects/antlr/antlr-sandbox/target/generated-sources/antlr4/main/java/pl/kajman/antlr/sandbox/grammars/ExprListener.java:[6,56] type org.antlr.v4.runtime.tree.ParseTreeListener does not take parameters
/home/kajman/projects/antlr/antlr-sandbox/target/generated-sources/antlr4/main/java/pl/kajman/antlr/sandbox/grammars/ExprParser.java:[44,32] cannot find symbol
symbol: method
getRuleContexts(java.lang.Class<main.java.pl.kajman.antlr.sandbox.grammars.ExprParser.StatContext>)
location: class main.java.pl.kajman.antlr.sandbox.grammars.ExprParser.ProgContext