生成 java 类时无法使用 maven protobuf-maven-plugin 插件解析 google proto



包含导入语句的文件。

import "goproto/common/date.proto";
import "goproto/common/timestamp.proto";
import "goproto/common/custom_options.proto";
option java_multiple_files = true;
option java_package = "com.kafka.proto";
message KafkaInfoLogProto {
int32 pid = 1;
com.types.Timestamp event_time = 2;
string tid = 3;
string device_id = 4;
string email = 5;
string city_id = 6;
com.types.Date check_in = 7;
com.types.Date check_out = 8;
}

导入原型文件存在于另一个名为 common 的目录中,例如让我描述导入的原型文件之一。

syntax = "proto3";
package com.types;
option java_multiple_files = true;
option java_outer_classname = "DateProto";
option java_package = "com.types";
message Date {
int32 year = 1;
int32 month = 2;
int32 day = 3;
}

我的pom文件看起来像:

<dependencies>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
</dependency>
</dependencies>

使用的插件是:

<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.6.1:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.19.0:exe:${os.detected.classifier}</pluginArtifact>
<attachProtoSources>true</attachProtoSources>
<includeDependenciesInDescriptorSet>true</includeDependenciesInDescriptorSet>
<protoSourceRoot>../goproto/</protoSourceRoot>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>

我得到的错误。

[ERROR] /goproto/aug/kafka_info_log.proto [0:0]: goproto/common/date.proto: File not found.
goproto/common/timestamp.proto: File not found.
kafka_info_log.proto: Import "goproto/common/date.proto" was not found or had errors.
kafka_info_log.proto: Import "goproto/common/timestamp.proto" was not found or had errors.
kafka_info_log.proto:16:5: "com.types.Timestamp" is not defined.
kafka_info_log.proto:21:5: "com.types.Date" is not defined.
kafka_info_log.proto:22:5: "com.types.Date" is not defined.

我已经在网上搜索了这个,但找不到任何解决方案。请帮忙。

您可能在以下至少两项之间不匹配:

  • import语句中指定的 protobuf 包
  • 导入的 protobuf 文件中package语句
  • 导入文件的目录结构

让我们看一下您的Date消息:它是在包com.types中声明的。

因此,要使一切正常工作,例如,您需要执行以下操作:

  1. date.proto移动到目录goproto/common/com/types
  2. KafkaInfoLogProto中将导入声明为:import "com/types/date.proto";
  3. pom.xml中的设置更改为:<protoSourceRoot>../goproto/common</protoSourceRoot>

或者:

  1. date.proto移动到目录goproto/common/com/types
  2. KafkaInfoLogProto中将导入声明为:import "common/com/types/date.proto";
  3. 将 POM 保留原样

这就是 protobuf 编译器解析导入的方式。如果您使用的是软件包,则 protobuf 源根目录下的相对路径应与声明的package匹配。

最新更新