Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Maven plugin for ANTLR 3

...

titleNew plugin pages

...

Please refer to

...

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...

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

In the grammar file...

Code Block
@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...

Code Block
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...

...

the Official ANTLR3 Maven documentation