【问题标题】:using antlr4-maven-plugin for both main and test code对主代码和测试代码使用 antlr4-maven-plugin
【发布时间】:2015-06-29 17:34:00
【问题描述】:

我正在做一个项目,其中我在主代码中有一个 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
    作为测试源目录

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

【问题讨论】:

    标签: java maven antlr4


    【解决方案1】:

    将您的测试语法保存在 ${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>
    

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

    【讨论】:

    • 抱歉,这两天比较忙!我现在对你投了赞成票,因为这是一个很好、完整的答案。我今晚会验证它,然后接受。 :)
    • 工作得很好,只是 Maven 抱怨它应该是 basedir,而不是 baseDir
    猜你喜欢
    • 2019-10-08
    • 2014-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-05
    • 2023-03-24
    • 2023-03-04
    • 1970-01-01
    相关资源
    最近更新 更多