JustDanyul
JustDanyul

Reputation: 14044

Antlr4 "Test Rig" and maven

I am struggling a little bit to figure out how I can use the antlr4 maven plug in to run the TestRig class?

I've read though the output of mvn antlr4:help -Ddetail=true, which is the only documentation I have been able to find, but this doesn't mention how to use the TestRig. So, what's the recommended way of using the test rig together with maven? using the grun alias method doesn't seem very elegant here.

UPDATED - Reasoning

Hello :)

Right, don't get me wrong but I really don't understand why you wouldn't want this functionality in the maven plugin? And I don't understand why its soul purpose should be to compile grammars?

Currently, if I maintain a build with Maven, and I use the antlr4-maven-plugin , it will install both the plugin and the antlr 4.1 in my maven repository. With this already there, why would I start adding things to my classpath and creating aliases when maven can take care of that? I mean, this is what maven is for really. If I had a antlr4:TestRig goal, then all I would do was use that. No need to manually maintain the class path, or create bash aliases. It would just work.

And be far far more elegant that hard-coding elements from my local maven repository in my class path, and maintaining bash aliases. Or alternatively, maintain two installations per. version of antlr I wish to use (one maintained by me, simply to use TestRig, and one maintained by maven for everything else).

Additionally, if I wanted to use a different version of antlr, then I wouldn't need to update the classpath and my aliases, maven would simply manage all this for me :)

Upvotes: 7

Views: 2714

Answers (4)

Ralph
Ralph

Reputation: 120761

I like the idea of using Maven to run the TestRig. But I do not like to add the dependency org.antlr:antlr4 to my code (because I already have org.antlr:antlr4-runtime) so my solution is to configure the exec plugin with an additional dependency.

<properties>
    <antlr.version>4.7.1</antlr.version>
</properties>           
...
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
            <id>TestRigGui</id>
            <phase>none</phase>
            <goals>
                <goal>java</goal>
            </goals>
            <configuration>
                <mainClass>org.antlr.v4.gui.TestRig</mainClass>
                <arguments>                             
                    <!-- Grammar -->
                    <argument>de.humanfork.experiment.antlr.Hello</argument>
                    <!-- start rule -->
                    <argument>hello_rule</argument>
                    <!-- enable gui -->
                    <argument>-gui</argument>
                    <!-- input file -->
                    <argument>example.txt</argument>
                </arguments>                
                <includePluginDependencies>true</includePluginDependencies>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.antlr</groupId>
            <artifactId>antlr4</artifactId>
            <version>${antlr.version}</version>
        </dependency>
    </dependencies>
</plugin>

Then use: mvn exec:java@TestRigGui to start the GUI

BTW: in ANTLR 4.7.1 the TestRig main class is: org.antlr.v4.gui.TestRig

Upvotes: 0

Jared Beck
Jared Beck

Reputation: 17528

I had a similar question, in that I wanted to use the TestRig -gui option for debugging my grammar. I didn't find a way to run the GUI via the antlr4-maven-plugin, but I did manage to build a satisfactory CLASSPATH. The key was to include target/classes.

# Assuming your project is in $PROJECT ..
CLASSPATH=".:/usr/local/lib/antlr-4.1-complete.jar:$PROJECT/target/classes"
alias grun='java org.antlr.v4.runtime.misc.TestRig'
mvn -q compile
grun MyGrammer startingRule -gui < test_input

Should produce a lovely GUI view of the syntax tree.

Upvotes: 2

jasterm007
jasterm007

Reputation: 173

This is how I invoke TestRig with Maven:

mvn exec:java -Dexec.mainClass="org.antlr.v4.runtime.misc.TestRig" 
-Dexec.args="<DOT_NOTATION_GRAMMAR_CLASSPATH> <START_RULE> 
-gui <INPUT_FILE>"

So if you've got MyGrammar.g4 in src/main/antlr4/com/test/parser with a starting rule of startRule:

mvn exec:java -Dexec.mainClass="org.antlr.v4.runtime.misc.TestRig" 
-Dexec.args="com.test.parser.MyGrammar startRule 
-gui <INPUT_FILE>"

Upvotes: 6

Sam Harwell
Sam Harwell

Reputation: 99859

Why would the Maven plugin run the TestRig class? The Maven plugin's job is converting the .g4 grammar files to .java source files in the proper package locations and ensuring those generated files get compiled. TestRig is not used for any part of that.

Edit: I have been using ANTLR for many years, in many applications. In all that time I have never updated my system classpath, nor operated ANTLR/gunit/TestRig from the command line or created aliases for it. Doing so is not helpful for automated testing and inevitably leads users into the problems you described. That said, the thought that TestRig needed special support in the Maven plugin also never crossed my mind, because better solutions already exist.

Some alternatives

  1. You can use the surefire plugin, and write a JUnit test that performs operations on your grammar directly (create a lexer/parser, parse some input, and perhaps even call inspect() on the resulting parse tree.
  2. You can use the surefire plugin, and write a JUnit test that explicitly calls TestRig.main(String[]) with the correct arguments.
  3. You can modify the ANTLR 4 Maven plugin to add a new goal for running TestRig, and submit a pull request to the project to have it included in a future release (you would need to make a very compelling case since there are already 2 alternatives that are more suited to long-term successful testing of a project using ANTLR 4).

Upvotes: 2

Related Questions