Generate Java code for service contract in gRPC using Protocol Buffers Maven Plugin

When working with gRPC applications in Java, using the Protocol Buffer Compiler tool to generate Java code for service contracts is necessary. There are many ways to use this Protocol Buffer Compiler, either you can install and use the Protocol Buffer Compiler file directly here, or you can use it through a Maven plugin. For convenience, you can use a Maven plugin. There are also many Maven plugins that can do this, one of which is called Protocol Buffers Maven Plugin. In this tutorial, I will guide you on using the Protocol Buffers Maven Plugin to generate Java code for service contracts in gRPC!

First, I will create a new Maven project as an example:

You need to declare gRPC dependencies as follows:

with “grpc.version” having the following value:

I will create a new .proto file defining a simple service contract for the gRPC server located in the src/main/proto directory:

has the following content:

Now, we will declare to use Protocol Buffers Maven Plugin to generate Java code!

I will declare this plugin as follows:

There are 2 necessary configurations related to using Protocol Buffer Compiler and gRPC Java Codegen plugin for Protocol Buffer Compiler to generate messages and services defined in .proto file, which you need to declare with Protocol Buffers Maven Plugin.

As you can see above, I declared to use Protocol Buffer Compiler using plugin parameter <protocArtifact> with the value pointing to the protoc dependency. Format when declaring this dependency is groupId:artifactId:version[:type[:classifier]], guys! The protoc dependency will detect the operating system you are using to download the appropriate Protocol Buffer Compiler execution file.

For each version, you will see on the Maven Central Repository, that many execution files of Protocol Buffer Compiler are stored here. For example, for the version of the protoc dependency that I declared 3.25.4, if you go to the address https://repo1.maven.org/maven2/com/google/protobuf/protoc/3.25.4/, you will see the following result:

As for the gRPC Java Codegen plugin for Protocol Buffer Compiler, I use the plugin parameter <pluginArtifact>. The format is similar to the declaration for the plugin parameter <protocArtifact>!

Similar to the proc dependency, the execution files of the gRPC Java Codegen plugin are also stored on the Maven Central Repository. For my configuration above, if you go to https://repo1.maven.org/maven2/io/grpc/protoc-gen-grpc-java/1.66.0/, you will also see the following result:

Here, I also use another Maven plugin called os-maven-plugin to get information about the operating system and chip of the computer that we are using so that the Protocol Buffers Maven Plugin can use the correct execution file it needs to use.

For Macbook M1 laptops and above, you need to install Rosetta software using the command:

to create an adaptive bridge between Intel processors and Apple’s new Apple Silicon chips, so that you can run the execution file of Protocol Buffer Compiler!

There are 2 goals of Protocol Buffers Maven Plugin that you need to use: compile and compile-custom. The compile goal will use the Protocol Buffer Compiler to generate Java classes for messages defined in the .proto file, and the compile-custom goal will be used by the plugins that you declare for the Protocol Buffer Compiler. Depending on the purpose, those plugins will generate Java code based on the definition of the .proto file.

By default, the Protocol Buffers Maven Plugin will use .proto files in the /src/main/proto directory to generate source code, so now, if you run Maven Compile “mvn compile”, you will see Protocol Buffers Maven Plugin generate Java files in the /target/generated-sources/protobuf directory as follows:

The grpc-java folder will contain the Java code of the service contract and the java folder will contain the Java code of the gRPC messages!

You also need to declare the javax.annnotation-api dependency so that our project can compile:

At this point, we have completed using Protocol Buffers Maven Plugin to generate Java code for service contracts in gRPC!

Add Comment