tonglin0325的个人主页

编译grpc-java项目生成protoc-gen-grpc-java文件

使用protoc命令生成service代码的时候,需要使用如下命令

1
2
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项目,

1
2
git clone git@github.com:grpc/grpc-java.git

切换到使用的grpc版本,比如v1.59.0,编译这个版本需要jdk11的支持

1
2
git checkout v1.59.0

使用gradlew命令进行编译

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
➜  /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执行文件

1
2
3
4
➜  /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代码

官方文档:

1
2
https://github.com/grpc/grpc-java/tree/master/compiler

其他文档:java/go grpc 生成 service