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>
    <version>1.1.0</version>
        <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>org.xerial.thirdparty</groupId>
      <artifactId>org.antlr.antlr3</artifactId>
      <version>3.1.1</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