当前位置:网站首页>idea 编译protobuf 文件的设置使用
idea 编译protobuf 文件的设置使用
2022-07-30 05:16:00 【nandao158】
protobuf 是一种数据格式,文件是.proto 格式的,有时需要编译成java 文件 ,目前流行 idea 工具开发并编译。那么应该怎么准备环境呢?我这有两种方案:下载idea插件编译和通过maven 编译:
二者相同的步骤如下:idea 下载插件
一、下载idea插件编译:

1、安装成功后重启:出现以下按钮说明安装成功。

2、官网下载protobuf 编译工具:protoc-3.11.8-win64.rar,解压打开后显示:
例如: D:\Program Files\protoc-3.11.4-win64\bin\proto.exe

3、点开 Idea上插件 Google ProtobufTool 按钮,点击setting 设置编译相关目录

4、通过有边的Browse 按钮选择相应目录:
protoc_path :就是刚刚下载的工具的目录+命令(=后面是需要编译的.proto 文件的目录),
比如: D:\Program Files\protoc-3.11.4-win64\bin\proto.exe -I=E:\ideaProject\nmp-sdk\nmp-sdk\proto
protofile_path:需要编译的.proto 文件的目录
out_path :编译生成的java文件输出的目录
5、点击成功后进入这个页面:

6、选择一些文件点击Gen 就会编译 如图 :

7、编译成功图:

二、maven 编译方案 下篇详细介绍,这个方案比较简单易用
1、pom.xml 中引用相关jar:
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId> protobuf-java</artifactId>
<version>3.9.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-core</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf-lite</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>${grpc.version}</version>
</dependency>插件需要的信息
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--proto编译-->
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.1</version>
<configuration>
<!--protobuf 编译成java 文件时用这个目录,二选一-->
<!-- <protoSourceRoot>${project.basedir}/proto</protoSourceRoot>-->
<!--生成后的java 文件 编译成jar包时用这个目录,二选一-->
<protoSourceRoot>${project.basedir}/src</protoSourceRoot>
<!--<outputDirectory>${project.build.directory}/generated-sources/protobuf/java</outputDirectory>-->
<!--编译后文件的输出的目录-->
<outputDirectory>${project.build.sourceDirectory}</outputDirectory>
<!--设置是否在生成java文件之前清空outputDirectory的文件,默认值为true-->
<clearOutputDirectory>false</clearOutputDirectory>
<!--suppress UnresolvedMavenProperty -->
<protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>extensions 扩展配置:
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.5.0.Final</version>
</extension>
</extensions>grpc 配置
<!--grpc版本号-->
<grpc.version>1.6.1</grpc.version>
<!--protobuf 版本号-->
<protobuf.version>3.9.0</protobuf.version>2、引入成功后打开maven插件,点击此处,生成相应的java文件:
(根据个人需求,我这里配置这个:<protoSourceRoot>${project.basedir}/proto</protoSourceRoot>)

3、然后把相关java文件打成单独的jar 包:
(根据个人需求,我这里配置这个:<protoSourceRoot>${project.basedir}/src</protoSourceRoot>)

