1
votes

I'm trying to figure out how to use Maven with antlr4, and haven't been able to find anything that quite works all the way through. I'm no Maven expert. I'm starting out with a scaled-down example from chapter 3 of "The Definitive ANTLR 4 Reference". I'm using the Command Prompt console window on Windows 10. I was originally trying to use Eclipse but wasn't having much luck, so I thought I'd go back to basics and try the Command Prompt.

My main java file called App.java is at my-app/src/main/java/com/mycompany/app/App.java

My antlr4 grammar file called ArrayInit.g4 is at my-app/src/main/antlr4/ArrayInit.g4

The java files generated from ArrayInit.g4 (ArrayInitListener.java, ArrayInitParser.java, etc.) after typing "mvn antrl4:antlr4" are at my-app/target/generated-sources/antlr4

My question for the moment is how can I set up my pom.xml file so that when I compile App.java, it compiles and includes the generated antlr4 java files. I've been trying to configure the pom with various source, output, destination directories, but with no luck.

This is my grammar file ArrayInit.g4 -

grammar ArrayInit;

init : '{' value (',' value)* '}' ;

value : init
        | INT
        ;

INT : [0-9]+ ;
WS : [ \t\r\n]+ -> skip ;

This is my main java file App.java -

package com.mycompany.app;

import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;

public class App 
{
    public static void main( String[] args ) throws Exception
    {       
        ANTLRInputStream input = new ANTLRInputStream(System.in);

        ArrayInitLexer lexer = new ArrayInitLexer(input);
    }
}

This is my pom.xml file -

<?xml version="1.0" encoding="UTF-8"?>

<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>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>my-app</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.release>11</maven.compiler.release>
  </properties>

  <dependencies>
    <dependency>
        <groupId>org.antlr</groupId>
        <artifactId>antlr4-runtime</artifactId>
        <version>4.7.2</version>
    </dependency>
    <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.12</version>
         <scope>test</scope>
    </dependency>

  </dependencies>

  <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.antlr</groupId>
                    <artifactId>antlr4-maven-plugin</artifactId>
                    <version>4.7.2</version>
                    <executions>
                        <execution>
                            <id>antlr</id>
                            <goals>
                                <goal>antlr4</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                </plugin>
            </plugins>
        </pluginManagement>
  </build>
</project>

This is my error message when I try to run mvn compile -

E:\mvnbael\my-app>mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< com.mycompany.app:my-app >----------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ my-app ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\mvnbael\my-app\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ my-app ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to E:\mvnbael\my-app\target\classes
[INFO] /E:/mvnbael/my-app/src/main/java/com/mycompany/app/App.java: E:\mvnbael\my-app\src\main\java\com\mycompany\app\App.java uses or overrides a deprecated API.
[INFO] /E:/mvnbael/my-app/src/main/java/com/mycompany/app/App.java: Recompile with -Xlint:deprecation for details.
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /E:/mvnbael/my-app/src/main/java/com/mycompany/app/App.java:[20,17] cannot find symbol
  symbol:   class ArrayInitLexer
  location: class com.mycompany.app.App
[ERROR] /E:/mvnbael/my-app/src/main/java/com/mycompany/app/App.java:[20,44] cannot find symbol
  symbol:   class ArrayInitLexer
  location: class com.mycompany.app.App
[INFO] 2 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.240 s
[INFO] Finished at: 2019-09-13T11:07:21-07:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project my-app: Compilation failure: Compilation failure:
[ERROR] /E:/mvnbael/my-app/src/main/java/com/mycompany/app/App.java:[20,17] cannot find symbol
[ERROR]   symbol:   class ArrayInitLexer
[ERROR]   location: class com.mycompany.app.App
[ERROR] /E:/mvnbael/my-app/src/main/java/com/mycompany/app/App.java:[20,44] cannot find symbol
[ERROR]   symbol:   class ArrayInitLexer
[ERROR]   location: class com.mycompany.app.App
1
Remove pluginManagement tags from your pom file...khmarbaise

1 Answers

1
votes

As the comment says, I had to remove <pluginManagement> tags from the pom.xml in order to get Maven to automatically run ANTLR as part of generate-sources.