Idea中如何使用protobuf插件

1. 新建Maven项目

在这里插入图片描述

2. 引入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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>protobuff-parser</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <protobuf-maven-plugin.version>0.5.1</protobuf-maven-plugin.version>
        <protoc.version>3.5.1-1</protoc.version>
        <grpc.version>1.13.1</grpc.version>
    </properties>


    <build>
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.5.0.Final</version>
            </extension>
        </extensions>

        <plugins>
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>${protobuf-maven-plugin.version}</version>
                <configuration>
                    <protocArtifact>com.google.protobuf:protoc:${protoc.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>
        </plugins>
    </build>
</project>

只需要引入protobuf-maven-plugin插件即可,os-maven-plugin是避免os.detected.classifier报红引入的

3. 编写proto文件

addressbook.proto

syntax = "proto3";

package tutorial;

option java_multiple_files = true;
option java_package = "com.example.tutorial.protos";
option java_outer_classname = "AddressBookProtos";

// Person message 表示一个人的信息,包含可选字段。
message Person {
  string name = 1;     // 人的姓名。
  int32 id = 2;        // 人的唯一标识符。
  string email = 3;    // 人的电子邮件地址。

  // PhoneType 枚举定义不同类型的电话号码。
  enum PhoneType {
    PHONE_TYPE_UNSPECIFIED = 0;  // 默认值,未指定电话类型。
    PHONE_TYPE_MOBILE = 1;       // 手机号码。
    PHONE_TYPE_HOME = 2;         // 家庭电话号码。
    PHONE_TYPE_WORK = 3;         // 工作电话号码。
  }

  // PhoneNumber message 表示一个电话号码,包含可选的类型。
  message PhoneNumber {
    string number = 1;         // 电话号码字符串。
    PhoneType type = 2;       // 电话号码的类型,默认为 HOME。
  }

  repeated PhoneNumber phones = 4;  // 与此人相关联的电话号码列表。
}

// AddressBook message 表示人员的地址簿。
message AddressBook {
  repeated Person people = 1;  // 地址簿中的人员列表。
}

4. 使用插件编译

整体项目结构:

在这里插入图片描述

  • 刷新maven,idea加载插件后,maven的Plugins就会出现protobuf插件,双击 protobuf:compile

在这里插入图片描述

  • 控制台显示编译成功

    在这里插入图片描述

  • 在项目目录中,查看到target目录,找到generated-sources --> protobuf 文件夹,这里的文件就是Java结构

在这里插入图片描述
项目地址(plugin分支):

https://gitee.com/javadage/protobuff-parser

https://github.com/JavaSqh/protobuff-parser.git

有任何问题请在下方留言。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值