Possible to use Maven to generate Java sources from grammar?

439 views
Skip to first unread message

bri...@gmail.com

unread,
May 24, 2013, 5:55:05 PM5/24/13
to antlr-di...@googlegroups.com
Is there a way to have Maven generate the Java classes from the grammar?

This is what I have done. I wrote my grammar, produces the java classes using the following command using the following:

antlr4 FixedField.g4 -visitor -package fixedfield

I implemented my visitor stuff, put maven in my dependencies, and now my project works. Yet, I have to manually run the antlr command with arguments if I want to regenerate the Java classes. Is there a way to have Maven do this?

brian

Brian Lavender

unread,
May 27, 2013, 2:02:05 PM5/27/13
to antlr-di...@googlegroups.com
Clarify my question a bit.

On Friday, May 24, 2013 2:55:05 PM UTC-7, Brian Lavender wrote:
Is there a way to have Maven generate the Java classes from the grammar?

I wrote my grammar and produced the java classes using the following command:

antlr4 FixedField.g4 -visitor -package fixedfield

 Is there a way to have Maven do this?

brian

Jim Idle

unread,
May 27, 2013, 9:44:04 PM5/27/13
to antlr-di...@googlegroups.com
Brian,

Assuming that you know the standard layout for a Maven project, then place your .g4 files under:

src/main/antlr4/your/package/path/

In your pom, add:

<!-- Our generated parsers require the ANTLR4 runtime -->
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>${antlr-version}</version>
</dependency>


And:

<plugin>

<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.0</version>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>


Then when you run:

mvn install

Or similar, the .g4 files will be found underneath the src/antlr4 directory and the output will be placed in target/generated-sources/your/package/path and the java files will be automatically included in the java compilation phase. 

This is basically very standard maven configuration. 

If you are using Eclipse as your IDE, then you can also add:

<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings 
only. It has no influence on the Maven build itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<versionRange>[4.0,)</versionRange>
<goals>
<goal>antlr4</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute></execute>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>

Which will tell the IDE what is going on with the ANTLR plugin and stop Eclipse complaining about not knowing the lifecycle for ANTLR.

Jim



--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Brian Lavender

unread,
May 29, 2013, 3:38:41 AM5/29/13
to antlr-di...@googlegroups.com
Jim,

Thank you for providing the info for adding the antlr4 maven plugin. I added it to my pom.xml and it did generate the java sources, yet it did not generate the FixedFieldBaseVisitor class which I usually specify the "-visitor" option to antlr4 to generate. Is there a way to tell the plug-in to generate the base visitor class?

brian

Brian Lavender

unread,
May 29, 2013, 4:11:35 AM5/29/13
to antlr-di...@googlegroups.com
After a little digging, I discovered I could add a configuration parameter to the plug-in. It now produces the base visitor classes too! Below shows the update I made to the configuration settings in my pom.xml


        <plugin>

      <groupId>org.antlr</groupId>
        <artifactId>antlr4-maven-plugin</artifactId>
        <version>4.0</version>
        <executions>
        <execution>
        <goals>
        <goal>antlr4</goal>
        </goals>
        <configuration>
                <visitor>true</visitor>
        </configuration>
        </execution>
        </executions>
        </plugin>
Reply all
Reply to author
Forward
0 new messages