The ANTLR plugin extends the Java plugin to add support for generating parsers using ANTLR.

The ANTLR plugin supports ANTLR version 2, 3 and 4.


To use the ANTLR plugin, include the following in your build script:

Example: Using the ANTLR plugin

apply plugin: 'antlr'


The ANTLR plugin adds a number of tasks to your project, as shown below.


Generates the source files for all production ANTLR grammars.


Generates the source files for all test ANTLR grammars.


Generates the source files for all ANTLR grammars for the given source set.

The ANTLR plugin adds the following dependencies to tasks added by the Java plugin.

Table 1. ANTLR plugin - additional task dependencies
Task name Depends on







Project layout


Production ANTLR grammar files. If the ANTLR grammar is organized in packages, the structure in the antlr folder should reflect the package structure. This ensures that the generated sources end up in the correct target subfolder.


Test ANTLR grammar files.


ANTLR grammar files for the given source set.

Dependency management

The ANTLR plugin adds an antlr dependency configuration which provides the ANTLR implementation to use. The following example shows how to use ANTLR version 3.

Example: Declare ANTLR version

repositories {

dependencies {
    antlr "org.antlr:antlr:3.5.2" // use ANTLR version 3
    // antlr "org.antlr:antlr4:4.5" // use ANTLR version 4

If no dependency is declared, antlr:antlr:2.7.7 will be used as the default. To use a different ANTLR version add the appropriate dependency to the antlr dependency configuration as above.

Convention properties

The ANTLR plugin does not add any convention properties.

Source set properties

The ANTLR plugin adds the following properties to each source set in the project.


The ANTLR grammar files of this source set. Contains all .g or .g4 files found in the ANTLR source directories, and excludes all other types of files. Default value is non-null.


The source directories containing the ANTLR grammar files of this source set. Can set using anything that implicitly converts to a file collection. Default value is [projectDir/src/name/antlr].

Controlling the ANTLR generator process

The ANTLR tool is executed in a forked process. This allows fine grained control over memory settings for the ANTLR process. To set the heap size of an ANTLR process, the maxHeapSize property of AntlrTask can be used. To pass additional command-line arguments, append to the arguments property of AntlrTask.

Example: Setting custom max heap size and extra arguments for ANTLR

generateGrammarSource {
    maxHeapSize = "64m"
    arguments += ["-visitor", "-long-messages"]