What is the format for specifying a package in the Antlr4 maven plugin?
Asked Answered
P

6

12

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]
Pyromagnetic answered 14/8, 2013 at 22:26 Comment(0)
B
7

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

Besprinkle answered 14/8, 2013 at 22:42 Comment(1)
Worked for me after adding the option minus sign: <argument>-package</argument>Jaf
W
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>
Wright answered 14/8, 2013 at 23:14 Comment(0)
G
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>
Guillerminaguillermo answered 25/2, 2016 at 11:1 Comment(0)
S
2

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.

Swine answered 29/1, 2020 at 10:8 Comment(3)
I created a bug report: github.com/antlr/antlr4/issues/2738Swine
This should be a different questionFlea
Moving src/main/antlr4/de/schmitz/Demo.g4 to src/main/antlr4/my/package/Demo.g4 is okay to me. Anyway, this answer shows me the best folder layout.Cynosure
B
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;
}
Bini answered 16/5, 2014 at 12:39 Comment(0)
C
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.

Coating answered 8/4, 2016 at 21:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.