Compile Protocol Buffers Using Maven

DZone 's Guide to

Compile Protocol Buffers Using Maven

This tutorial explains how to compile protocol buffers using Maven.

· Java Zone ·
Free Resource

Hi folks! In this article, we will explore how to compile protocol buffers using Maven. We assume the reader has a basic idea about protocol buffers and Maven.

Google developed protocol buffers for use in their internal services. It is a binary encoding format that allows you to specify a schema for your data using a specification language, like so:


The snippet defines the schema for a Person data type that has three fields: id, name, and email. In addition to naming a field, you can provide a type that will determine how the data is encoded and sent over the wire – above we see an int32 type and a string type. Keywords for validation and structure are also provided (required and optional above), and fields are numbered, which aids in backward compatibility. You can read more about protocol buffers through this link https://developers.google.com/protocol-buffers.

You may also like: Using Google’s Protocol Buffers With Java


Before going further, you will need to install the following tools.

Java  SDK

The first step is to download and install Java. In this tutorial, we will be generating code for Java.


Next, we download and install Apache Maven. The installation process is simple enough, so I won’t get into the details. Please feel free to ask questions in the comments or the forum if you get stuck.


Protocol Buffer Compiler

You will need to install the protocol buffer compiler. Detailed installation instructions for the protocol buffer compiler can be found through this link: https://github.com/protocolbuffers/protobuf.

However, the basic steps are to unzip the package, browse to the directory in the terminal run the following commands.


Maven Protoc Plugin 

In order to compile the protocol buffer, you need to compile and install the Maven Protoc Plugin. Once you have the source, you can run the following commands to compile the plugin:


Writing Your Proto Files

cd to src/main/resources or src/main/protobuf directory and create a new file called hello.proto and add the following text:


We can test out protocol buffer compiler installation and that our file is valid by running the command shown below. It will generate the Hello.java file.


Generating Source Files

Now we get to the automatic generation of the source files, which we do using the maven-protoc-plugin we installed earlier. Open the pom file at protoccompiler/pom.xml, look for the “dependencies” element, and add the following dependency to pull in protocol buffer support files.


Now look for the “plugins” element and add the plugin definition below. We are invoking the “protoc-jar-maven-plugin” that we compiled earlier in the tutorial by specifying its groupId and artifactId. Note that this assumes that protoc is in the path. If this is not the case, you can specify the fully qualified path to the binary. Using “inputDirectories", we are specifying the location of the proto files. The plugin looks for files in the specified directory with the “.proto” extension. 


Once you make the changes, save the pom file and compile the project using the mvn clean install command. You should see the generated sources in target/generated-sources.

Here's the link to source code:


Further Reading

Building Scalable Microservices With gRPC

Is Protobuf 5x Faster Than JSON? (Part 2)

protocol buffers ,protobuf ,java ,maven

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}