Configuring InteliJ IDEA for ANTLR 4 with Maven
Steps to configure InteliJ for using the ANTLR4 language with automated builds via Maven. This is as of 10 June, 2016 with IntelliJ IDEA Ultimate 2016.1 and Antlr 4.3. I’m using Ubuntu 16.04, but that probably doesn’t matter.
1) Download and install the ANTLR4 plugin. This will give you syntax highlighting, etc. You could also use this for building the grammer, but we’re going to use the Maven tool for that instead. Go to File | Settings... | Plugins | Browse Repositories | Antlr v4 Grammer Plugin to make the changes as shown below.
2) Set up a new maven project. In that project we’re going to add two folder hierarchies. The first is src/main/antlr4. This will hold the ANTLR grammers. You put each grammer in a subdirectory corresponding to the package you want the Java grammer files to be generated to. In this case, I’m creating a grammer called com.hablutzel.jutzo.parser.Jutzo, so the file hierarchy looks like the below.
Doing thing this way allows you to keep the ANTLR 4 sources separate from the Java sources, and also keeps them separate from the Java classes that ANTLR will generate. However, we still have to compile the ANTLR grammar. You can do that manually, when you have the grammar open (CTRL-SHIFT-G), but we want this to happen manually. We’ll do that with a Maven build plugin in the next step.
(3) Configure the pom.xml file to build the ANTLR grammar into Java files. This can be done with the antlr4-maven-plugin, but there is an undocumented feature that lets you change where the files get generated. Normally they go to the target/generated-sources folder, but InteliJ IDEA maven projects are generally set up to exclude the entire target directory, so you can’t add the generated-sources as a source folder. Instead, we’ll change the source folder using the <outputDirectory> configuration element so that it points to gen/main/antlr4 instead; the actual Java files generated will be in the right package structure under that directory (so the grammar Jutzo.g4 above will go to gen/main/antlr4/com/hablutzel/jutzo/parser/*)
<build>
<plugins>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.5.3</version>
<configuration>
<outputDirectory>gen/main/antlr4</outputDirectory>
</configuration>
<executions>
<execution>
<id>antlr4</id>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
The build plugin requires Maven 3, so you have to change that. In File | Settings | Maven make sure you’re using Maven 3:
(4) OK, now we have to add the gen/main/antlr4 as a source directory. Do this with File | Project Structure | Modules | Sources, as shown below.
(5) Almost there. We have the Maven target, but nothing to invoke it when we do a build. In order to do that, we have to add a maven goal task as the first step of the make. In Run | Edit Configurations, add “Run Maven Goal” a before launch task (the little green +). Given the above build plugin configuration, which has an execution ID of “antlr4″ and a goal of “antlr4″, the poorly named “command line”, which is really the maven goal, is “antlr4:antlr4″. (That’s executionid:goal).
Make sure that goal is the first before launch task:
(6) [Optional] if you want to make sure that the ctrl-shift-G generates the grammar in the same place, open the grammar, right-shift in the editor window, and select “Configure ANTLR...”. Change the output directory to the fully qualified path name under the gen/main/antlr4 directory - in this example, you have to use the entire gen/main/antlr4/com/hablutzel/jutzo/parser path. You only have to do this if you want to use ctrl-shift-G to generate the grammar.
This is useful if you want to build the project without running it, as I haven’t figured out how to make the build trigger the maven task. If you figure that out, please comment and I’ll add it.
(7) Finally you have to make sure that the project has ANTLR support at runtime. Back to the pom file, and add the dependency:
<dependencies>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4</artifactId>
<version>4.5.3</version>
</dependency>
</dependencies>
That should be everything.