使用protoc-jar-maven-plugin生成grpc项目

embedded/2024/9/23 7:27:57/

在《使用protobuf-maven-plugin生成grpc项目》中我们使用protobuf-maven-plugin完成了grpc代码的翻译。本文我们将只是替换pom.xml中的部分内容,使用protoc-jar-maven-plugin来完成相同的功能。总体来说protoc-jar-maven-plugin方案更加简便。

环境

见《使用protobuf-maven-plugin生成grpc项目》

准备工作

目录结构

见《使用protobuf-maven-plugin生成grpc项目》

pom.xml

本次pom.xml的修改我们将基于《在不同操作系统上自动生成Protocol Buffers的Java语言包的方法2》。因为它是基于protoc-jar-maven-plugin翻译proto的message类型,而本文只需要新增对grpc的支持即可。

新增grpc依赖

这块的内容和《使用protobuf-maven-plugin生成grpc项目》中一致。

<properties><io-grpc.version>1.63.0</io-grpc.version>
</properties><dependencies><dependency><groupId>io.grpc</groupId><artifactId>grpc-netty-shaded</artifactId><version>${io-grpc.version}</version><scope>runtime</scope></dependency><dependency><groupId>io.grpc</groupId><artifactId>grpc-protobuf</artifactId><version>${io-grpc.version}</version></dependency><dependency><groupId>io.grpc</groupId><artifactId>grpc-stub</artifactId><version>${io-grpc.version}</version></dependency><dependency> <!-- necessary for Java 9+ --><groupId>org.apache.tomcat</groupId><artifactId>annotations-api</artifactId><version>6.0.53</version><scope>provided</scope></dependency></dependencies>

分割message和service生成

之前我们使用下面代码生成message类型代码。

<configuration><protoSourceRoot>${project.basedir}/src/main/proto</protoSourceRoot><outputDirectory>${project.basedir}/src/main/java/protojava</outputDirectory><protocArtifact>com.google.protobuf:protoc:${protobuf-java.version}:exe:${os.detected.classifier}</protocArtifact>
</configuration>

由于message和service类型生成指令不一样,于是我们要分开处理。这就要使用到outputTargets标签。而该标签和configuration/outputDirectory互斥,于是就要去掉configuration/outputDirectory,并在outputTargets/outputTarget下新增outputDirectory

<outputTargets><outputTarget><type>java</type><outputDirectory>src/main/java/protojava</outputDirectory></outputTarget><outputTarget><type>grpc-java</type><pluginArtifact>io.grpc:protoc-gen-grpc-java:${io-grpc.version}</pluginArtifact><outputDirectory>src/main/java/protojava</outputDirectory></outputTarget>
</outputTargets>

上侧java部分用于生成proto中的message部分;grpc-java则用于生成proto中的service部分。
protoc的翻译操作也不用像使用protobuf-maven-plugin方案那样,要执行一次protobuf:compile后再执行一次protobuf:compile-custom
。而只需要执行一次protoc-jar:run。
在这里插入图片描述

完整文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>proto-message-gen</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>21</maven.compiler.source><maven.compiler.target>21</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><protobuf-java.version>4.26.1</protobuf-java.version><io-grpc.version>1.63.0</io-grpc.version></properties><dependencies><dependency><groupId>com.google.protobuf</groupId><artifactId>protobuf-java</artifactId><version>${protobuf-java.version}</version></dependency><dependency><groupId>com.google.protobuf</groupId><artifactId>protobuf-java-util</artifactId><version>${protobuf-java.version}</version></dependency><dependency><groupId>io.grpc</groupId><artifactId>grpc-netty-shaded</artifactId><version>${io-grpc.version}</version><scope>runtime</scope></dependency><dependency><groupId>io.grpc</groupId><artifactId>grpc-protobuf</artifactId><version>${io-grpc.version}</version></dependency><dependency><groupId>io.grpc</groupId><artifactId>grpc-stub</artifactId><version>${io-grpc.version}</version></dependency><dependency> <!-- necessary for Java 9+ --><groupId>org.apache.tomcat</groupId><artifactId>annotations-api</artifactId><version>6.0.53</version><scope>provided</scope></dependency></dependencies><build><plugins><plugin><groupId>com.github.os72</groupId><artifactId>protoc-jar-maven-plugin</artifactId><version>3.11.4</version><executions><execution><phase>generate-sources</phase><goals><goal>run</goal></goals><configuration><protocArtifact>com.google.protobuf:protoc:${protobuf-java.version}</protocArtifact><inputDirectories><include>src/main/proto</include></inputDirectories><outputTargets><outputTarget><type>java</type><outputDirectory>src/main/java/protojava</outputDirectory></outputTarget><outputTarget><type>grpc-java</type><pluginArtifact>io.grpc:protoc-gen-grpc-java:${io-grpc.version}</pluginArtifact><outputDirectory>src/main/java/protojava</outputDirectory></outputTarget></outputTargets></configuration></execution></executions></plugin></plugins></build></project>

