2
votes

I'm working on a project in which I've got an antlr4 grammar in main code, and I'd like to add a "mini-grammar" for some tests. I'd like the generated .java files for that mini-grammar to only be available to test code. Can the antlr4-maven-plugin support this?

After some experimentation, I settled on this less-than-ideal setup:

  • both my main-targeted and test-targeted grammars are in src/main/resources (I realize this isn't the standard place; I set sourceDirectory to account for this)
  • the antrun plugin copies
    ${project.build.directory}/generated-sources/antlr4/**/MyTestGrammar*.java
    to
    ${project.build.directory}/generated-test-resources/antlr4
  • the build-helper-maven-plugin plugin adds
    ${project.build.directory}/generated-test-resources/antlr4
    as a test source dir

This requires three plugin configurations, and that I explicitly specify which of the generated grammars are meant for tests and which are meant for main code. Is there a better way?

1

1 Answers

4
votes

Save your test grammar in a subfolder of ${baseDir}/src/test/antlr4. Then you can try and put something like this inside the build-plugins element of your POM:

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

and then add the generate sources while compiling the test classes:

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

You may want to adjust directories and packages names according to your needs