4、此处编译成功,最后对该项目pom文件里的标签整体解析一下,加深大家的理解:
<?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">
<parent>
<artifactId>nmp-sdk</artifactId>
<groupId>com.navinfo.nmp</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>${parent.groupId}</groupId>
<artifactId>nmp-sdk-proto</artifactId>
<packaging>jar</packaging><!--项目产生的构件类型,例如jar、war、ear、pom。插件可以创建他们自己的构件类型,所以前面列的不是全部构件类型 -->
<version>1.0-SNAPSHOT</version>
<!--此次jar包引入 -->
<dependencies>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<!--fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.33</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>org.locationtech.jts</groupId>
<artifactId> jts-core</artifactId>
<version>1.15.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId> lombok</artifactId>
<version>1.18.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId> protobuf-java</artifactId>
<version>3.9.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-core</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf-lite</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>${grpc.version}</version>
</dependency>
</dependencies>
<!--项目分发信息,在执行mvn deploy后表示要发布的位置。有了这些信息就可以把网站部署到远程服务器或者把构件部署到远程仓库。 -->
<distributionManagement>
<repository>
<id>lock-releases</id>
<name>Release Deploy</name>
<url>http://10.60.145.1:881/repository/maven-releases/</url>
</repository>
<!--构件的快照部署到哪里?如果没有配置该元素,默认部署到repository元素配置的仓库,参见distributionManagement/repository元素 -->
<snapshotRepository>
<id>lock-snapshot</id>
<name>Snapshot Deploy</name>
<url>http://10.60.145.241:881/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
<build>
<finalName>nmp-sdk-proto</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.*</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
<!--protobuf相关配置 -->
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.1</version>
<configuration>
<!--protobuf 编译成java 文件时用这个目录,二选一-->
<protoSourceRoot>${project.basedir}/proto</protoSourceRoot>
<!--生成后的java 文件 编译成jar包时用这个目录,二选一-->
<!-- <protoSourceRoot>${project.basedir}/src</protoSourceRoot>-->
<!--默认值-->
<outputDirectory>${project.build.sourceDirectory}</outputDirectory>
<!--设置是否在生成java文件之前清空outputDirectory的文件,默认值为true-->
<clearOutputDirectory>false</clearOutputDirectory>
<!--suppress UnresolvedMavenProperty -->
<protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<!--suppress UnresolvedMavenProperty -->
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<!--使用来自该项目的一系列构建扩展 -->
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.5.0.Final</version>
</extension>
</extensions>
</build>
<!--项目开发者属性,如即时消息如何处理等 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<enduro.version>0.0.1-SNAPSHOT</enduro.version>
<!--grpc版本号-->
<grpc.version>1.6.1</grpc.version>
<!--protobuf 版本号-->
<protobuf.version>3.9.0</protobuf.version>
</properties>
</project>5、到此就 结束了,有不明白的地方欢迎留言!
边栏推荐
猜你喜欢

429. N 叉树的层序遍历(两种解法)

mysql高阶语句(一)

MySQL(4)

【Redis高手修炼之路】Jedis——Jedis的基本使用

el-table中加入el-input框和el-input-number框,实现el-table的可编辑功能

Kyligence 亮相第五届南方信息大会并获评“CIO 优选数字化服务商”

容器化 | 在 K8s 上部署 RadonDB MySQL Operator 和集群

Golang go-redis cluster模式下不断创建新连接,效率下降问题解决

How MySQL to prepare SQL pretreatment (solve the query IN SQL pretreatment can only query out the problem of a record)

This article will take you through js to deal with the addition, deletion, modification and inspection of tree structure data
随机推荐
Kyligence 再获 CRN, insideBIGDATA 两大国际奖项认可
mysql cannot connect remotely Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (10060 "Unknown error")
【小程序项目开发 -- 京东商城】uni-app 商品分类页面(下)
MySQL(4)
关于组织开展2022年广东省技术先进型服务企业认定工作的通知
C语言实现安全性极高的游戏存档并读档
pytorch官网中如何选择以及后面的安装和pycharm测试步骤
Hexagon_V65_Programmers_Reference_Manual (11)
ThinkPHP high imitation blue play cloud network disk system source code / docking easy payment system program
容器化 | 在 KubeSphere 中部署 MySQL 集群
无代码开发平台子管理员入门教程
Golang go-redis cluster模式下不断创建新连接,效率下降问题解决
MySQL - 函数及约束命令
1475. 商品折扣后的最终价格
ugly programmer
丑陋的程序员
《后浪》程序员版,献给新一代程序员的演讲,何冰《后浪》演讲模仿秀
MySQL索引常见面试题(2022版)
Go语学习笔记 - gorm使用 - 事务操作 Web框架Gin(十一)
Seata异常:endpoint format should like ip:port