SriK SriK - 5 months ago 45x
Java Question

Compose Protocol buffers (protobuf) definitions hierarchically by referencing other protobuf messages in other repositories or projects or jar files

The big picture is that I have a protobuf (think of it like a class) datatype that refers to another protobuf that is within another Jar file that is a dependency in my POM file. This works perfectly for .java files but unfortunately doesn't work for protobuf files. The best solution I can think of is to tell Maven to extract this other dependency Jar (that contain proto files) file in a location and then tell Maven to do a protoc compile of all these proto files in that location. Alas, I dont know how to tell Maven to do this. Any help would be greatly appreciated as we use standard Jar files to capture our proto files.

import "X.proto"; // refers to a proto file in another jar

import "Y.proto";

message A {

repeated X o = 1; // This cant be done
repeated Y e = 2;

The above will not work since X is not in the same path as this file.


I found the solution for this in Gradle. Fill in the blanks below and point your repo correctly, and you should be able to get the below to work. Now you can hierarchically compose protobufs across multiple projects through proto files within other jar files !

In gradle, you can use the following:

buildscript { // these are your protobuf plugin repositories

repositories { maven {url 'WHERE YOUR PROTOBUF PLUGIN EXISTS. e.g. http://maven or MavenCentral()'} }

dependencies {

   classpath ''

   classpath ''

} }

apply plugin: ''

group = "YOUR PACKAGE NAME. e.g. com.x.y" version = "1.0" // release version

repositories { // these are your regular dependencies repositories. This could be very similar to the ones in buildscript above.



protobuf { // this is needed by the plugin and is where teh magic happens. It tells protobuf where to extract all the proto files that it finds in all the dependency jar files you have



sourceSets { // also magic you need for this protobuf plugin to work

main {

    proto {  // this tells the plugin where your project specific protofiles are located

       srcDir 'src/main/resources/proto/'


    java {
       srcDir 'src/main/java'


dependencies {

 compile ''