【问题标题】:Antlr4 in own Maven moduleAntlr4 在自己的 Maven 模块中
【发布时间】:2024-04-30 02:35:02
【问题描述】:

当我构建使用 Antlr4 的 maven 模块并查看 Jar 时,从 Antlr4 编译生成的源代码位于根文件夹中。 但是,当我想在我的 Antlr4-impl 模块中包含带有生成源的 Jar 作为我想要使用生成的类的依赖项时,找不到它们。 如何正确链接它们?我是否必须将生成的 Sources 移动到 maven src/main/java 部分?他们需要一个包裹吗?如果是,如何在 maven 插件中设置源的包?

这是我的 antlr 生成模块的 pom.xml:

<?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>

这是我要实现类的模块的 pom.xml:

<?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>

编辑:添加 build-helper-maven-plugin 相同的结果 - 类位于生成的 jar 的根文件夹中,但将使用它们的模块不会选择这些类。

在此配置中也尝试使用 maven-shade-plugin

<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>

不同的 jar,但有一些结果 - 在 jar 中,类位于:org/shaded/plexus/util/ 但模块无法导入类。

【问题讨论】:

  • 您可能会发现这里的答案很有帮助。它描述了如何添加一个额外的源目录,您可以使用它来包含您生成的 antlr 源。 *.com/questions/9752972/…

标签: java maven pom.xml antlr4


【解决方案1】:

不是一个完美的解决方案,而是一个解决方案:

this post 中,他们说您可以在 *.g 文件的 @header 标记中添加包名称。不幸的是,ANTLR 不会在 Sourcefolder 中生成包,它只生成带有包声明的类文件。所以你必须在包中生成它们:

这是一个 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>

如果你把它添加到 g 文件中:

@header {
    package net.site.antlr;
}

但是你不能用这个解决方案在不同的包中生成不同的语法。

【讨论】: