导入"google/protobuf/descriptor.proto"在Java协议缓冲区中 [英] Importing "google/protobuf/descriptor.proto" in java protocol buffers

查看:257
本文介绍了导入"google/protobuf/descriptor.proto"在Java协议缓冲区中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

因此,在我的 .proto 文件中,我这样做了:

So in my .proto file I do:

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

...

由于我的文件没有编译有关该依赖项的投诉,因此我得到了描述符.proto文件的副本,将其放置在与我的proto文件相同的目录中.

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 文件一起编译,导致在运行时有2个编译后的 descriptor.proto :

This solved the problem but I don't believe this is the correct way. Now the descriptor.proto gets compiled together with my .proto file resulting in having 2 compiled descriptor.proto at runtime:

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

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

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

感谢此处的最佳实践提示!

Thanks for the best practise tip here!

推荐答案

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

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

import "google/protobuf/descriptor.proto";

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

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

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

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

其中< protobuf-install-directory >是协议缓冲区的安装目录.关键是描述符.proto位于

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

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

protobuf导入策略中的级别必须与文件系统中的目录匹配,就像在Java中一样.

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

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

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

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

这篇关于导入"google/protobuf/descriptor.proto"在Java协议缓冲区中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