java - 对主代码和测试代码使用antlr4-maven-plugin

标签 java maven antlr4

我正在开发一个项目,其中主代码中有一个 antlr4 语法,我想为一些测试添加一个“迷你语法”。我想要生成的 .java该迷你语法的文件仅可用于测试代码。 antlr4-maven-plugin可以支持这个吗?

经过一些实验,我决定采用这种不太理想的设置:

  • 我的主要目标语法和测试目标语法都在 src/main/resources 中(我意识到这不是标准位置;我设置 sourceDirectory 来解释这一点)
  • antrun 插件副本
    ${project.build.directory}/generated-sources/antlr4/**/MyTestGrammar*.java

    ${project.build.directory}/generated-test-resources/antlr4
  • build-helper-maven-plugin 插件添加了
    ${project.build.directory}/generated-test-resources/antlr4
    作为测试源目录

这需要三个插件配置,并且我明确指定生成的语法中哪些用于测试,哪些用于主代码。有更好的办法吗?

最佳答案

将测试语法保存在 ${baseDir}/src/test/antlr4 的子文件夹中。 然后你可以尝试将类似的内容放入 POM 的 build-plugins 元素中:

    <plugin>
        <groupId>org.antlr</groupId>
        <artifactId>antlr4-maven-plugin</artifactId>
        <version>${antlr4.plugin.version}</version>
        <configuration>
            <arguments>
                <argument>-visitor</argument>
            </arguments>
        </configuration>
        <executions>

            <execution>
                <id>antlr-4</id>
                <goals>
                    <goal>antlr4</goal>
                </goals>
            </execution>

            <execution>
                <id>antlr-test</id>
                <configuration>
                    <sourceDirectory>${baseDir}/src/test/antlr4</sourceDirectory>
                    <outputDirectory>${baseDir}/target/generated-test-sources-antlr/antlr4</outputDirectory>
                </configuration>
                <phase>generate-test-sources</phase>
                <goals>
                    <goal>antlr4</goal>
                </goals>
            </execution>

        </executions>
    </plugin>

然后在编译测试类时添加生成源:

 <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>add-test-sources</id>
                        <phase>generate-test-sources</phase>
                        <goals>
                            <goal>add-test-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>${baseDir}/target/generated-test-sources-antlr/antlr4</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

您可能需要根据需要调整目录和包名称

关于java - 对主代码和测试代码使用antlr4-maven-plugin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31096264/

相关文章:

java - 无法使用 Intellij Idea 连接到 openshift

java - Spring Boot viewControllerHandlerMapping配置

java - 为什么maven在settings.xml中未配置的目录中使用本地存储库

Eclipse 在 maven 项目中自动完成代码时出现段错误

java - 如何指定maven的distributionManagement组织范围?

antlr - 为什么 ANTLR 要求标记所有替代品或不标注替代品?

java - 迁移 Xero oauth 1.0a 至 2.0 (JAVA)

java - MyBatis:如何存储查询中的多个值?

java - 使用antlr指定基本监听器/访问者实现

ant - 有 ANTLR4 Ant 任务吗?