protobuf 和 intellij 配置使用

来源:互联网 发布:单片机 电化学工作站 编辑:程序博客网 时间:2024/06/06 08:45

protobuf intellij 使用

简介

Protocol Buffer 简称 ProtoBuf 是用于结构化数据串行化的灵活、高效、自动的方法,又如 XML,不过它更小、更快、也更简单。你可以定义自己的数据结构,然后使用代码生成器生成的代码来读写这个数据结构。你甚至可以在无需重新部署程序的情况下更新数据结构

安装

intellij 安装 使用protobuf

  1. 在intellij file-> setting>plugins 搜索安装protobuf 支持 安装 重启idea

  2. maven依赖插件

    定义版本号 <properties>    <!--grpc版本号-->    <grpc.version>1.7.0</grpc.version>    <!--protobuf 版本号-->    <protobuf.version>3.4.0</protobuf.version></properties>在build 标签里加入 <extensions>    <extension>        <groupId>kr.motd.maven</groupId>        <artifactId>os-maven-plugin</artifactId>        <version>1.5.0.Final</version>    </extension></extensions>在build 标签  plugins 下加入<plugin>        <groupId>org.xolstice.maven.plugins</groupId>        <artifactId>protobuf-maven-plugin</artifactId>        <version>0.5.1</version>        <configuration>            <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>maven 加入依赖库 <dependency>    <groupId>com.google.protobuf</groupId>    <artifactId>protobuf-java</artifactId>    <version>${protobuf.version}</version></dependency><dependency>    <groupId>io.grpc</groupId>    <artifactId>grpc-netty</artifactId>    <version>${grpc.version}</version>    <scope>provided</scope></dependency><dependency>    <groupId>io.grpc</groupId>    <artifactId>grpc-protobuf</artifactId>    <version>${grpc.version}</version>    <scope>provided</scope></dependency><dependency>    <groupId>io.grpc</groupId>    <artifactId>grpc-stub</artifactId>    <version>${grpc.version}</version>    <scope>provided</scope></dependency>
  3. maven 下载 插件和依赖

    点击下图的刷新按钮 若不知道在哪,可以在 菜单栏view->Tool windows->maven projects 就可以显示,下载有点慢

  4. 下载完成后会在项目target 目录下生protoc-plugins 生成.exe可执行文件h和一系列依赖文件

  5. 建立.proto文件,将.proto文件放在src main 目录下新建的proto文件夹下,默认情况下,maven 编译时会去下面去寻找.proto 文件

    syntax = "proto3";option java_package = "com.ynt.mediaquality.modeanalyze.protobuf";option java_outer_classname = "test";message Person {int32 id = 1;string name = 2;string email = 3;}
  6. 将.proto 文件编译成java文件,双击下图的箭头所示

    控制台出现

    [INFO] — protobuf-maven-plugin:0.5.1:compile (default-cli) @ modeAnalyze —
    [INFO] Compiling 1 proto file(s) to E:\IdeaProjects\mediaQuality\modeAnalyze\target\generated-sources\protobuf\java
    [INFO] ————————————————————————
    [INFO] BUILD SUCCESS

    表示成功

  7. 编译后文件出现在 文件 target ->protobuf -> java下

原创粉丝点击