自定义Maven以在构建时自动创建ANTLR4语法java文件

问题描述 投票:1回答:1

如何编译ANTLR4语法作为maven构建的第一步?

手动从* .g4文件创建语法非常简单 - 在linux上只需要从控制台运行java -jar /usr/local/lib/antlr-4.0-complete.jar grammarName.g4。这会创建一些需要在构建的下一步中编译的java文件。

是否有可能指示maven找到所有* .g4文件,然后为每个文件运行上述命令,然后继续进行构建?如果语法编译失败,编译也会失败。

或者也许有一个插件可以应付这个任务?

我已经看过this answer了 - 缺点是它依赖于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
maven antlr4 custom-build-step
1个回答
2
投票

实际上,你在问题中提到的帖子中提出的解决方案使用的antlr maven plugin与你需要的完全兼容。解决方案的要点是能够在日食中使用它,但如果你不需要它,你可以跳过关于m2e(在pluginManagement会话中)的要点部分,并立即使用antlr maven plugin

看看documentation of the plugin

© www.soinside.com 2019 - 2024. All rights reserved.