在 java 协议缓冲区中导入“google/protobuf/descriptor.proto”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20069295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:51:20  来源:igfitidea点击:

Importing "google/protobuf/descriptor.proto" in java protocol buffers

javaprotocol-buffers

提问by Jan Zyka

I have a .protofile definition which needs to import "google/protobuf/descriptor.proto"because I use Custom Options.

我有一个.proto需要导入的文件定义,"google/protobuf/descriptor.proto"因为我使用了Custom Options

So in my .protofile I do:

所以在我的.proto文件中,我这样做:

import "google/protobuf/descriptor.proto";
package ...;

...

Since my file didn't compile complaining about the dependency, I got a copy of the descriptor.proto file placing it in the same directory my proto file was.

由于我的文件没有编译抱怨依赖项,我得到了一个 descriptor.proto 文件的副本,将它放在我的 proto 文件所在的同一目录中。

This solved the problem but I don't believe this is the correct way. Now the descriptor.protogets compiled together with my .protofile resulting in having 2 compiled descriptor.protoat runtime:

这解决了问题,但我不相信这是正确的方法。现在descriptor.proto与我的.proto文件一起编译descriptor.proto,导致在运行时编译了2 个:

  • the one shipped with the protobuf-java-2.5.0.jarfile
  • the one which was compiled together with my .protofile
  • protobuf-java-2.5.0.jar文件附带的那个
  • 与我的.proto文件一起编译的那个

I think the --proto-pathoption should be used somehow but not entirely sure what is the correct way.

我认为--proto-path应该以某种方式使用该选项,但不完全确定正确的方法是什么。

Thanks for the best practise tip here!

感谢这里的最佳实践提示!

采纳答案by Bruce Martin

When I have used descriptor in a .proto, I have used it like

当我在 .proto 中使用描述符时,我使用它就像

import "google/protobuf/descriptor.proto";

message AddressBook {
  required google.protobuf.FileDescriptorSet proto_files = 1;

Then to generate the java (on windows) with addressbookSD.proto in the default directory:

然后在默认目录中使用 addressbookSD.proto 生成 java(在 Windows 上):

protoc addressbookSD.proto --java_out=./ --proto_path=./ --proto_path=<protobuf-install-directory>\src

where <protobuf-install-directory> is the protocol buffers install directory. The key point is descriptor.proto is in

其中 < protobuf-install-directory> 是协议缓冲区安装目录。关键是descriptor.proto在

<protobuf-install-directory>\src\google\protobuf

The levels in an protobuf import stament must match directories in the File system just like they would in java.

protobuf 导入语句中的级别必须与文件系统中的目录匹配,就像它们在 java 中一样。

So I use <protobuf-install-directory>\srcas the import directory, The directory structure must be

所以我使用< protobuf-install-directory>\src作为导入目录,目录结构必须是

<protobuf-install-directory>\src
    +-- google
         +-- protobuf
             +-- descriptor.proto

回答by JonasVautherin

Just for the record: I had the same problem recently, and in my case my project depends on protobuf-lite. Apparently, protobuf needed the 'full' protobuf for the custom extensions, and therefore I had to add the dependency, as shown here:

仅供记录:我最近遇到了同样的问题,就我而言,我的项目依赖于protobuf-lite. 显然,protobuf 需要用于自定义扩展的“完整”protobuf,因此我必须添加依赖项,如下所示:

dependencies {
    protobuf "io.grpc:grpc-protobuf:${grpcVersion}"

    ...
}

Note that I depend on gRPC in my project, but that works with com.google.protobuf, too.

请注意,我在我的项目中依赖于 gRPC,但这也适用于com.google.protobuf