Donnie

不积跬步无以至千里

使用maven插件生成protobuf文件

配置依赖

下面代码放入pom.xml里

    <properties>
            <protobuf.version>3.6.1</protobuf.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>com.google.protobuf</groupId>
                <artifactId>protobuf-java</artifactId>
                <version>${protobuf.version}</version>
            </dependency>
        </dependencies>
        
        <build>
            <extensions>
                <extension>
                    <groupId>kr.motd.maven</groupId>
                    <artifactId>os-maven-plugin</artifactId>
                    <version>1.5.0.Final</version>
                </extension>
            </extensions>
            <plugins>
    
                <!-- proto 编译组件 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>copy-protoc</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>copy</goal>
                            </goals>
                            <configuration>
                                <artifactItems>
                                    <artifactItem>
                                        <groupId>com.google.protobuf</groupId>
                                        <artifactId>protoc</artifactId>
                                        <version>${protobuf.version}</version>
                                        <classifier>${os.detected.classifier}</classifier>
                                        <type>exe</type>
                                        <overWrite>true</overWrite>
                                        <!-- 生成的Java类目录-->
                                        <outputDirectory>${project.build.directory}</outputDirectory>
                                    </artifactItem>
                                </artifactItems>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
    
                <plugin>
                    <groupId>org.xolstice.maven.plugins</groupId>
                    <artifactId>protobuf-maven-plugin</artifactId>
                    <version>0.5.1</version>
                    <extensions>true</extensions>
                    <configuration>
                        <!-- porto源文件-->
                        <protoSourceRoot>${project.basedir}/src/main/java/com/donnie/proto</protoSourceRoot>
                        <protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}</protocArtifact>
                        <pluginId>grpc-java</pluginId>
                        <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.0.0:exe:${os.detected.classifier}</pluginArtifact>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>compile</goal>
                                <goal>compile-custom</goal>
                                <goal>test-compile</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
    
            <!-- 编译jar包的jdk版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
    
            </plugins>
        </build>

proto文件

user.porto 放入com.donnie.proto目录

    syntax = "proto3";
    option java_package = "com.donnie.proto";
    option java_outer_classname = "UserModel";
    
    message User {
         int32 id = 1;
         string name = 2;
         string email = 3;
         string sex = 4;
    }

测试类


    import com.donnie.proto.UserModel;
    
    public class Test {
    
        public static void main(String[] args) {
            UserModel.User.Builder user = UserModel.User.newBuilder();
            user.setId(1);
            user.setSex("男");
            user.setEmail("oox@163.com");
    
            System.out.println(user);
        }
    }

intellij Idea 安装protobuf插件

安装插件protobuf Support,效果如图:

赞赏支持