测试代码

见《使用protobuf-maven-plugin生成grpc项目》

代码仓库

https://github.com/f304646673/proto-gen.git

参考资料

  • https://os72.github.io/protoc-jar-maven-plugin/index.html

http://www.ppmy.cn/embedded/30297.html

相关文章

工厂模式和策略模式区别

工厂模式和策略模式都是面向对象设计模式&#xff0c;但它们的目的和应用场景有所不同。 工厂模式是一种创建型设计模式&#xff0c;旨在通过使用一个工厂类来创建对象&#xff0c;而不是直接使用new关键字来创建对象。这样做可以使系统更容易扩展和维护&#xff0c;因为新的对…

JavaScript中的Math对象方法、Date对象方法

个人主页&#xff1a;学习前端的小z 个人专栏&#xff1a;JavaScript 精粹 本专栏旨在分享记录每日学习的前端知识和学习笔记的归纳总结&#xff0c;欢迎大家在评论区交流讨论&#xff01; 文章目录 &#x1f31f;Math对象方法&#x1f344;1 Math静态属性&#x1f344;2 Math…

Go语言基本语法(五)标识符、关键字、操作符、分隔符和字面量

在Go语言中&#xff0c;程序的基本构建块包括标识符、关键字、操作符、分隔符和字面量&#xff0c;它们共同构成了Go语言的语法基础。以下是这些元素的详细介绍&#xff1a; 标识符&#xff08;Identifier&#xff09; Go语言中的标识符是用来命名程序元素&#xff08;如变量…

【计算机毕业设计】基于SSM++jsp的社区管理与服务系统【源码+lw+部署文档+讲解】

目录 摘 要 Abstract 第一章 绪论 第二章 系统关键技术 第三章 系统分析 3.1.1技术可行性 3.1.2经济可行性 3.1.3运行可行性 3.1.4法律可行性 3.4.1注册流程 3.4.2登录流程 3.4.3活动报名流程 第四章 系统设计 4.3.1登录模块顺序图 4.3.2添加信息模块顺序图 4.4.1 数据库E-…

C#核心之面向对象-封装

面向对象-封装 文章目录 1、类和对象1、什么是类2、类的声明3、类声明语法4、类声明实例5、对象(类)6、实例化对象语法7、实例化对象 2、成员变量和访问修饰符1、成员变量2、访问修饰符3、成员变量的使用和初始值 3、成员方法1、成员方法声明2、成员方法的使用 4、构造函数和…

贪心算法在Python、JavaScript、Java、C++和C#中的多样化实现及其在钱币找零、分数骑士、活动选择、最小生成树与哈夫曼编码问题中的应用示例

贪心算法是一种在每一步选择中都采取在当前状态下最好或最优&#xff08;即最有利&#xff09;的选择&#xff0c;从而希望导致结果是全局最好或最优的算法策略。贪心算法在有最优子结构的问题中尤为有效。下面我将提供5种不同编程语言实现的贪心算法示例&#xff0c;包括活动选…

linux(ubuntu18.04.2) Qt编译 MySQL(8.0以上版本)链接库 Qt版本 5.12.12及以上 包含Mysql动态库缺失问题

整理这篇文档的意义在于&#xff1a;自己走了很多弯路&#xff0c;淋过雨所以想为别人撑伞&#xff0c;也方便回顾&#xff0c;仅供参考 一、搭建开发环境&#xff1a; 虚拟机&#xff08;ubuntu-20.04.6-desktop-amd64&#xff09;&#xff1a;Mysql数据库 8.0.36Workbench …

【Linux驱动层】iTOP-RK3568学习之路(六):定时器

一、函数定义 Linux 内核中使用 timer_list 结构体表示内核定时器&#xff1a; struct timer_list {struct hlist_node entry;unsigned long expires; /* 定时器超时时间&#xff0c;单位是节拍数 */void (*function)(struct timer_list *); /* 定时处理函数 */u32 flags;…