How to compile multiple proto files in single command?

余生颓废 提交于 2019-12-18 03:25:09

问题


I have two proto files inside single directory and I am looking for a way to generate classes from those files in single command. Protobuf documentation says that we need to use --proto_path argument for this.

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

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.


回答1:


The problem is that you are specifying --proto_path as an absolute path but your proto files as relative paths. You can either drop the --proto_path argument (it defaults to the current directory anyway), or you can do:

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



回答2:


Commands for 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. Fortunately it's still easy using a for loop (from here), using relative directories:

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:

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



回答3:


Here's an option using find

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



回答4:


Download the windows version of the compiler from this link https://github.com/google/protobuf/releases/tag/v3.3.0

Start command prompt from bin folder of the downloaded application. Use the potoc command to generate the classes.

    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




回答5:


Simplest way:

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



回答6:


find . -name "*.proto" | xargs -I {} protoc "{}"



来源:https://stackoverflow.com/questions/24949801/how-to-compile-multiple-proto-files-in-single-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!