Compile protocol buffers using maven

Reading Time: 3 minutes

Hi Folks! As part of this blog, we will explore how we can compile protocol buffers using maven. We assume the reader has the 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:

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: idname, 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

Prerequisite

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 and I won’t get into the details. Please feel free to ask questions in the comments or the forum if you get stuck.

# 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.

./configure
make
sudo make install
protoc --version

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

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:

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.

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.

<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.

<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 the link of source code

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

Conclusion

After reading this blog, you must have idea how we can compile protocol buffers using maven.

Reference

https://developers.google.com/protocol-buffers/docs

Written by 

Munander is a Software Consultant in Knoldus Software LLP. He has done b.tech from IMS Engineering college, Ghaziabad. He has decent knowledge of C,C++,Java,Angular and Lagom. He always tries to explore new technologies. His hobbies include playing cricket and adventure.

1 thought on “Compile protocol buffers using maven4 min read

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading