1
votes

When I build the maven Module that is using Antlr4 and look in the Jar the compiled generated sources from Antlr4 are in the Root folder. But when I want to include the Jar with the generated sources in my Antlr4-impl module as dependency where I want to use the generated classes they can't be found. How I can link them up correctly ? Do I have to move the generated Sources to the maven src/main/java section ? Do they need a package ? If yes, how can I set the package for the sources in the maven plugin ?

Here is My pom.xml form the antlr generation module:

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>net-site-arti</artifactId>
        <groupId>net.site.reverse</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>net-site-arti-antlr</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.antlr</groupId>
            <artifactId>antlr4-runtime</artifactId>
            <version>4.7.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.antlr</groupId>
                <artifactId>antlr4-maven-plugin</artifactId>
                <version>4.7.1</version>
                <configuration>
                    <outputDirectory>src/main/generated-sources</outputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>antlr4</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
             <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>src/main/generated-sources</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

And here is the pom.xml form the module where I want to implement the classes:

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>net-site-arti</artifactId>
        <groupId>net.site.reverse</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>net-site-arti-antlr-impl</artifactId>

    <dependencies>
        <dependency>
            <groupId>net.site.reverse</groupId>
            <artifactId>net-site-arti-antlr</artifactId>
            <version>${project.version}</version>
        </dependency>

            <dependency>
                <groupId>org.antlr</groupId>
                <artifactId>antlr4-runtime</artifactId>
                <version>4.7.1</version>
            </dependency>

    </dependencies>
</project>

EDIT: Add build-helper-maven-plugin same result - the classes are in the root folder from the generated jar but the module that will use them don't pick up the classes.

Tried also with the maven-shade-plugin in this configuration

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <relocations>
                                <relocation>
                                    <pattern></pattern>
                                    <shadedPattern>org.shaded.plexus.util.</shadedPattern>
                                    <excludes>
                                        <exclude>org.codehaus.plexus.util.xml.Xpp3Dom</exclude>
                                        <exclude>org.codehaus.plexus.util.xml.pull.*</exclude>
                                    </excludes>
                                </relocation>
                            </relocations>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

different jar but some result - in the jar the classes are under: org/shaded/plexus/util/ but the module can't import the classes.

1
You may find the answers here helpful. It describes how to add anextra source directory, which you can use to include your generated antlr sources. stackoverflow.com/questions/9752972/…PiRocks

1 Answers

0
votes

Not a perfect solution but a solution:

In this post they say you can add the package name in a @header tag in the *.g-File. Unfortunately ANTLR doesn't generate the package in the Sourcefolder it only generated the Class-Files with the package declaration. So you have to generate them in the package:

here is an example pom:

<plugin>
                <groupId>org.antlr</groupId>
                <artifactId>antlr4-maven-plugin</artifactId>
                <version>4.7.1</version>
                <configuration>
                    <outputDirectory>src/main/java/net/site/antlr</outputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>antlr4</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

And if you add this to the g-File:

@header {
    package net.site.antlr;
}

But you can't generate, with this solution, different grammar in different packages.