简体   繁体   English

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

[英]How to compile multiple proto files in single command?

I have two proto files inside single directory and I am looking for a way to generate classes from those files in a single command.我在单个目录中有两个 proto 文件,我正在寻找一种在单个命令中从这些文件生成类的方法。 It seems this can be accomplished with the --proto_path argument.这似乎可以通过--proto_path参数来完成。 As per the documentation:根据文档:

You must provide one or more .proto files as input.您必须提供一个或多个 .proto 文件作为输入。 Multiple .proto files can be specified at once.可以一次指定多个 .proto 文件。 Although the files are named relative to the current directory, each file must reside in one of the IMPORT_PATHs [specified by the --proto_path argument] so that the compiler can determine its canonical name.尽管文件是相对于当前目录命名的,但每个文件都必须驻留在 IMPORT_PATHs [由 --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

I used --proto_path argument as shown below我使用了--proto_path参数,如下所示

C:\shekhar\proto_trial>protoc 
                       --proto_path=C:\shekhar\proto_trial 
                       --java_out=C:\shekhar\proto_trial\java_op 
                       *.proto

but I am getting following error但我收到以下错误

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).

Please suggest some way to compile all the proto files together in single shot.请建议一些方法来一次性编译所有 proto 文件。

The problem is that you are specifying --proto_path as an absolute path but your proto files as relative paths.问题是您将--proto_path指定为绝对路径,而将 proto 文件指定为相对路径。 You can either drop the --proto_path argument (it defaults to the current directory anyway), or you can do:您可以删除--proto_path参数(它默认为当前目录),或者您可以执行以下操作:

protoc --proto_path=C:\shekhar\proto_trial
       --java_out=C:\shekhar\proto_trial\java_op
       C:\shekhar\proto_trial\*.proto

Here's an option using find这是一个使用 find 的选项

protoc --js_out=js \
        -Iproto/ \
        $(find proto/google -iname "*.proto")

Commands for Protobuf >= 3.5 Protobuf >= 3.5 的命令

It seems to me that the normal command on Windows only worked for Protobuf <= 3.4, and in the newer versions you cannot use the wildcard * but you have to put all the filenames separately.在我看来,Windows 上的普通命令仅适用于 Protobuf <= 3.4,而在较新的版本中,您不能使用通配符 *,但您必须单独放置所有文件名。 Fortunately it's still easy using a for loop (from here ), using relative directories:幸运的是,使用 for 循环(来自此处)仍然很容易,使用相对目录:

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

Alternatively, from here , you could also try to use Git Bash if you have it installed as it could expand the wildcard properly, and then use the command as you have before:或者,从这里开始,如果您安装了 Git Bash,您也可以尝试使用它,因为它可以正确扩展通配符,然后像以前一样使用命令:

protoc proto_trial\*.proto --java_out=proto_trial\java_op

Download the windows version of the compiler from this link https://github.com/google/protobuf/releases/tag/v3.3.0从此链接下载 Windows 版本的编译器https://github.com/google/protobuf/releases/tag/v3.3.0

Start command prompt from bin folder of the downloaded application.从下载的应用程序的 bin 文件夹启动命令提示符。 Use the potoc command to generate the classes.使用potoc 命令生成类。

    protoc --proto_path=<Directory name where your proto file is residing> 
           --java_out=<Directory name where you want your output classes to get generated> 
           <absolute path of your protofile with extention>

Use wild character * for multiple protofiles对多个原型文件使用通配符 *

最简单的方法:

protoc C:\shekhar\proto_trial\*.proto --java_out=C:\shekhar\proto_trial\java_op

find .找 。 -name "*.proto" | -name "*.proto" | xargs -I {} protoc "{}" xargs -I {} 协议“{}”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM