📜  protobuf java gradle mvn - Java (1)

📅  最后修改于: 2023-12-03 15:03:52.604000             🧑  作者: Mango

Protobuf with Java

Protocol Buffers, also known as protobuf, is a language- and platform-neutral mechanism for serializing structured data. It is widely used in distributed computing and for communication between microservices. This article will focus on how to use protobuf with Java using Gradle and Maven build tools.

Installation

First, we need to install protobuf on our system. Follow the instructions on the official protobuf website to download and install protobuf.

Creating a .proto file

Next, we need to create a .proto file to define our message types. Here's an example:

syntax = "proto3";

package com.example;

message Person {
  string name = 1;
  int32 age = 2;
  repeated string addresses = 3;
}

In this example, we define a message type called Person with three fields: name of type string with a tag of 1, age of type int32 with a tag of 2, and addresses of type string with a tag of 3.

Generating Java code from the .proto file

Now that we have our .proto file, we need to generate Java code from it. We can do this using the protoc command-line tool with the protobuf Java plugin. Here's an example:

$ protoc --java_out=src/main/java src/main/proto/person.proto

This will generate a Java file Person.java under the com/example package in the src/main/java directory.

Using protobuf with Gradle

If we're using Gradle as our build tool, we can add the protobuf-gradle-plugin to our build.gradle file. Here's an example:

plugins {
    id 'java'
    id 'com.google.protobuf' version '0.8.15'
}

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.12.0'
    }
    plugins {
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.30.0'
        }
    }
    generateProtoTasks {
        all()*.plugins {
            grpc {}
        }
    }
}

dependencies {
    implementation 'io.grpc:grpc-protobuf:1.30.0'
}

In this example, we're using protobuf and gRPC. We specify the version of protobuf in the artifact property, and the version of the gRPC protoc plugin in the plugins.grpc.artifact property. We also configure our protocol buffer generation by adding the grpc plugin to all generations in generateProtoTasks.all()*.plugins.grpc{}. Lastly, we specify the gRPC protobuf dependency in the dependencies block.

Using protobuf with Maven

If we're using Maven as our build tool, we can add the protobuf-maven-plugin to our pom.xml file. Here's an example:

<build>
    <plugins>
        <plugin>
            <groupId>org.xolstice.maven.plugins</groupId>
            <artifactId>protobuf-maven-plugin</artifactId>
            <version>0.6.1</version>
            <configuration>
                <protocArtifact>com.google.protobuf:protoc:3.12.0</protocArtifact>
                <pluginId>grpc-java</pluginId>
                <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.30.0</pluginArtifact>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>compile-custom</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>com.google.protobuf</groupId>
        <artifactId>protobuf-java</artifactId>
        <version>3.12.0</version>
    </dependency>
    <dependency>
        <groupId>io.grpc</groupId>
        <artifactId>grpc-protobuf</artifactId>
        <version>1.30.0</version>
    </dependency>
</dependencies>

In this example, we're using the protobuf-maven-plugin to generate Java code from our .proto files. We specify the version of protobuf in the protocArtifact property, and the version of the gRPC protoc plugin in the pluginArtifact property. We also configure our plugin by specifying the pluginId property. Lastly, we specify the protobuf and gRPC protobuf dependencies in the dependencies block.

Conclusion

In this article, we learned how to use protobuf with Java using Gradle and Maven build tools. We created a .proto file, generated Java code from it, and then configured our build files to use protobuf and gRPC dependencies.