Java 如何在单个命令中编译多个 proto 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24949801/
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
How to compile multiple proto files in single command?
提问by Shekhar
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.
我在单个目录中有两个 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
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 文件。
采纳答案by Kenton Varda
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:
问题是您指定--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
回答by Sandeep
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. Use the potoc command to generate the classes.
从下载的应用程序的 bin 文件夹启动命令提示符。使用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
对多个原型文件使用通配符 *
回答by Ashok Kumar Pant
Simplest way:
最简单的方法:
protoc C:\shekhar\proto_trial\*.proto --java_out=C:\shekhar\proto_trial\java_op
回答by PHPirate
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. Fortunately it's still easy using a for loop (from here), using relative directories:
在我看来,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
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
回答by Cosmin Lehene
Here's an option using find
这是一个使用 find 的选项
protoc --js_out=js \
-Iproto/ \
$(find proto/google -iname "*.proto")
回答by hetal
find . -name "*.proto" | xargs -I {} protoc "{}"
找 。-name "*.proto" | xargs -I {} 协议“{}”