安装 protobuf
brew install autoconf automake libtool
git clone https://github.com/google/protobuf.git
cd protobuf
git checkout v3.5.0
sh ./autogen.sh
./configure --prefix=/usr/local/protobuf/
sudo make && make install
编译插件
wget https://github.com/grpc/grpc-java/archive/v1.7.1.tar.gz
tar zxvf grpc-java-1.7.1.tar.gz
cd grpc-java/compiler
export CXXFLAGS="-I/usr/local/protobuf/include" LDFLAGS="-L/usr/local/protobuf/lib"
../gradlew java_pluginExecutable
protoc-gen-grpc-java就在build/exe/java_plugin下
报错:
执行 ../gradlew java_pluginExecutable出现如下错误
Starting a Gradle Daemon (subsequent builds will be faster)
*** Android SDK is required. To avoid building Android projects, set -PskipAndroid=true
FAILURE: Build failed with an exception.
解决方案:添加-PskipAndroid=true参数,即命令如下
../gradlew java_pluginExecutable -PskipAndroid=true
使用方法
protoc --plugin=protoc-gen-grpc-java=build/exe/java_plugin/protoc-gen-grpc-java \
--grpc-java_out="$OUTPUT_FILE" --proto_path="$DIR_OF_PROTO_FILE" "$PROTO_FILE"
比如
protoc --java_out=./src/main/java --plugin=protoc-gen-grpc-java=/Users/diyai/github/grpc-java-1.7.1/compiler/build/exe/java_plugin/protoc-gen-grpc-java --grpc-java_out=./src/main/java ./src/main/java/com/ctu/crackcaptcha/classification_service.proto
- 生成的文件