The ANTLR 4 plugin for Maven can generate parsers for any number of grammars in your project.

Compiling Grammars into Parsers

By default, the antlr4 goal will search for grammar files in the directory ${basedir}/src/main/antlr4 and any additional .tokens files in the directory ${basedir}/src/main/antlr4/imports. This can be configured to search other directories using the plugin configuration parameters as described in the antlr4 goal documentation.

The following figure shows the expected layout of files for the default configuration of this plugin.

      +--- antlr4/...       .g4 files organized in the required package structure
             +--- imports/  user-created .tokens files and .g4 files that are imported by other grammars

The next step is to configure your POM to call the plugin. The goals will normally run during the generate-sources phase of the build. Examples of how to configure your POM can be found on the various examples pages, reachable via the page menu. If you stick with the default values, the snippet below will suffice:


Note that you can create multiple executions, and thus build some grammars with different options to others (such as setting the debug option for instance).