10

What is the format for specifying a package in the Antlr4 maven plugin antlr4-maven-plugin?

I feel like I should be able to do the following:

<plugin>
    <groupId>com.tunnelvisionlabs</groupId>
    <artifactId>antlr4-maven-plugin</artifactId>
    <version>4.0</version>
    <configuration>
        <arguments>package my.package.name</arguments>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>antlr4</goal>
            </goals>
        </execution>
    </executions>
</plugin>

but that results in the following error:

[ERROR] Failed to execute goal com.tunnelvisionlabs:antlr4-maven-plugin:4.0:antlr4 (default) on project my_project: Unable to parse configuration of mojo com.tunnelvisionlabs:antlr4-maven-plugin:4.0:antlr4 for parameter arguments: Cannot assign configuration entry 'arguments' with value 'package my.package.name' of type java.lang.String to property of type java.util.List -> [Help 1]
Brian Kent
  • 3,754
  • 1
  • 26
  • 31

6 Answers6

9

If I am you, I will make a maven project per package and try this

<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.0</version>
<configuration>
    <sourceDirectory>${basedir}/src</sourceDirectory>
</configuration>
<executions>
    <execution>
        <goals>
            <goal>antlr4</goal>
        </goals>
    </execution>
</executions>
</plugin>

but usually, When I pass an argument in maven configuration, I do the following. but I am not sure of that syntax in antlr4

<plugin>
<groupId>com.tunnelvisionlabs</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.0</version>
<configuration>
    <arguments>
      <argument>-package</argument>
      <argument>my.package.name</argument>

   </arguments>
</configuration>
<executions>
    <execution>
        <goals>
            <goal>antlr4</goal>
        </goals>
    </execution>
</executions>
</plugin>

Edit: Notice the - in front of package so the antlr-maven-plugin will recognize it as a parameter

oruckdeschel
  • 88
  • 1
  • 8
mebada
  • 2,252
  • 4
  • 27
  • 35
7

The package is automatically determined based on the location of the file in your project, similar to the way the package is determined for Java files. The output is also placed in a location determined by the location of the source file. To change the package where the code is generated, you'll need to move the grammar file.

Other arguments can be specified like this:

<arguments>
  <argument>arg1</argument>
  <argument>arg2</argument>
</arguments>
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
5

Your configuration arguments syntax is wrong.

Please change the configuration of antlr4-maven-plugin from

<configuration>
    <arguments>package my.package.name</arguments>
</configuration>

to:

<configuration>
    <arguments>
        <argument>-package</argument>
        <argument>my.package.name</argument>
    </arguments>
</configuration>
dongpf
  • 1,537
  • 16
  • 8
1

In order to add package information to the generated code you must add the following annotation to the g4 file:

@header {
    package com.this.is.my.package;
}
Renato Mendes
  • 68
  • 1
  • 7
1

I tried

@header {
    package com.this.is.my.package;
}

but when you have imports it adds package line for each file imported and as a result compiler errors raised in generated file. You have to be careful to add @header so file with imported grammars had only one package line. I think it's a bug.

skypjack
  • 49,335
  • 19
  • 95
  • 187
Mike
  • 11
  • 1
1

I have the Demo.g4 inside src/main/antlr4/de/schmitz.

Now the classes are generated to target/generated-sources/antlr4/de/schmitz.

The package is de.schmitz.

Everything is correct.

Now I want to change the package and folders of the generated classes (actually NOT moving my Demo.g4):

<arguments>
    <argument>-package</argument>
    <argument>my.package</argument>
</arguments>

The classes are generated to target/generated-sources/antlr4/de/schmitz.

The package is my.package.

This cannot be compiled, so it could be an error.

So I want to change the output directory:

<outputDirectory>${project.build.directory}/generated-sources/antlr4/my/package</outputDirectory>

Now it get's buggy.

The package is my.package.

But the folder is target/generated-sources/antlr4/my/package/de/schmitz.

So it's concatenated instead of overwritten.

Marco Schmitz
  • 219
  • 4
  • 8