编译 protoc-gen-grpc-java插件 的文档在:
https://github.com/grpc/grpc-java/tree/master/compiler
编译的步骤:
Change to the compiler
directory:
$ cd $GRPC_JAVA_ROOT/compiler
To compile the plugin:
$ ../gradlew java_pluginExecutable
To test the plugin with the compiler:
$ ../gradlew java_pluginExecutable
编译过程中如果出现下面错误: fatal error: 'google/protobuf/io/zero_copy_stream.h' file not found。
是因为找不到这些头文件。找不到的原因如下:
Some versions of Mac OS X (e.g., 10.10) doesn't have /usr/local
in the default search paths for header files and libraries.
It will fail the build of the codegen. To work around this, you will need to set environment variables:
$ export CXXFLAGS="-I/usr/local/include" LDFLAGS="-L/usr/local/lib"
参考: https://github.com/grpc/grpc-java/blob/master/COMPILING.md
错误的时候提示信息:
$ ../gradlew java_pluginExecutable
Download https://repo1.maven.org/maven2/org/mortbay/jetty/alpn/jetty-alpn-agent/2.0.2/jetty-alpn-agent-2.0.2.pom
Download https://repo1.maven.org/maven2/org/eclipse/jetty/jetty-parent/25/jetty-parent-25.pom
Download https://repo1.maven.org/maven2/org/mortbay/jetty/alpn/jetty-alpn-agent/2.0.2/jetty-alpn-agent-2.0.2.jar
*** Building codegen requires Protobuf version 3.0.0-beta-2
*** Please refer to https://github.com/grpc/grpc-java/blob/master/COMPILING.md#how-to-build-code-generation-plugin
:grpc-compiler:compileJava_pluginExecutableJava_pluginCpp
/Users/ghj1976/project/github/grpc/grpc-java/compiler/src/java_plugin/cpp/java_generator.h:8:10: fatal error: 'google/protobuf/io/zero_copy_stream.h' file not found
#include <google/protobuf/io/zero_copy_stream.h>
^
1 error generated.
In file included from /Users/ghj1976/project/github/grpc/grpc-java/compiler/src/java_plugin/cpp/java_plugin.cpp:8:
/Users/ghj1976/project/github/grpc/grpc-java/compiler/src/java_plugin/cpp/java_generator.h:8:10: fatal error: 'google/protobuf/io/zero_copy_stream.h' file not found
#include <google/protobuf/io/zero_copy_stream.h>
^
1 error generated.
In file included from /Users/ghj1976/project/github/grpc/grpc-java/compiler/src/java_plugin/cpp/java_generator.cpp:1:
/Users/ghj1976/project/github/grpc/grpc-java/compiler/src/java_plugin/cpp/java_generator.h:8:10: fatal error: 'google/protobuf/io/zero_copy_stream.h' file not found
#include <google/protobuf/io/zero_copy_stream.h>
^
1 error generated.
:grpc-compiler:compileJava_pluginExecutableJava_pluginCpp FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':grpc-compiler:compileJava_pluginExecutableJava_pluginCpp'.
> Multiple build operations failed.
C++ compiler failed while compiling java_generator.h.
C++ compiler failed while compiling java_plugin.cpp.
C++ compiler failed while compiling java_generator.cpp.
See the complete log at: file:///Users/ghj1976/project/github/grpc/grpc-java/compiler/build/tmp/compileJava_pluginExecutableJava_pluginCpp/output.txt
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 1 mins 24.831 secs
正确的时候的提示信息:
$ ../gradlew java_pluginExecutable
*** Building codegen requires Protobuf version 3.0.0-beta-2
*** Please refer to https://github.com/grpc/grpc-java/blob/master/COMPILING.md#how-to-build-code-generation-plugin
:grpc-compiler:compileJava_pluginExecutableJava_pluginCpp
:grpc-compiler:linkJava_pluginExecutable
:grpc-compiler:java_pluginExecutable
BUILD SUCCESSFUL
Total time: 14.818 secs
This build could be faster, please consider using the Gradle Daemon: https://docs.gradle.org/2.11/userguide/gradle_daemon.html
localhost:compiler ghj1976$
$ ../gradlew java_pluginExecutable
Downloading https://services.gradle.org/distributions/gradle-2.13-bin.zip
.....................................................................................................................................................................................
Unzipping /Users/ghj1976/.gradle/wrapper/dists/gradle-2.13-bin/4xsgxlfjcxvrea7akf941nvc7/gradle-2.13-bin.zip to /Users/ghj1976/.gradle/wrapper/dists/gradle-2.13-bin/4xsgxlfjcxvrea7akf941nvc7
Set executable permissions for: /Users/ghj1976/.gradle/wrapper/dists/gradle-2.13-bin/4xsgxlfjcxvrea7akf941nvc7/gradle-2.13/bin/gradle
Download https://repo1.maven.org/maven2/com/google/protobuf/protobuf-gradle-plugin/0.7.7/protobuf-gradle-plugin-0.7.7.pom
Download https://repo1.maven.org/maven2/com/google/protobuf/protobuf-gradle-plugin/0.7.7/protobuf-gradle-plugin-0.7.7.jar
*** Building codegen requires Protobuf version 3.0.0-beta-2
*** Please refer to https://github.com/grpc/grpc-java/blob/master/COMPILING.md#how-to-build-code-generation-plugin
:grpc-compiler:compileJava_pluginExecutableJava_pluginCpp
:grpc-compiler:linkJava_pluginExecutable
:grpc-compiler:java_pluginExecutable
BUILD SUCCESSFUL
Total time: 4 mins 29.65 secs
This build could be faster, please consider using the Gradle Daemon: https://docs.gradle.org/2.13/userguide/gradle_daemon.html
localhost:compiler ghj1976$
我这里编译后的文件在下面目录的 protoc-gen-grpc-java 文件:
/Users/ghj1976/project/github/grpc/grpc-java/compiler/build/exe/java_plugin
对于需要产生Java版本的gRPC自动编译代码的,需要做下面几步:
1、下载 protoc,对于原来就在用 protoc 的人来说,需要注意替换成 3.0
的版本。下载、使用方法参考: http://www.cnblogs.com/ghj1976/p/5435565.html
2、同时去官网下载protoc-gen-grpc-java
的源码自行在本地编译(参考本文上面部分)。
3、然后使用 protoc 命令时指定 --plugin
编译,不要试图使用 IDE 的protobuf 插件。
http://evthoriz.com/2015/07/07/gRPC-tutorial/
具体产生 java 版gRPC代码的命令如下:
$ protoc --plugin=protoc-gen-grpc-java=build/binaries/java_pluginExecutable/protoc-gen-grpc-java
--grpc-java_out="$OUTPUT_FILE" --proto_path="$DIR_OF_PROTO_FILE" "$PROTO_FILE"
具体到我这里的代码就是:
$ protoc --plugin=protoc-gen-grpc-java=/Users/ghj1976/project/github/grpc/grpc-java/compiler/build/exe/java_plugin/protoc-gen-grpc-java --grpc-java_out=./java/ ./proto/helloworld.proto
产生 android 使用的 java-nano 代码方法如下:
$ protoc --plugin=protoc-gen-grpc-java=build/binaries/java_pluginExecutable/protoc-gen-grpc-java
--grpc-java_out=nano:"$OUTPUT_FILE" --proto_path="$DIR_OF_PROTO_FILE" "$PROTO_FILE"
具体到我这里的代码就是:
$ protoc --plugin=protoc-gen-grpc-java=/Users/ghj1976/project/github/grpc/grpc-java/compiler/build/exe/java_plugin/protoc-gen-grpc-java --grpc-java_out=nano:./javanano/ ./proto/helloworld.proto
参考:
protoc not generating service stub files
http://stackoverflow.com/questions/31029675/protoc-not-generating-service-stub-files