Question

How can I compile an ANTLR4 grammar as a first step of maven build?

Creating a grammar from *.g4 file manually is pretty straightforward - on linux one needs just to run java -jar /usr/local/lib/antlr-4.0-complete.jar grammarName.g4 from console. This creates some java files that need to be compiled in the next step of build.

Is it possible to instruct maven to find all *.g4 files, then to run the above command for every one of them, and then proceed with build further? If grammar compilation fails build should fail also.

Or maybe is there's a plugin that can cope with this task?

I've seen this answer already - the drawback is it is IDE dependent. I want my build to be IDE and system independent as much as possible (and I'm using Netbeans so given answer is unacceptable).

EDIT

I've tried benzonico's suggestion. I've added depenedcies as listed in referenced pom.xml from other answer. Here's how my pom.xml looks like:

<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 files are automatically generated form g4 file, but unfortunately there's a lot of errors. They are very similar to those I've had when compiling manually and had wrong classpath. Some of the errors are:

/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
Was it helpful?

Solution

Actually the solution proposed in the post you refer in your question use the antlr maven plugin which is totally compatible with what you need. The gist in the solution is to be able to use it within eclipse but if you don't need that you can just skip the part of the gist talking about m2e (in the pluginManagement session) and use the antlr maven plugin right away.

Have a look at the documentation of the plugin

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top