编译grpc-java项目生成protoc-gen-grpc-java文件
使用protoc命令生成service代码的时候,需要使用如下命令
protoc --plugin=protoc-gen-grpc-java=./protoc-gen-grpc-java -I=./ --java_out=./src/main/java/ --grpc-java_out=./src/main/java/ ./src/main/proto/helloworld.proto
其中的protoc-gen-grpc-java执行文件需要自己编译生成
编译的步骤如下
git clone grpc-java项目,
git clone git@github.com:grpc/grpc-java.git
切换到使用的grpc版本,比如v1.59.0,编译这个版本需要jdk11的支持
git checkout v1.59.0
使用gradlew命令进行编译
➜ /Users/lintong/coding/java/grpc-java/compiler git:(ae49d275b) $ ../gradlew java_pluginExecutable -PskipAndroid=true * Skipping the build of Android projects because skipAndroid=true > Configure project :grpc-compiler *** Building codegen requires Protobuf *** Please refer to https://github.com/grpc/grpc-java/blob/master/COMPILING.md#how-to-build-code-generation-plugin Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0. You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins. For more on this, please refer to https://docs.gradle.org/8.3/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation. BUILD SUCCESSFUL in 2s 2 actionable tasks: 1 executed, 1 up-to-date
在build目录下编译得到protoc-gen-grpc-java执行文件
➜ /Users/lintong/coding/java/grpc-java/compiler git:(ae49d275b) $ cd build/exe/java_plugin ➜ /Users/lintong/coding/java/grpc-java/compiler/build/exe/java_plugin git:(ae49d275b) $ ls protoc-gen-grpc-java
这时候就可以使用protoc命令到编译proto文件得到service代码,否则只能得到model代码
官方文档:
https://github.com/grpc/grpc-java/tree/master/compiler
本文只发表于博客园和tonglin0325的博客,作者:tonglin0325,转载请注明原文链接:https://www.cnblogs.com/tonglin0325/p/5572371.html