Maven ANTLR Plugin

Here, I explain how to use maven ANTLR (version 3.x) plug-in using a simple example.

Add the following description to your pom.xml file:

  <!-- 
    with this configurartion, from your ANTLR3 grammar files (*.g) in the folder
    src/main/java/**/*.g, Java lexer/parser/treewalker files will be generated.
  -->
  <build>
    <plugins>
      <plugin>
        <groupId>org.xerial.maven</groupId>
        <artifactId>maven-antlr-plugin</artifactId>
        <executions>
          <execution>
            <phase>generate-sources</phase>
            <goals>
              <goal>generate</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <sourceDirectory>${basedir}/src/main/java</sourceDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <!-- dependencies required to run ANTLR-generated lexer/parser -->
  <dependencies>
    <dependency>
      <groupId>antlr</groupId>
      <artifactId>antlr</artifactId>
      <version>2.7.7</version>
    </dependency>
    <dependency>
      <groupId>org.antlr</groupId>
      <artifactId>antlr</artifactId>
      <version>3.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.antlr</groupId>
      <artifactId>antlr-runtime</artifactId>
      <version>3.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.antlr</groupId>
      <artifactId>stringtemplate</artifactId>
      <version>3.1-b1</version>
    </dependency>
  </dependencies>

How to Regenerate Lexer/Parsers from the Grammar

Run the following command in your project folder, in which your pom.xml is placed:

> mvn generate-sources