编译 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

protoc的protoc-gen-grpc-java插件的更多相关文章

  1. 解决Ubuntu下Firefox+OpenJDK没有Java插件的问题

    如果是安装的OpenJDK,很遗憾它是没有libnpjp2.so的. 此时按照网上各种奇怪的方法都挣扎无效,但可以用icedtea插件来解决这个问题. icedtea的版本与本机安装的OpenJDK版 ...

  2. 关于Chrome浏览器不能使用Java插件的问题

    最近测试的“上海电信宽带测速系统”中HTTP测试需要用到java插件,之前装过好多次插件,装好后还是提示java插件未安装,郁闷了N久,最近问题终于得到了解决,故做分享~ 关于Chrome浏览器不能使 ...

  3. 几款常用Eclipse java插件

    以下是我最近常用的几款Eclipse java插件: ADT Plugin https://dl-ssl.google.com/android/eclipse/ WindowBuilder Pro  ...

  4. Linux下启用Chrome/Firefox的Java插件

    JDK 已经安装好,可是浏览器执行 Java Applet 时提示需安装 Java 插件. 这时,在浏览器安装文件夹中 plugins 文件夹下创建2个重要的符号链接就可以. libnpjp2.so ...

  5. Gradle 1.12用户指南翻译——第二十三章. Java 插件

    其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Github上的地址: https://g ...

  6. 【解决Jira】Chrome提示Java插件因过期而遭到阻止(JIRA上传截屏截图)

    最近经常被这个问题所困扰:用Chrome访问JIRA上传截屏截图时,地址栏下面弹出通知,提示JAVA插件已过期.但是由于公司要求统一开发环境和设置,不能更新到最新版,就像这样: 结果网页上的Java就 ...

  7. Gradle Java 插件

    Java 插件是构建 JVM 项目的基础,它为项目增加了很多能力,例如编译,测试,打包,发布等等. 很多插件都是基于 Java 插件实现的,例如 Android 插件. 用法 使用 id 应用插件 p ...

  8. VS Code Java 更新 – 全新Gradle for Java插件,更方便的代码操作, 1.0 语言支持发布

    大家好,欢迎来到 9 月版的 Visual Studio Code Java 更新.在这篇文章中,我们将分享我们最新的Gradle插件,更加方便的代码操作(Getter/Setter等等),以及最近的 ...

  9. VS Code Java 3月更新|代码补全、Maven 以及 Java 插件预览版本新升级!

    Nick Zhu Senior Program Manager, Developer Division at Microsoft 大家好,欢迎来到 Visual Studio Code Java 的 ...

  10. gRPC Java的代码架构

    RPC(远程过程调用) 的架构最常见的是"动态代理"方式,事先定义好接口,用一个代理假装实现了这个接口(真正的实现放在服务端),供客户端调用,代理内部将该方法调用封装成一个网络请求 ...

随机推荐

  1. 可选的Web Components类库

    首先需要说明的是这不是一篇 Web Components 的科普文章,如果对此了解不多推荐先读<A Guide to Web Components>. 有句古话-“授人以鱼,不如授人以渔” ...

  2. 【转】使用itms-services从浏览器发布iOS App遇到的问题总结

    itms-service是apple为iOS企业用户($299)提供的无线分发安装方式所使用的协议,使用这种方式发布应用不需要通过App Store,任何iOS设备都可以安装企业用户通过这种方式发布的 ...

  3. 数据接口管理工具 thx RAP

    RAP是数据接口管理工具.在开发时前端将请求转至RAP,由RAP提供模拟数据:而后端使用RAP测试接口的正确性.这样RAP就成为了开发过程中的强 依赖,进而确保接口文档的实时正确性.RAP采用JSON ...

  4. matlab语言基础

    (一)常用命令 清空命令窗口:clc 清空工作空间:clear 打开editor:edit 查看函数的帮助信息:help 函数名  让命令窗口信息紧凑显示:format compact (二)变量与常 ...

  5. 【python】python异常类型

    python2: BaseException +-- SystemExit +-- KeyboardInterrupt +-- GeneratorExit +-- Exception +-- Stop ...

  6. 域控制器中的FSMO角色

    FSMO是Flexible single master operation的缩写,意思就是灵活单主机操作.营运主机(Operation Masters,又称为Flexible Single Maste ...

  7. jQuery-webcam(.NET)实现WEB摄像头监控

    jQuery-webcam是一个非常好用的摄像头监控工具,DEMO可官方下载地址http://www.xarg.org/project/jquery-webcam-plugin/ 1.下载解压后,jq ...

  8. [系统开发] Postfix 邮件管理系统

    一.简介 开发时间:2012年 开发工具:Perl CGI 这是我开发的 Postfix 邮件管理系统,通过它可以安全.方便的对邮件域名.用户.权限.组.邮箱容量.安全等进行各种设置:界面样式借鉴了 ...

  9. PL/SQL中查看列注释、列属性

  10. Maven错误在这里看【项目无法成功编译由于maven未成功下载依赖导致】