如何编译ANTLR4语法作为maven构建的第一步?自定义Maven在编译时自动创建ANTLR4语法java文件
从* .g4文件手动创建语法非常简单 - 在linux上,只需从控制台运行java -jar /usr/local/lib/antlr-4.0-complete.jar grammarName.g4
即可。这会创建一些需要在下一步构建中编译的java文件。
是否可以指示maven找到所有* .g4文件,然后对其中的每一个运行上述命令,然后继续进一步构建?如果语法编译失败,构建应该也会失败。
或者也许是有一个插件,可以应付这个任务?我看过this answer已经 - 缺点是它依赖于IDE。我希望我的构建尽可能地成为IDE和系统独立的(并且我使用Netbeans,所以给出的答案是不可接受的)。
编辑
我已经试过benzonico的建议。我已经添加了depenedcies,如其他答案中引用的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
请你看看我的编辑和详细说明吗?你的回答肯定让我更接近解决方案,但我还没有。 – kajman 2013-03-09 19:49:57
它现在,我不得不删除pom.xml的依赖 - ' org.antlr antlr4 - Maven的插件 4.0 com.tunnelvisionlabs antlr4运行时 4.0 ' –
kajman
2013-03-10 11:22:27
注意,文档链接亲这里提供的是antlr v2,而不是v4。有关更多最新信息,请参见[我如何获得有关antlr4-maven-plugin](http://stackoverflow.com/q/15324837/2506021)的帮助。 – 2014-01-15 17:17:28