Compile Protocol Buffers Using Maven
This tutorial explains how to compile protocol buffers using Maven.
Join the DZone community and get the full member experience.
Join For FreeHi 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:
xxxxxxxxxx
message Person {
required int32 id = 1;
required string name = 2;
optional string email = 3;
}
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.
xxxxxxxxxx
# Run this command to check the correct version of Maven is installed, in the path
$mvn --version
# It should echo the following line among other output.
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.
xxxxxxxxxx
./configure
make
sudo make install
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:
xxxxxxxxxx
cd maven-protoc-plugin
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:
xxxxxxxxxx
message HelloWorld {
required string message = 1;
}
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.
xxxxxxxxxx
protoc -I=./ --java_out=./ hello.proto
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.
xxxxxxxxxx
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.1.0</version>
</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.
xxxxxxxxxx
<build>
<plugins>
<plugin>
<groupId>com.github.os72</groupId>
<artifactId>protoc-jar-maven-plugin</artifactId>
<version>3.1.0.1</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<protocVersion>3.1.0</protocVersion>
<inputDirectories>
<include>src/main/resources</include>
</inputDirectories>
</configuration>
</execution>
</executions>
</plugin>
</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
Opinions expressed by DZone contributors are their own.
Comments