DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • What Are Protocol Buffers?
  • Next Evolution in Integration: Architecting With Intent Using Model Context Protocol
  • Integrating Model Context Protocol (MCP) With Microsoft Copilot Studio AI Agents
  • Using Python Libraries in Java

Trending

  • Cloud Security and Privacy: Best Practices to Mitigate the Risks
  • Beyond Simple Responses: Building Truly Conversational LLM Chatbots
  • Build a Simple REST API Using Python Flask and SQLite (With Tests)
  • MySQL to PostgreSQL Database Migration: A Practical Case Study
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Compile Protocol Buffers Using Maven

Compile Protocol Buffers Using Maven

This tutorial explains how to compile protocol buffers using Maven.

By 
Munander Singh user avatar
Munander Singh
·
Mar. 06, 20 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
64.0K Views

Join the DZone community and get the full member experience.

Join For Free

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:

ProtoBuf
 




xxxxxxxxxx
1


 
1
message Person {
2
  required int32 id = 1;
3
  required string name = 2;
4
  optional string email = 3;
5
}



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

Prerequisites

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.

Maven

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.

Java
 




xxxxxxxxxx
1


 
1
# Run this command to check the correct version of Maven is installed, in the path
2
$mvn  --version
3
# It should echo the following line among other output.
4
Apache Maven 3.X.X ....



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.

Java
 




xxxxxxxxxx
1


 
1
./configure
2
make
3
sudo make install
4
protoc --version



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:

Java
 




xxxxxxxxxx
1


 
1
cd maven-protoc-plugin
2
mvn clean install



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:

Java
 




xxxxxxxxxx
1


 
1
message HelloWorld {
2
  required string message = 1;
3
}



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.

Java
 




xxxxxxxxxx
1


 
1
protoc -I=./ --java_out=./ hello.proto
2
ls -l



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.

XML
 




xxxxxxxxxx
1


 
1
<dependency>
2
    <groupId>com.google.protobuf</groupId>
3
    <artifactId>protobuf-java</artifactId>
4
    <version>3.1.0</version>
5
</dependency>



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. 

XML
 




xxxxxxxxxx
1
22


 
1
<build>
2
        <plugins>
3
            <plugin>
4
                <groupId>com.github.os72</groupId>
5
                <artifactId>protoc-jar-maven-plugin</artifactId>
6
                <version>3.1.0.1</version>
7
                <executions>
8
                    <execution>
9
                        <phase>generate-sources</phase>
10
                        <goals>
11
                            <goal>run</goal>
12
                        </goals>
13
                        <configuration>
14
                            <protocVersion>3.1.0</protocVersion>
15
                            <inputDirectories>
16
                                <include>src/main/resources</include>
17
                            </inputDirectories>
18
                        </configuration>
19
                    </execution>
20
                </executions>
21
            </plugin>
22
        </plugins>



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:

https://github.com/Munandermaan/Compile-Protocol-buffers-using-maven.

Further Reading

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

Protocol Buffers Protocol (object-oriented programming) Buffer (application) Apache Maven

Opinions expressed by DZone contributors are their own.

Related

  • What Are Protocol Buffers?
  • Next Evolution in Integration: Architecting With Intent Using Model Context Protocol
  • Integrating Model Context Protocol (MCP) With Microsoft Copilot Studio AI Agents
  • Using Python Libraries in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!