maven 使用插件方式编译protobuf3 协议

<?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>im3</artifactId>
        <groupId>com.im</groupId>
        <version>3.0.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>im3-proto</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>3.11.4</version>
        </dependency>
    </dependencies>


    <build>
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.4.1.Final</version>
            </extension>
        </extensions>
        <plugins>
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.5.1</version>
                <configuration>
                    <protocArtifact>
                        com.google.protobuf:protoc:3.1.0:exe:${os.detected.classifier}
                    </protocArtifact>
                    <pluginId>grpc-java</pluginId>
                </configuration>
<!--                <executions>-->
<!--                    <execution>-->
<!--                        <goals>-->
<!--                            <goal>compile</goal>-->
<!--                            <goal>compile-custom</goal>-->
<!--                        </goals>-->
<!--                    </execution>-->
<!--                </executions>-->
            </plugin>
        </plugins>
    </build>
</project>

protobuf3 协议文件例子

ProtoMsg.proto

syntax = "proto3";
option java_package = "com.im.common.proto";
option java_outer_classname = "MessageProto";

enum HeadType {
    LOGIN_REQUEST = 0; //登陆请求
    LOGIN_RESPONSE = 1; //登录响应
    LOGOUT_REQUEST = 2; //退出请求
    LOGOUT_RESPONSE = 3;
    KEEPALIVE_REQUEST = 4; //心跳请求PING;
    KEEPALIVE_RESPONSE = 5;
    MESSAGE_REQUEST = 6; //消息请求;
    MESSAGE_RESPONSE = 7; //消息回执;
    MESSAGE_NOTIFICATION = 8; //通知消息
}

/*通知消息*/
message MessageNotification {
    uint32 msg_type = 1; //通知类型 1 上线 2 下线 ...
    bytes sender = 2;
    string json = 3;
    string timestamp = 4;
}

/*登录信息*/
// LoginRequest对应的HeadType为Login_Request
// 消息名称去掉下划线,更加符合Java 的类名规范
message LoginRequest {
    string uid = 1; // 用户唯一id
    string deviceId = 2; // 设备ID
    string token = 3; // 用户token
    uint32 platform = 4; //客户端平台 windows、mac、android、ios、web
    string app_version = 5; // APP版本号
}

//token说明: 账号服务器登录时生成的Token
/*登录响应*/
message LoginResponse {
    bool result = 1; //true 表示成功,false表示失败
    uint32 code = 2; //错误码
    string info = 3; //错误描述
    uint32 expose = 4; //错误描述是否提示给用户:1 提示;0 不提示
    string session_id = 5; //sessionId
}

/*聊天消息*/
message MessageRequest {
    uint64 msg_id = 1; //消息id
    string from = 2; //发送方uId
    string to = 3; //接收方uId
    uint64 time = 4; //时间戳(单位:毫秒)
    uint32 msg_type = 5; //消息类型  1:纯文本  2:音频 3:视频 4:地理位置 5:其他
    string session_id = 6; //sessionId
    string content = 7; //消息内容
    string url = 8; //多媒体地址
    string property = 9; //附加属性
    string from_nick = 10; //发送者昵称
    string json = 11; //附加的json串
}

message MessageResponse {
    bool result = 1; //true表示发送成功,false表示发送失败
    uint32 code = 2; //错误码
    string info = 3; //错误描述
    uint32 expose = 4; //错误描述是否提示给用户:1 提示;0 不提示
    bool last_block = 5;
    fixed32 block_index = 6;
}

/*顶层消息*/
//顶层消息是一种嵌套消息,嵌套了各种类型消息
//内部的消息类型,全部使用optional字段
//根据消息类型 type的值,最多只有一个有效
message Message {
    HeadType type = 1; //消息类型
    uint64 sequence = 2; //消息系列号,主要用于Request和Response,Response的值必须和Request相同,使得发送端可以进行事务匹配处理
    fixed32 session_id = 3;
    LoginRequest loginRequest = 4;
    LoginResponse loginResponse = 5;
    MessageRequest messageRequest = 6;
    MessageResponse messageResponse = 7;
    MessageNotification notification = 8;
}

工程结构

 注意:

proto 文件放在 src/main/proto 文件夹下

执行mvn命令

在编译路径下会生成 Java文件

 把Java代码和包路径复制到 src/main/java 下

原文地址:https://www.cnblogs.com/xiaojf/p/12792179.html