Compile Protocol Buffers Using Maven

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)

 

 

 

 

Top