Maven仅在默认包中生成Antlr源

Mei*_*une 8 eclipse maven-3 antlr4

我将从我的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>HPK</groupId>
<artifactId>WRB</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>WRB</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.antlr</groupId>
        <artifactId>antlr4</artifactId>
        <version>4.5.3</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-clean-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <id>auto-clean</id>
                    <phase>initialize</phase>
                    <goals>
                        <goal>clean</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.antlr</groupId>
            <artifactId>antlr4-maven-plugin</artifactId>
            <version>4.3</version>
            <executions>
                <execution>
                    <configuration>
                        <arguments>
                            <argument>-visitor</argument>
                            <argument>-package</argument>
                            <argument>wrb.grammar</argument>
                        </arguments>
                    </configuration>
                    <id>antlr</id>
                    <goals>
                        <goal>antlr4</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <pluginManagement>
        <plugins>
            <!--This plugin's configuration is used to store Eclipse m2e settings 
                only. It has no influence on the Maven build itself. -->
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>
                                        org.apache.maven.plugins
                                    </groupId>
                                    <artifactId>
                                        maven-clean-plugin
                                    </artifactId>
                                    <versionRange>
                                        [3.0.0,)
                                    </versionRange>
                                    <goals>
                                        <goal>clean</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore></ignore>
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
Run Code Online (Sandbox Code Playgroud)

当我maven install在这个项目上运行时,maven应该从wrb.grammar包中的antlr4插件生成源代码,但事实并非如此.它完成所有工作,但将源代码放入这些目录中,它只是将它们放在它所谓的"default-package"中,而这只是它的根源antlr/generated-sources.

如果我通过右键单击语法并在run as下选择它来使用Antlr4IDE插件,则会在右侧目录中生成源.

我正在与这个小项目合作的另一个人没有问题maven-install.除了我们的操作系统和eclipse版本,一切都是一样的.

我在MacOS上使用Eclipse Oxygen.

我的错误是maven-plugin没有生成我想要的目录?

Chr*_*hme 8

我检查了antlr4版本4.3的来源。该-package参数仅由代码生成器模板使用,而不在实际工具源代码中的任何位置使用(请参阅 的Github 搜索结果genPackage)。因此它不会影响输出文件的位置。

相反,每个输出文件的位置是根据相应输入文件的位置确定的(请参阅源代码中的此处此处)。这符合Maven 插件文档中的解释:

如果您的语法打算成为名为 org.foo.bar 的包的一部分,那么您可以将其放置在目录 src/main/antlr4/org/foo/bar 中。然后,该插件将在输出目录 target/ generated-sources/antlr4/org/foo/bar 中生成 .java 和 .tokens 文件。编译 Java 文件时,它们将位于 Javac 编译器的正确位置,无需任何特殊配置。生成的java文件由插件自动提交编译。

此外,使用 antlr4-maven-plugin 时,无需指定该-package选项。由于插件-package从输入文件路径派生参数的值,并自动将其添加到 antlr 调用中(请参阅此处的源代码)。这可能也是为什么-pacakge不能直接作为Maven 插件中的配置参数使用的原因。

解决方案

为了将生成的文件放置在与包名称匹配的目录结构中,您需要对输入文件使用相同的结构。

本质上,您需要做的就是将语法文件放入 中,从配置中src/main/antlr4/wrb/grammar删除参数,一切都会按预期工作。-package

顺便说一句:而不是写

<arguments>
   <argument>-visitor</argument>
</arguments>
Run Code Online (Sandbox Code Playgroud)

你可以简单地写

<visitor>true</visitor>
Run Code Online (Sandbox Code Playgroud)

因为antlr4-maven-plugin可以直接理解这个参数。