title: Customize Maven to automatically create ANTLR4 grammar java files on build
body: <p>How can I compile an ANTLR4 grammar as a first step of maven build?</p>
  
  <p>Creating a grammar from *.g4 file manually is pretty straightforward - on linux one needs just to run <code>java -jar /usr/local/lib/antlr-4.0-complete.jar grammarName.g4</code> from console. This creates some java files that need to be compiled in the next step of build.</p>
  
  <p>Is it possible to instruct maven to find all *.g4 files, then to run the above command for every one of them, and then proceed with build further? If grammar compilation fails build should fail also.</p>
  
  <p>Or maybe is there's a plugin that can cope with this task?</p>
  
  <p>I've seen <a href="https://stackoverflow.com/questions/14926060/how-to-automatically-generate-lexerparser-with-antlr4-and-maven">this answer</a> already - the drawback is it is IDE dependent. I want my build to be IDE and system independent as much as possible (and I'm using Netbeans so given answer is unacceptable).</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I've tried benzonico's suggestion. I've added depenedcies as listed in referenced pom.xml from other answer. Here's how my pom.xml looks like:</p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
  &lt;groupId&gt;pl.kajman&lt;/groupId&gt;
  &lt;artifactId&gt;antlr-sandbox&lt;/artifactId&gt;
  &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  &lt;packaging&gt;jar&lt;/packaging&gt;
  
  &lt;name&gt;antlr-sandbox&lt;/name&gt;
  &lt;url&gt;http://maven.apache.org&lt;/url&gt;
  
  &lt;properties&gt;
      &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
  &lt;/properties&gt;
  
  &lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.antlr&lt;/groupId&gt;
          &lt;artifactId&gt;antlr4-maven-plugin&lt;/artifactId&gt;
          &lt;version&gt;4.0&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.tunnelvisionlabs&lt;/groupId&gt;
          &lt;artifactId&gt;antlr4-runtime&lt;/artifactId&gt;
          &lt;version&gt;4.0&lt;/version&gt;
          &lt;scope&gt;compile&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;junit&lt;/groupId&gt;
          &lt;artifactId&gt;junit&lt;/artifactId&gt;
          &lt;version&gt;4.11&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.tunnelvisionlabs&lt;/groupId&gt;
          &lt;artifactId&gt;antlr4&lt;/artifactId&gt;
          &lt;version&gt;4.0&lt;/version&gt;
          &lt;classifier&gt;complete&lt;/classifier&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  &lt;build&gt;
      &lt;sourceDirectory&gt;src&lt;/sourceDirectory&gt;
      &lt;outputDirectory&gt;bin&lt;/outputDirectory&gt;
  
      &lt;plugins&gt;
          &lt;plugin&gt;
              &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
              &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
              &lt;version&gt;3.0&lt;/version&gt;
              &lt;configuration&gt;
                  &lt;source&gt;6&lt;/source&gt;
                  &lt;target&gt;6&lt;/target&gt;
                  &lt;showWarnings&gt;true&lt;/showWarnings&gt;
                  &lt;showDeprecation&gt;true&lt;/showDeprecation&gt;
                  &lt;compilerArguments&gt;
                      &lt;Xlint /&gt;
                  &lt;/compilerArguments&gt;
              &lt;/configuration&gt;
          &lt;/plugin&gt;
  
          &lt;plugin&gt;
              &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
              &lt;artifactId&gt;build-helper-maven-plugin&lt;/artifactId&gt;
              &lt;version&gt;1.7&lt;/version&gt;
              &lt;executions&gt;
                  &lt;execution&gt;
                      &lt;phase&gt;generate-sources&lt;/phase&gt;
                      &lt;goals&gt;
                          &lt;goal&gt;add-source&lt;/goal&gt;
                      &lt;/goals&gt;
                      &lt;configuration&gt;
                          &lt;sources&gt;
                              &lt;source&gt;target/generated-sources/antlr4&lt;/source&gt;
                          &lt;/sources&gt;
                      &lt;/configuration&gt;
                  &lt;/execution&gt;
              &lt;/executions&gt;
          &lt;/plugin&gt;
  
          &lt;plugin&gt;
              &lt;groupId&gt;com.tunnelvisionlabs&lt;/groupId&gt;
              &lt;artifactId&gt;antlr4-maven-plugin&lt;/artifactId&gt;
              &lt;version&gt;4.0&lt;/version&gt;
              &lt;configuration&gt;
                  &lt;sourceDirectory&gt;src&lt;/sourceDirectory&gt;
              &lt;/configuration&gt;
              &lt;executions&gt;
                  &lt;execution&gt;
                      &lt;goals&gt;
                          &lt;goal&gt;antlr4&lt;/goal&gt;
                      &lt;/goals&gt;
                  &lt;/execution&gt;
              &lt;/executions&gt;
          &lt;/plugin&gt;
      &lt;/plugins&gt;
  &lt;/build&gt;
  </code></pre>
  
  <p></p>
  
  <p>Java files are automatically generated form g4 file, but unfortunately there's a lot of errors. They are very similar to those I've had when compiling manually and had wrong classpath. Some of the errors are:</p>
  
  <pre><code>/home/kajman/projects/antlr/antlr-sandbox/target/generated-sources/antlr4/main/java/pl/kajman/antlr/sandbox/grammars/ExprListener.java:[6,56] type org.antlr.v4.runtime.tree.ParseTreeListener does not take parameters
  
  /home/kajman/projects/antlr/antlr-sandbox/target/generated-sources/antlr4/main/java/pl/kajman/antlr/sandbox/grammars/ExprParser.java:[44,32] cannot find symbol
  symbol:   method 
  getRuleContexts(java.lang.Class&lt;main.java.pl.kajman.antlr.sandbox.grammars.ExprParser.StatContext&gt;)
  location: class main.java.pl.kajman.antlr.sandbox.grammars.ExprParser.ProgContext
  </code></pre>