如何在单个命令中编译多个proto文件?

She*_*har 11 java protocol-buffers

我在单个目录中有两个proto文件,我正在寻找一种方法,可以在单个命令中从这些文件生成类.Protobuf文档说我们需要使用--proto_path参数.

C:\shekhar\proto_trial>dir
 Volume in drive C is C

 Directory of C:\shekhar\proto_trial

07/25/2014  12:16 PM    <DIR>          .
07/25/2014  12:16 PM    <DIR>          ..
07/25/2014  12:16 PM    <DIR>          java_op
07/25/2014  12:16 PM               230 map.proto
07/23/2014  04:24 PM               161 message.proto
07/25/2014  12:17 PM             1,228 response.proto
               3 File(s)          1,619 bytes
               3 Dir(s)  50,259,398,656 bytes free
Run Code Online (Sandbox Code Playgroud)

我使用了--proto_path如下所示的参数

C:\shekhar\proto_trial>protoc 
                       --proto_path=C:\shekhar\proto_trial 
                       --java_out=C:\shekhar\proto_trial\java_op 
                       *.proto
Run Code Online (Sandbox Code Playgroud)

但我得到以下错误

message.proto: File does not reside within any path specified using --proto_path (or -I). 
You must specify a --proto_path which encompasses this file. 
Note that the proto_path must be an exact prefix of the .proto file names -- protoc is too dumb to figure out when two paths (e.g. absolute and relative) are equivalent (it's harder than you think).
Run Code Online (Sandbox Code Playgroud)

请建议一些单独编译所有原型文件的方法.

Ken*_*rda 17

问题是您指定的--proto_path是绝对路径,但您的proto文件是相对路径.您可以删除--proto_path参数(无论如何都默认为当前目录),或者您可以执行以下操作:

protoc --proto_path=C:\shekhar\proto_trial
       --java_out=C:\shekhar\proto_trial\java_op
       C:\shekhar\proto_trial\*.proto
Run Code Online (Sandbox Code Playgroud)

  • 它不起作用: /main/resources/proto/*.proto: Invalid argument 我发现的唯一解决方案是在同一行上一个一个地添加每个文件。 (2认同)

Cos*_*ene 17

这是一个使用 find 的选项

protoc --js_out=js \
        -Iproto/ \
        $(find proto/google -iname "*.proto")
Run Code Online (Sandbox Code Playgroud)

  • 这应该是正确的答案,因为如果有 .proto 文件的子目录,所有相关的 .proto 文件都需要使用相同的命令运行才能使目录正确。 (3认同)

PHP*_*ate 5

Protobuf >= 3.5 的命令

在我看来,Windows 上的普通命令仅适用于 Protobuf <= 3.4,而在较新的版本中,您不能使用通配符 *,但您必须将所有文件名分开放置。幸运的是,使用 for 循环(来自此处)仍然很容易,使用相对目录:

for /f %i in ('dir /b proto_trial\*.proto') do protoc proto_trial\%i --java_out=proto_trial\java_op
Run Code Online (Sandbox Code Playgroud)

或者,从这里开始,如果您安装了 Git Bash,您也可以尝试使用它,因为它可以正确扩展通配符,然后像以前一样使用命令:

protoc proto_trial\*.proto --java_out=proto_trial\java_op
Run Code Online (Sandbox Code Playgroud)