无法配置Maven以查找antlr4生成的源文件

时间:2019-09-13 18:52:58

标签: maven antlr antlr4

我试图弄清楚如何将Maven与antlr4一起使用,并且一直没有找到可以正常工作的任何东西。我不是Maven专家。我从“权威ANTLR 4参考”的第3章开始按比例缩小示例。我正在Windows 10上使用命令提示符控制台窗口。我最初是想使用Eclipse,但是运气不佳,所以我想我会回到基础知识并尝试命令提示符。

我的主要Java文件App.java位于my-app / src / main / java / com / mycompany / app / App.java

我的名为ArrayInit.g4的antlr4语法文件位于my-app / src / main / antlr4 / ArrayInit.g4

键入“ mvn antrl4:antlr4”后,从ArrayInit.g4(ArrayInitListener.java,ArrayInitParser.java等)生成的Java文件位于my-app / target / generated-sources / antlr4

目前,我的问题是如何设置pom.xml文件,以便在编译App.java时可以编译并包括生成的antlr4 java文件。我一直在尝试使用各种源,输出,目标目录配置pom,但是没有运气。

这是我的语法文件ArrayInit.g4-

grammar ArrayInit;

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

value : init
        | INT
        ;

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

这是我的主要Java文件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);
    }
}

这是我的pom.xml文件-

<?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>

这是我尝试运行mvn编译时的错误消息-

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 个答案:

答案 0 :(得分:0)

正如评论所说,我必须从<pluginManagement>删除pom.xml标记,以使Maven作为generate-sources的一部分自动运行ANTLR。