Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 18 Next »

Maven plugin for ANTLR 3

New plugin pages

These notes refer to an old version of the maven plugin. If you are using ANTLR 3.1.3 or above see the new plugin pages

If you are using Maven as your build management tool there is an ANTLRv3 plugin available. Add the following to the plugins section of your pom.xml

            <plugin>
                <groupId>org.antlr</groupId>
                <artifactId>antlr3-maven-plugin</artifactId>
                <version>3.1.1.2</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>antlr</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

Note: This plugin has been derived from an earlier one at org.codehaus.mojo with the same artifactId. You need to use the org.antlr plugin if you want support for ANTLR 3.1.x features. Thanks to Jim Idle and Jared Bunting for the new plugin.

Packages and grammar files

The plugin will look for grammar files under src/main/antlr. For example, a project with a package org.eatmoreveg.parser could have Java source files and ANTLR grammar files arranged like this...

 base.dir
     +--pom.xml
     +--src
        |
        +--main
           |
           +--java
           |  |
           |  +--org
           |     |
           |     +--eatmoreveg
           |        |
           |        +--VegApp.java
           |
           +--antlr
              |
              +--org
                 |
                 +--eatmoreveg
                    |
                    +--parser
                       |
                       +--Veg.g

In the grammar file...

@header {
package org.eatmoreveg.parser;
}

@lexer::header {
package org.eatmoreveg.parser;
}

When you build your project, the classes generated by ANTLR will be written to target/generated-sources/org/eatmoreveg/parser.

In VegApp.java...

package org.eatmoreveg;

import org.eatmoreveg.parser.VegLexer;
import org.eatmoreveg.parser.VegParser;

Tips and tricks

If you are using Netbeans you can install the Maven plugin which allows you to open, create and build Maven projects from within the IDE.

If you want to share a tokens file between grammars you will need to set the libDirectory variable to tell the plugin where to find it, as in this example...

    <plugin>
        <groupId>org.antlr</groupId>
        <artifactId>antlr3-maven-plugin</artifactId>
        <version>3.1.1.2</version>
        <configuration>
            <libDirectory>target/generated-sources/antlr/myorg/myproject/mypackage</libDirectory>
        </configuration>
        <executions>
            <execution>
                <phase>generate-sources</phase>
                <goals>
                     <goal>antlr</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
  • No labels