1.说明

本文介绍Protobuf生成Java代码的方法,
配置对应的Maven插件,
把.proto文件生成Java代码。

2.插件配置

创建Maven工程grpc-compile,
修改pom.xml,
引入生成代码需要的jar包依赖,
以及构建配置:

<dependencies><dependency><groupId>io.grpc</groupId><artifactId>grpc-protobuf</artifactId><version>1.30.2</version></dependency><dependency><groupId>io.grpc</groupId><artifactId>grpc-stub</artifactId><version>1.30.2</version></dependency>
</dependencies>
<build><extensions><extension><groupId>kr.motd.maven</groupId><artifactId>os-maven-plugin</artifactId><version>1.6.2</version></extension></extensions><plugins><plugin><groupId>org.xolstice.maven.plugins</groupId><artifactId>protobuf-maven-plugin</artifactId><version>0.6.1</version><configuration><protocArtifact>com.google.protobuf:protoc:3.12.0:exe:${os.detected.classifier}</protocArtifact><pluginId>grpc-java</pluginId><pluginArtifact>io.grpc:protoc-gen-grpc-java:1.32.1:exe:${os.detected.classifier}</pluginArtifact><protoSourceRoot>src/main/proto</protoSourceRoot><outputDirectory>src/main/java</outputDirectory><clearOutputDirectory>false</clearOutputDirectory></configuration><executions><execution><goals><goal>compile</goal><goal>compile-custom</goal></goals></execution></executions></plugin></plugins>
</build>

3.创建.proto文件

新建src/main/proto目录,
在里面创建helloworld.proto文件:

