Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 对主代码和测试代码使用antlr4 maven插件_Java_Maven_Antlr4 - Fatal编程技术网

Java 对主代码和测试代码使用antlr4 maven插件

Java 对主代码和测试代码使用antlr4 maven插件,java,maven,antlr4,Java,Maven,Antlr4,我正在做一个项目,在主代码中有一个antlr4语法,我想为一些测试添加一个“迷你语法”。我希望该迷你语法生成的.java文件仅可用于测试代码。antlr4 maven插件能支持这一点吗 经过一些实验后,我确定了这个不太理想的设置: 我的主要目标语法和测试目标语法都在src/main/resources中(我意识到这不是标准位置;我设置了sourceDirectory来说明这一点) antrun插件将${project.build.directory}/generated sources/ant

我正在做一个项目,在主代码中有一个antlr4语法,我想为一些测试添加一个“迷你语法”。我希望该迷你语法生成的
.java
文件仅可用于测试代码。antlr4 maven插件能支持这一点吗

经过一些实验后,我确定了这个不太理想的设置:

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

    ${project.build.directory}/generated test resources/antl4
  • build helper maven插件将
    ${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>

org.antlr
antlr4 maven插件
${antlr4.plugin.version}
-访客
antlr-4
antlr4
antlr试验
${baseDir}/src/test/antlr4
${baseDir}/target/生成的测试源antlr/antlr4
生成测试源
antlr4
然后在编译测试类时添加生成源:

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

org.codehaus.mojo
构建助手maven插件
添加测试源
生成测试源
添加测试源
${baseDir}/target/生成的测试源antlr/antlr4

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

对不起,这几天很忙!我现在投票给你,因为这是一个好的、完整的答案。我今晚会核实一下,然后接受。:)除了Maven抱怨它应该是basedir,而不是basedir之外,它工作得非常好