# Chapter 29. The Antlr Plugin

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

## 29.1. Usage

To use the Antlr plugin, include in your build script:

Example 29.1. Using the Antlr plugin

`build.gradle`

    apply plugin: 'antlr'

## 29.2. Tasks

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

Table 29.1. Antlr plugin - tasks

Task name | Depends on | Type | Description
`generateGrammarSource` | - | `AntlrTask` | Generates the source files for all production Antlr grammars.
`generateTestGrammarSource` | - | `AntlrTask` | Generates the source files for all test Antlr grammars.
`generate`SourceSet`GrammarSource` | - | `AntlrTask` | 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 29.2. Antlr plugin - additional task dependencies

Task name | Depends on
compileJava | generateGrammarSource
compileTestJava | generateTestGrammarSource
compile`SourceSet`Java | generate`SourceSet`GrammarSource

## 29.3. Project layout

Table 29.3. Antlr plugin - project layout

Directory | Meaning
`src/main/antlr` | Production Antlr grammar files.
`src/test/antlr` | Test Antlr grammar files.
`src/`sourceSet`/antlr` | Antlr grammar files for the given source set.

## 29.4. Dependency management

The Antlr plugin adds an `antlr` dependency configuration. You use this to declare the version of Antlr you wish to use.

Example 29.2. Declare Antlr version

`build.gradle`

    repositories {
        mavenCentral()
    }

    dependencies {
        antlr 'antlr:antlr:2.7.7'
    }

## 29.5. Convention properties

The Antlr plugin does not add any convention properties.

## 29.6. Source set properties

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

Table 29.4. Antlr plugin - source set properties

Property name | Type | Default value | Description
`antlr` | `SourceDirectorySet` (read-only) | Not null | The Antlr grammar files of this source set. Contains all `.g` found in the Antlr source directories, and excludes all other types of files.
`antlr.srcDirs` | `Set<File>`. Can set using anything described in Section 15.5, “Specifying a set of input files”. | `[`projectDir`/src/`name`/antlr]` | The source directories containing the Antlr grammar files of this source set.