// 显示声明使用proto3, 否则使用默认的proto2
syntax = "proto3";// 生成类的包名
option java_package = "com.asiainfo.yuwen.grpc.helloworld";
// 生成类的文件名,否则默认生成的类名为proto文件名的驼峰命名
option java_outer_classname = "HelloWorldProto";
// 定义的所有消息、枚举和服务生成对应的多个类文件,而不是以内部类的形式出现
option java_multiple_files = false;// greeting服务定义
service Greeter {// sayHello方法,格式为"方法名 请求参数 返回参数"rpc SayHello (HelloRequest) returns (HelloReply) {}// 另一个sayHello方法rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}// 方法请求,包含用户名
message HelloRequest {string name = 1;
}// 方法响应,包含响应的消息
message HelloReply {string message = 1;
}

4.生成Java代码

在grpc-compile下执行Maven编译命令:

mvn clean compile

编译成功日志:

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Detecting the operating system and CPU architecture
[INFO] ------------------------------------------------------------------------
[INFO] os.detected.name: windows
[INFO] os.detected.arch: x86_64
[INFO] os.detected.version: 10.0
[INFO] os.detected.version.major: 10
[INFO] os.detected.version.minor: 0
[INFO] os.detected.classifier: windows-x86_64
[INFO]
[INFO] ------------------< com.asiainfo.yuwen:grpc-compile >-------------------
[INFO] Building grpc-compile 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ grpc-compile ---
[INFO] Deleting D:\Code\Work\Telemetry\grpc-demo\grpc-compile\target
[INFO]
[INFO] --- protobuf-maven-plugin:0.6.1:compile (default) @ grpc-compile ---
[INFO] Compiling 1 proto file(s) to D:\Code\Work\Telemetry\grpc-demo\grpc-compile\src\main\java
[INFO]
[INFO] --- protobuf-maven-plugin:0.6.1:compile-custom (default) @ grpc-compile ---
[INFO] Compiling 1 proto file(s) to D:\Code\Work\Telemetry\grpc-demo\grpc-compile\src\main\java
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ grpc-compile ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Code\Work\Telemetry\grpc-demo\grpc-compile\src\main\resources
[INFO] Copying 1 resource
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ grpc-compile ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 2 source files to D:\Code\Work\Telemetry\grpc-demo\grpc-compile\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.998 s
[INFO] Finished at: 2021-09-29T17:33:22+08:00
[INFO] ------------------------------------------------------------------------

会在src/main/java目录下生成代码:

5.插件参数说明

下面详细说明protobuf-maven-plugin插件配

configuration下的配置项:

  1. protocArtifact:指定Protobuf编译器protoc具体版本,用于生成Java消息对象
  2. pluginId:指定protoc的插件Id
  3. pluginArtifact:指定生成Java代码的具体插件版本,用于生成Java接口服务
  4. protoSourceRoot:proto文件所在目录
  5. outputDirectory:Java类输出目录
  6. clearOutputDirectory:是否需要清理输出目录

goals目标说明:

  1. compile:编译消息对象
  2. compile-custom :依赖上一步生成的消息对象,生成接口服务

说明一下,
上面clearOutputDirectory设置为false,
不清理输出目录,
是因为compile-custom时需要依赖上一步生成的消息对象,
如果清理了输出目录,
则compile-custom执行时会报错,
但是关闭了自动清理,
如果重新生成代码,
需要手动删除输出目录下的所有代码。

6.查看插件帮助手册

查看插件帮助手册(简略)
mvn help:describe -Dplugin=protobuf
查看插件帮助手册(详细,包含配置参数说明)
mvn help:describe -Dplugin=protobuf -Ddetail
查看插件帮助手册(详细,指定goal的说明)
mvn help:describe -Dplugin=protobuf -Dgoal=compile-custom -Ddetail

命令输出内容如下,
有部分省略:

Name: Maven Protocol Buffers Plugin
Description: Maven Plugin that executes the Protocol Buffers (protoc)compiler
Group Id: org.xolstice.maven.plugins
Artifact Id: protobuf-maven-plugin
Version: 0.6.1
Goal Prefix: protobufThis plugin has 15 goals:protobuf:compileDescription: This mojo executes the protoc compiler for generating mainJava sources from protocol buffer definitions. It also searches dependencyartifacts for .proto files and includes them in the proto_path so that theycan be referenced. Finally, it adds the .proto files to the project asresources so that they are included in the final artifact.
protobuf:compile-cppDescription: This mojo executes the protoc compiler for generating main C++sources from protocol buffer definitions. It also searches dependencyartifacts for .proto files and includes them in the proto_path so that theycan be referenced. Finally, it adds the .proto files to the project asresources so that they are included in the final artifact.
protobuf:compile-csharpDescription: This mojo executes the protoc compiler for generating main C#sources from protocol buffer definitions. It also searches dependencyartifacts for .proto files and includes them in the proto_path so that theycan be referenced. Finally, it adds the .proto files to the project asresources so that they are included in the final artifact.
protobuf:compile-customDescription: This mojo executes the protoc compiler with the specifiedplugin executable to generate main sources from protocol bufferdefinitions. It also searches dependency artifacts for .proto files andincludes them in the proto_path so that they can be referenced. Finally, itadds the .proto files to the project as resources so that they are includedin the final artifact.
protobuf:helpDescription: Display help information on protobuf-maven-plugin.Call mvn protobuf:help -Ddetail=true -Dgoal=<goal-name> to displayparameter details.
protobuf:test-compileDescription: This mojo executes the protoc compiler for generating testJava sources from protocol buffer definitions. It also searches dependencyartifacts in the test scope for .proto files and includes them in theproto_path so that they can be referenced. Finally, it adds the .protofiles to the project as test resources so that they can be included in thetest-jar artifact.
......
For more information, run 'mvn help:describe [...] -Ddetail'

7.参考文章

Maven Protocol Buffers Plugin Usage


http://www.taodudu.cc/news/show-1250947.html

相关文章:

  • Protobuf生成Java代码(命令行)
  • Maven查看插件信息
  • SpringBoot脚手架工程快速搭建
  • SpringBoot集成MyBatis-Plus分页插件
  • SNMP客户端工具MIB Browser
  • PowerDesigner运行自定义VBS脚本,复制Name到Comment
  • BitMap-BitSet(JDK1.8)基本使用入门
  • IDEA查看Java类的UML关系图
  • 30. 包含min函数的栈
  • 35. 复杂链表的复制
  • 58 - II. 左旋转字符串
  • 03. 数组中重复的数字
  • 53 - II. 0~n-1中缺失的数字
  • 04. 二维数组中的查找
  • 11. 旋转数组的最小数字
  • 50. 第一个只出现一次的字符
  • 32 - I. 从上到下打印二叉树
  • 32 - II. 从上到下打印二叉树 II
  • 32 - III. 从上到下打印二叉树 III
  • 26. 树的子结构
  • PostgreSQL数据库密码
  • SpringBoot中使用Hibernate Validator校验工具类
  • 28. 对称的二叉树
  • 解决tomcat的undeploy
  • 解决eclipse出现The superclass javax.servlet.http.HttpServlet was not found on the Java Build Path
  • 下载安装neo4j
  • vue-drag-resize实线页面的拖拽与缩放
  • 解决IDEA不能编译XML文件
  • 播放视频和音频文件java
  • 实时获取屏幕大小

Protobuf生成Java代码(Maven)相关推荐

  1. Protobuf生成Java代码(命令行)

    1.说明 本文介绍Protobuf生成Java代码的方法, 下载必须的Protobuf工具, 然后通过命令行, 把.proto文件生成Java代码. 2.准备Protobuf工具 2.1.获取prot ...

  2. M1 芯片maven 编译protobuf生成Java代码时,不能找到protoc-gen-grpc-java:exe:osx-aarch_64问题记录

    背景描述: 换电脑后使在m1芯片编译protobuf的maven插件,来生成项目代码,发现无法下载到arm版本的插件,报错如图: 本地使用 brew install protobuf安装的最新版本: ...

  3. YangTools从YANG生成Java类(Maven)

    1.说明 ODL提供了Yang Tools工具从YANG文件生成Java类, 本文介绍使用Maven插件的方式生成, 基于yang-maven-plugin这个插件. 2.创建Maven工程 Ecli ...

  4. Jsonschema2pojo从JSON生成Java类(Maven)

    1.说明 jsonschema2pojo工具可以从JSON Schema(或示例JSON文件)生成Java类型, 并且可以配置生成Jackson 1.x,Jackson 2.x, Moshi 1.x或 ...

  5. mybatis-plus生成java代码

    文章目录 前言 一.mybatis-plus生成java代码是什么 二.使用步骤 1.创建maven项目 2.导入项目依赖 3.创建代码生成器CodeGenerator.java 4.代码生成器Cod ...

  6. CXF wsdl2java 生成java代码供客户端使用

    CXF wsdl2java 生成java代码供客户端使用 环境配置: 1.下载apache-cxf-2.6.2在环境变量中配置CXF_HOME 值为E:\gavin\cxf\apache-cxf-3. ...

  7. 【Android APT】注解处理器 ( 根据注解生成 Java 代码 )

    文章目录 一.生成 Java 代码 二.实现 IButterKnife 接口 三.视图绑定主要操作 四.完整注解处理器代码 五.博客资源 Android APT 学习进阶路径 : 推荐按照顺序阅读 , ...

  8. 通过物理模型生成Java代码

    通过物理模型生成Java代码 软件开发过程中,我们一般是先针对数据库建模,物理建模完成后,生成数据库表,编码阶段的时候我们会针对数据库表生成大量的Javaeban或者是实体类 Powertdesign ...

  9. java插件开发_编写一个IDEA插件之:自动生成Java代码

    我很喜欢IDEA的一键自动生成代码功能,例如自动生成构造方法.字段的Get/Set方法.ToString方法等等,除此之外,也有一些插件提供自动生成代码的功能,例如我们所熟悉的GsonFormat插件 ...

最新文章

  1. 断网,启用网络,关机的实现。
  2. R语言使用ggplot2包和plotrix包绘制带有错误条(error bars)的可视化结果:使用ggplot2包绘制具有置信区间的可视化图像、使用plotrix包绘制具有置信区间的可视化图像
  3. [洛谷P4012] [网络流24题] 深海机器人问题
  4. IdentityServer4(八)使用EntityFramework Core对数据进行持久化
  5. 调用布尔变量java_关于java的参数的调用,还有布尔的理解,这有一段代码,我有些不太理解,希望能够帮我分析下,谢谢...
  6. 微机原理——移位指令
  7. 2022年中国足球球迷行为洞察白皮书
  8. C++工作笔记-VS中“调用堆栈”窗口的使用,实现越界的快速定位
  9. 记号的认识、公式的理解
  10. Qt的环境与工具、信号与槽
  11. 【科普】你所不了解的SWF文件
  12. android horizontalscrollview 动画,Android horizontalscrollview使用教程
  13. azure mysql数据库_Azure上创建MySql数据库服务
  14. 【Jupyter Notebook】slides演示小技巧
  15. 基于ESP8266的遥控小车
  16. U盘安装ubuntu 16.04 遇到 gfxboot.c32:not a COM32R image boot 的解决方法
  17. vuecli4关于Warning in ./src/plugins/element.js “export ‘default’ (imported as ‘Vue’) was not found in
  18. Minesweeper-Java
  19. Matlab中向图中添加文本
  20. PreScan交通流车流插件(ITM)- Intelligent Traffic Module Plugin与matlab(simlink)联合仿真使用实例

热门文章

  1. beyond company30天到期
  2. Docker+Nextcloud快速部署个人网盘
  3. msp430入门编程46
  4. (十)java多线程之CountDownLatch
  5. Oracle历史记录
  6. Vuejs 计算属性
  7. matlab sdk7.1,免费试用MATLAB Compiler SDK
  8. R7-7 寻找大富翁 (25 分)
  9. 两个有序链表序列的交集
  10. 阿里云自动java和mysql数据库_阿里云服务器之基于Linux系统部署上线JavaWeb项目和连接MySQL数据库(从购买云服务器到发布JavaWeb项目全套详细流程)...