maven-antrun-plugin:为 protobuf 生成源代码不生成 Java 个文件
maven-antrun-plugin: generate sources for protobuf does not generate Java files
我的 pom.xml
中有以下插件,它应该生成编译其他项目所需的 java 文件:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<archive>
<manifestFile>META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<tasks>
<mkdir dir="src-gen" />
<exec executable="protoc">
<arg value="--java_out=src-gen" />
<arg value="proto/ros/RosTime.proto" />
<arg value="proto/ros/RosHeader.proto" />
<arg value="proto/ros/RosPoint.proto" />
<arg value="proto/ros/RosPose.proto" />
<arg value="proto/ros/RosPoseStamped.proto" />
<arg value="proto/ros/RosQuaternion.proto" />
<arg value="proto/U.proto" />
<arg value="proto/S.proto" />
<arg value="proto/Z.proto" />
<arg value="proto/algorithm/A.proto" />
<arg value="proto/algorithm/B.proto" />
<arg value="proto/algorithm/C.proto" />
<arg value="proto/algorithm/D.proto" />
<arg value="proto/algorithm/E.proto" />
<arg value="proto/algorithm/F.proto" />
<arg value="proto/env/G.proto" />
<arg value="proto/env/H.proto" />
<arg value="proto/env/J.proto" />
<arg value="proto/env/K.proto" />
<arg value="proto/env/L.proto" />
</exec>
</tasks>
<sourceRoot>src-gen</sourceRoot>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
但是,这根本不会在 src-gen 文件夹下生成任何东西。然后我不断收到错误,例如:
cannot find symbol
[ERROR] symbol: class AMsg
[ERROR] location: class project.messages.AFormatter
和
package project.messages.ros.RosHeader does not exist
package project.messages.ros.RosPoint does not exist
package project.messages.ros.RosPose does not exist
package project.messages.ros.RosPoseStamped does not
基本上它找不到必要的文件,自然而然地报错。我已经安装了2.5.0版本,依赖定义正确:
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.5.0</version>
</dependency>
我 运行 没主意了。到底是什么导致它不起作用?
编辑: 以下内容在 mvn install -X
的调试输出中引起了我的注意:
Execute:Java13CommandLauncher: Executing 'protoc' with arguments:
'--java_out=src-gen'
'proto/ros/RosTime.proto'
'proto/ros/RosHeader.proto'
'proto/ros/RosPoint.proto'
'proto/ros/RosPose.proto'
'proto/ros/RosPoseStamped.proto'
'proto/ros/RosQuaternion.proto'
.
.
.
.
The ' characters around the executable and arguments are
not part of the command.
[exec] ros/RosTime.proto: File not found.
[exec] proto/ros/RosHeader.proto: Import "ros/RosTime.proto" was not found or had errors.
[exec] proto/ros/RosHeader.proto:6:14: "project.messages.ros.RosTimeMsg" seems to be defined in "proto/ros/RosTime.proto", which is not imported by "proto/ros/RosHeader.proto". To use it here, please add the necessary import.
.
.
.
[INFO] --- maven-antrun-plugin:1.3:run (generate-sources) @ X.messages ---
[DEBUG] org.apache.maven.plugins:maven-antrun-plugin:jar:1.3:
[DEBUG] org.apache.maven:maven-plugin-api:jar:2.0.4:compile
[DEBUG] org.apache.maven:maven-project:jar:2.0.4:compile
[DEBUG] org.apache.maven:maven-settings:jar:2.0.4:compile
[DEBUG] org.apache.maven:maven-profile:jar:2.0.4:compile
[DEBUG] org.apache.maven:maven-model:jar:2.0.4:compile
[DEBUG] org.apache.maven:maven-artifact-manager:jar:2.0.4:compile
[DEBUG] org.apache.maven:maven-repository-metadata:jar:2.0.4:compile
[DEBUG] org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9:compile
[DEBUG] junit:junit:jar:3.8.1:compile
[DEBUG] classworlds:classworlds:jar:1.1-alpha-2:compile
[DEBUG] org.apache.maven:maven-artifact:jar:2.0.4:compile
[DEBUG] org.codehaus.plexus:plexus-utils:jar:1.5.6:compile
[DEBUG] org.apache.ant:ant-launcher:jar:1.7.1:runtime
[DEBUG] org.apache.ant:ant:jar:1.7.1:compile
[DEBUG] Created new class realm plugin>org.apache.maven.plugins:maven-antrun-plugin:1.3
[DEBUG] Importing foreign packages into class realm plugin>org.apache.maven.plugins:maven-antrun-plugin:1.3
[DEBUG] Imported: < project>runtime_shared:art.util.robot:1.0.0
[DEBUG] Populating class realm plugin>org.apache.maven.plugins:maven-antrun-plugin:1.3
[DEBUG] Included: org.apache.maven.plugins:maven-antrun-plugin:jar:1.3
[DEBUG] Included: junit:junit:jar:3.8.1
[DEBUG] Included: org.codehaus.plexus:plexus-utils:jar:1.5.6
[DEBUG] Included: org.apache.ant:ant-launcher:jar:1.7.1
[DEBUG] Included: org.apache.ant:ant:jar:1.7.1
[DEBUG] Excluded: org.apache.maven:maven-plugin-api:jar:2.0.4
[DEBUG] Excluded: org.apache.maven:maven-project:jar:2.0.4
[DEBUG] Excluded: org.apache.maven:maven-settings:jar:2.0.4
[DEBUG] Excluded: org.apache.maven:maven-profile:jar:2.0.4
[DEBUG] Excluded: org.apache.maven:maven-model:jar:2.0.4
[DEBUG] Excluded: org.apache.maven:maven-artifact-manager:jar:2.0.4
[DEBUG] Excluded: org.apache.maven:maven-repository-metadata:jar:2.0.4
[DEBUG] Excluded: org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9
[DEBUG] Excluded: classworlds:classworlds:jar:1.1-alpha-2
[DEBUG] Excluded: org.apache.maven:maven-artifact:jar:2.0.4
[DEBUG] Configuring mojo org.apache.maven.plugins:maven-antrun-plugin:1.3:run from plugin realm ClassRealm[plugin>org.apache.maven.plugins:maven-antrun-plugin:1.3, parent: sun.misc.Launcher$AppClassLoader@5c647e05]
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-antrun-plugin:1.3:run' with override configurator -->
[DEBUG] (f) pluginArtifacts = [org.apache.maven.plugins:maven-antrun-plugin:maven-plugin:1.3:, junit:junit:jar:3.8.1:compile, org.codehaus.plexus:plexus-utils:jar:1.5.6:compile, org.apache.ant:ant-launcher:jar:1.7.1:runtime, org.apache.ant:ant:jar:1.7.1:compile]
[DEBUG] (f) project = MavenProject: runtime_X:X.messages:1.0.0 @ /home/usr/workspace_runtime_X/X.messages/pom.xml
[DEBUG] (f) sourceRoot = /home/usr/workspace_runtime_X/X.messages/src-gen
[DEBUG] (f) tasks =
[DEBUG] -- end configuration --
如果对文件未创建有疑问,请确保使用绝对路径。在这种情况下,${basedir}
允许我们获取 Maven 基本目录位置(POM 的位置)的绝对路径:
<mkdir dir="${basedir}/src-gen" />
<exec executable="protoc">
<arg value="--java_out=${basedir}/src-gen" />
<arg value="--proto_path=${basedir}" /> <!-- proto_path needs to point to where the proto files are -->
<arg value="proto/ros/RosTime.proto" />
<!-- rest of proto files... -->
</exec>
然后,请注意 maven-antrun-plugin
的 <sourceRoot>
参数已弃用:
Deprecated. Use the build-helper-maven-plugin to bind source directories
按照建议,您应该使用该目标的 build-helper-maven-plugin:add-source
goal to add a source folder to your Maven project. Configure the folders to add with the sources
参数。
话虽如此,您可能更有机会使用 org.xolstice.maven.plugins:protobuf-maven-plugin
而不是依赖 Ant 任务:
Maven Protocol Buffers Plugin uses Protocol Buffer Compiler (protoc
) tool to generate Java source files from .proto
(protocol buffer definition) files for the specified project.
compile
goal can be used to generate the Java source files. Placing your .proto
files under src/main/proto
(configurable with the protoSourceRoot
parameter), the plugin will generate the Java source files inside ${project.build.directory}/generated-sources/protobuf/java
(configurable with the outputDirectory
参数)。
如果 protoc
可执行文件不在 PATH 环境变量中,您可以使用 protocExecutable
参数设置它。
我的 pom.xml
中有以下插件,它应该生成编译其他项目所需的 java 文件:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<archive>
<manifestFile>META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<tasks>
<mkdir dir="src-gen" />
<exec executable="protoc">
<arg value="--java_out=src-gen" />
<arg value="proto/ros/RosTime.proto" />
<arg value="proto/ros/RosHeader.proto" />
<arg value="proto/ros/RosPoint.proto" />
<arg value="proto/ros/RosPose.proto" />
<arg value="proto/ros/RosPoseStamped.proto" />
<arg value="proto/ros/RosQuaternion.proto" />
<arg value="proto/U.proto" />
<arg value="proto/S.proto" />
<arg value="proto/Z.proto" />
<arg value="proto/algorithm/A.proto" />
<arg value="proto/algorithm/B.proto" />
<arg value="proto/algorithm/C.proto" />
<arg value="proto/algorithm/D.proto" />
<arg value="proto/algorithm/E.proto" />
<arg value="proto/algorithm/F.proto" />
<arg value="proto/env/G.proto" />
<arg value="proto/env/H.proto" />
<arg value="proto/env/J.proto" />
<arg value="proto/env/K.proto" />
<arg value="proto/env/L.proto" />
</exec>
</tasks>
<sourceRoot>src-gen</sourceRoot>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
但是,这根本不会在 src-gen 文件夹下生成任何东西。然后我不断收到错误,例如:
cannot find symbol
[ERROR] symbol: class AMsg
[ERROR] location: class project.messages.AFormatter
和
package project.messages.ros.RosHeader does not exist
package project.messages.ros.RosPoint does not exist
package project.messages.ros.RosPose does not exist
package project.messages.ros.RosPoseStamped does not
基本上它找不到必要的文件,自然而然地报错。我已经安装了2.5.0版本,依赖定义正确:
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.5.0</version>
</dependency>
我 运行 没主意了。到底是什么导致它不起作用?
编辑: 以下内容在 mvn install -X
的调试输出中引起了我的注意:
Execute:Java13CommandLauncher: Executing 'protoc' with arguments:
'--java_out=src-gen'
'proto/ros/RosTime.proto'
'proto/ros/RosHeader.proto'
'proto/ros/RosPoint.proto'
'proto/ros/RosPose.proto'
'proto/ros/RosPoseStamped.proto'
'proto/ros/RosQuaternion.proto'
.
.
.
.
The ' characters around the executable and arguments are
not part of the command.
[exec] ros/RosTime.proto: File not found.
[exec] proto/ros/RosHeader.proto: Import "ros/RosTime.proto" was not found or had errors.
[exec] proto/ros/RosHeader.proto:6:14: "project.messages.ros.RosTimeMsg" seems to be defined in "proto/ros/RosTime.proto", which is not imported by "proto/ros/RosHeader.proto". To use it here, please add the necessary import.
.
.
.
[INFO] --- maven-antrun-plugin:1.3:run (generate-sources) @ X.messages ---
[DEBUG] org.apache.maven.plugins:maven-antrun-plugin:jar:1.3:
[DEBUG] org.apache.maven:maven-plugin-api:jar:2.0.4:compile
[DEBUG] org.apache.maven:maven-project:jar:2.0.4:compile
[DEBUG] org.apache.maven:maven-settings:jar:2.0.4:compile
[DEBUG] org.apache.maven:maven-profile:jar:2.0.4:compile
[DEBUG] org.apache.maven:maven-model:jar:2.0.4:compile
[DEBUG] org.apache.maven:maven-artifact-manager:jar:2.0.4:compile
[DEBUG] org.apache.maven:maven-repository-metadata:jar:2.0.4:compile
[DEBUG] org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9:compile
[DEBUG] junit:junit:jar:3.8.1:compile
[DEBUG] classworlds:classworlds:jar:1.1-alpha-2:compile
[DEBUG] org.apache.maven:maven-artifact:jar:2.0.4:compile
[DEBUG] org.codehaus.plexus:plexus-utils:jar:1.5.6:compile
[DEBUG] org.apache.ant:ant-launcher:jar:1.7.1:runtime
[DEBUG] org.apache.ant:ant:jar:1.7.1:compile
[DEBUG] Created new class realm plugin>org.apache.maven.plugins:maven-antrun-plugin:1.3
[DEBUG] Importing foreign packages into class realm plugin>org.apache.maven.plugins:maven-antrun-plugin:1.3
[DEBUG] Imported: < project>runtime_shared:art.util.robot:1.0.0
[DEBUG] Populating class realm plugin>org.apache.maven.plugins:maven-antrun-plugin:1.3
[DEBUG] Included: org.apache.maven.plugins:maven-antrun-plugin:jar:1.3
[DEBUG] Included: junit:junit:jar:3.8.1
[DEBUG] Included: org.codehaus.plexus:plexus-utils:jar:1.5.6
[DEBUG] Included: org.apache.ant:ant-launcher:jar:1.7.1
[DEBUG] Included: org.apache.ant:ant:jar:1.7.1
[DEBUG] Excluded: org.apache.maven:maven-plugin-api:jar:2.0.4
[DEBUG] Excluded: org.apache.maven:maven-project:jar:2.0.4
[DEBUG] Excluded: org.apache.maven:maven-settings:jar:2.0.4
[DEBUG] Excluded: org.apache.maven:maven-profile:jar:2.0.4
[DEBUG] Excluded: org.apache.maven:maven-model:jar:2.0.4
[DEBUG] Excluded: org.apache.maven:maven-artifact-manager:jar:2.0.4
[DEBUG] Excluded: org.apache.maven:maven-repository-metadata:jar:2.0.4
[DEBUG] Excluded: org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9
[DEBUG] Excluded: classworlds:classworlds:jar:1.1-alpha-2
[DEBUG] Excluded: org.apache.maven:maven-artifact:jar:2.0.4
[DEBUG] Configuring mojo org.apache.maven.plugins:maven-antrun-plugin:1.3:run from plugin realm ClassRealm[plugin>org.apache.maven.plugins:maven-antrun-plugin:1.3, parent: sun.misc.Launcher$AppClassLoader@5c647e05]
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-antrun-plugin:1.3:run' with override configurator -->
[DEBUG] (f) pluginArtifacts = [org.apache.maven.plugins:maven-antrun-plugin:maven-plugin:1.3:, junit:junit:jar:3.8.1:compile, org.codehaus.plexus:plexus-utils:jar:1.5.6:compile, org.apache.ant:ant-launcher:jar:1.7.1:runtime, org.apache.ant:ant:jar:1.7.1:compile]
[DEBUG] (f) project = MavenProject: runtime_X:X.messages:1.0.0 @ /home/usr/workspace_runtime_X/X.messages/pom.xml
[DEBUG] (f) sourceRoot = /home/usr/workspace_runtime_X/X.messages/src-gen
[DEBUG] (f) tasks =
[DEBUG] -- end configuration --
如果对文件未创建有疑问,请确保使用绝对路径。在这种情况下,${basedir}
允许我们获取 Maven 基本目录位置(POM 的位置)的绝对路径:
<mkdir dir="${basedir}/src-gen" />
<exec executable="protoc">
<arg value="--java_out=${basedir}/src-gen" />
<arg value="--proto_path=${basedir}" /> <!-- proto_path needs to point to where the proto files are -->
<arg value="proto/ros/RosTime.proto" />
<!-- rest of proto files... -->
</exec>
然后,请注意 maven-antrun-plugin
的 <sourceRoot>
参数已弃用:
Deprecated. Use the build-helper-maven-plugin to bind source directories
按照建议,您应该使用该目标的 build-helper-maven-plugin:add-source
goal to add a source folder to your Maven project. Configure the folders to add with the sources
参数。
话虽如此,您可能更有机会使用 org.xolstice.maven.plugins:protobuf-maven-plugin
而不是依赖 Ant 任务:
Maven Protocol Buffers Plugin uses Protocol Buffer Compiler (
protoc
) tool to generate Java source files from.proto
(protocol buffer definition) files for the specified project.
compile
goal can be used to generate the Java source files. Placing your .proto
files under src/main/proto
(configurable with the protoSourceRoot
parameter), the plugin will generate the Java source files inside ${project.build.directory}/generated-sources/protobuf/java
(configurable with the outputDirectory
参数)。
如果 protoc
可执行文件不在 PATH 环境变量中,您可以使用 protocExecutable
参数设置它。