Maven 仅在默认包中生成 Antlr-sources
Maven only generates Antlr-sources in default package
我将从我的 pom.xml:
开始
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>HPK</groupId>
<artifactId>WRB</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>WRB</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4</artifactId>
<version>4.5.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>auto-clean</id>
<phase>initialize</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.3</version>
<executions>
<execution>
<configuration>
<arguments>
<argument>-visitor</argument>
<argument>-package</argument>
<argument>wrb.grammar</argument>
</arguments>
</configuration>
<id>antlr</id>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<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.apache.maven.plugins
</groupId>
<artifactId>
maven-clean-plugin
</artifactId>
<versionRange>
[3.0.0,)
</versionRange>
<goals>
<goal>clean</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
当我在这个项目上运行 maven install
时,maven 应该从 wrb.grammar
包中的 antlr4 插件生成源代码,但它没有。它会做所有事情,但是将源代码放入这些目录中,它只是将它们放在它所谓的 "default-package" 中,这只是 antlr/generated-sources
.
的根目录
如果我通过右键单击语法并在 运行 下选择它来使用 Antlr4IDE 插件,源代码将在正确的目录中生成。
在这个小项目上与我一起工作的另一个人使用 maven-install
没有问题。除了我们的操作系统和eclipse版本,其他都是一样的。
我在 MacOS 上使用 Eclipse Oxygen。
maven-plugin 没有生成我想要的目录,我做错了什么?
我查看了antlr4 4.3版的源码。 -package
参数仅供代码生成器模板使用,但不会在实际工具源代码中的任何地方使用(参见 Github search results for genPackage
)。所以它不会影响输出文件的位置。
相反,每个输出文件的位置是根据相应输入文件的位置确定的(参见 here and here in the sources). This is fits with the explanation in the maven plugin docs:
If your grammar is intended to be part of a package called org.foo.bar then you would place it in the directory src/main/antlr4/org/foo/bar. The plugin will then produce .java and .tokens files in the output directory target/generated-sources/antlr4/org/foo/bar When the Java files are compiled they will be in the correct location for the Javac compiler without any special configuration. The generated java files are automatically submitted for compilation by the plugin.
此外,当使用 antlr4-maven-plugin 时,不需要指定 -package
选项。由于插件从输入文件路径中导出 -package
参数的值,并自动将其添加到 antlr 调用中(请参阅 Maven 插件中的 here in the sources). That is probably also the reason why -pacakge
is not directly available as a configuration parameter。
解决方案
为了将生成的文件放置在与您的包名称匹配的目录结构中,您需要对输入文件使用相同的结构。
基本上,您需要做的就是将语法文件放入 src/main/antlr4/wrb/grammar
,从配置中删除 -package
参数,一切都会按预期工作。
顺便说一句:而不是写作
<arguments>
<argument>-visitor</argument>
</arguments>
你可以简单地写
<visitor>true</visitor>
因为 this parameter 直接被 antlr4-maven-plugin 理解。
我将从我的 pom.xml:
开始<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">
<modelVersion>4.0.0</modelVersion>
<groupId>HPK</groupId>
<artifactId>WRB</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>WRB</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4</artifactId>
<version>4.5.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>auto-clean</id>
<phase>initialize</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.3</version>
<executions>
<execution>
<configuration>
<arguments>
<argument>-visitor</argument>
<argument>-package</argument>
<argument>wrb.grammar</argument>
</arguments>
</configuration>
<id>antlr</id>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<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.apache.maven.plugins
</groupId>
<artifactId>
maven-clean-plugin
</artifactId>
<versionRange>
[3.0.0,)
</versionRange>
<goals>
<goal>clean</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
当我在这个项目上运行 maven install
时,maven 应该从 wrb.grammar
包中的 antlr4 插件生成源代码,但它没有。它会做所有事情,但是将源代码放入这些目录中,它只是将它们放在它所谓的 "default-package" 中,这只是 antlr/generated-sources
.
如果我通过右键单击语法并在 运行 下选择它来使用 Antlr4IDE 插件,源代码将在正确的目录中生成。
在这个小项目上与我一起工作的另一个人使用 maven-install
没有问题。除了我们的操作系统和eclipse版本,其他都是一样的。
我在 MacOS 上使用 Eclipse Oxygen。
maven-plugin 没有生成我想要的目录,我做错了什么?
我查看了antlr4 4.3版的源码。 -package
参数仅供代码生成器模板使用,但不会在实际工具源代码中的任何地方使用(参见 Github search results for genPackage
)。所以它不会影响输出文件的位置。
相反,每个输出文件的位置是根据相应输入文件的位置确定的(参见 here and here in the sources). This is fits with the explanation in the maven plugin docs:
If your grammar is intended to be part of a package called org.foo.bar then you would place it in the directory src/main/antlr4/org/foo/bar. The plugin will then produce .java and .tokens files in the output directory target/generated-sources/antlr4/org/foo/bar When the Java files are compiled they will be in the correct location for the Javac compiler without any special configuration. The generated java files are automatically submitted for compilation by the plugin.
此外,当使用 antlr4-maven-plugin 时,不需要指定 -package
选项。由于插件从输入文件路径中导出 -package
参数的值,并自动将其添加到 antlr 调用中(请参阅 Maven 插件中的 here in the sources). That is probably also the reason why -pacakge
is not directly available as a configuration parameter。
解决方案
为了将生成的文件放置在与您的包名称匹配的目录结构中,您需要对输入文件使用相同的结构。
基本上,您需要做的就是将语法文件放入 src/main/antlr4/wrb/grammar
,从配置中删除 -package
参数,一切都会按预期工作。
顺便说一句:而不是写作
<arguments>
<argument>-visitor</argument>
</arguments>
你可以简单地写
<visitor>true</visitor>
因为 this parameter 直接被 antlr4-maven-plugin 理解。