编译 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. html之br标签

    <br>:自闭合 一般用法: 标签会告诉浏览器立即停止当前的文本流,并在下一行的左边继续输出,也通常用来在相临的段落之间制造一段垂直的间距. 代码: 效果: 问题:当段落中有表格或图片时怎 ...

  2. spring data redis RedisTemplate操作redis相关用法

    http://blog.mkfree.com/posts/515835d1975a30cc561dc35d spring-data-redis API:http://docs.spring.io/sp ...

  3. C#基础——Func和Action的介绍及其用法

    Func是一种委托,这是在3.5里面新增的,2.0里面我们使用委托是用Delegate,Func位于System.Core命名空间下,使用委托可以提升效率,例如在反射中使用就可以弥补反射所损失的性能. ...

  4. EditText使用详解-包含很多教程上看不到的功能演示

    写道 标题有点大,说是详解,其实就是对EditText的一些常用功能的介绍,包括密码框,电话框,空白提示文字等等的讲解,尽量的介绍详细一点,也就是所谓的详解了..呵呵 广告一下我的应用“我团”,最新1 ...

  5. (转载)CentOS6下 源代码方式安装openERP7.0

    CentOS6下 源代码方式安装openERP7.0 安装背景 :CPU32 bit,CentOS 6.4版本,openERP7.0,linux shell为bash,PostgreSQL9.2 1. ...

  6. 【linux】man和--help

    man和--help都是获取命令帮助的指令. man 格式: man [命令] [root@andon tmp]# man pwd --help 命令 --help [root@andon tmp]# ...

  7. 【python】sys.argv[]的用法

    在学python的过程中,一直弄不明白sys.argv[]的意思,虽知道是表示命令行参数,但还是有些稀里糊涂的感觉. 今天又好好学习了一把,总算是大彻大悟了. Sys.argv[]是用来获取命令行参数 ...

  8. windows证书地址

    C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA\MachineKeys 用certutil -sto ...

  9. android 模拟抢红包 原理

    Android微信抢红包外挂 源代码 标签: 微信 抢红包 外挂 插件 2015-02-20 22:59 30211人阅读 评论(16) 收藏 举报  分类: Android(58)  版权声明:本文 ...

  10. 36. Valid Sudoku

    ============= Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku b ...