scalagrpclagomakka-grpc

How to externalize protobuf files in JVM ecosystem?


I stumbled upon this Akka grpc tutorial which suggests that we can create a jar from a project that has .proto file under src/main/proto and add it as a dependency in client and server projects to build their respective stubs.

libraryDependencies += "com.example" %% "my-grpc-service" % "1.0.0" % "protobuf-src"

But this doesn't seem to work!! Are there any example projects that demonstrates how this would work in action? How can we externalize protobuf sources and use the same in a jvm based project?


Solution

  • I was able to figure out how to externalise protobuf files properly as per suggestion from akka-grpc docs.

    The problem was that I was not adding sbt-akka-grpc plugin required by sbt to recognise .proto files and include them in the packaged jar. Without this plugin there won't be any .proto file made available in the packaged jar.

    addSbtPlugin("com.lightbend.akka.grpc" % "sbt-akka-grpc" % "1.1.0")
    

    Make sure to add organization settings in your build.sbt to prepare jar correctly.

    organization := "com.iamsmkr"
    

    Also, if you wish to cross-compile this jar to multiple versions add following entries in your build.sbt:

    scalaVersion := "2.13.3"
    crossScalaVersions := Seq(scalaVersion.value, "2.12.14")
    

    and then to publish:

    $ sbt +publishLocal
    

    With appropriate jars published you can now add them as dependencies in your client and server projects like so:

    libraryDependencies += 
    "com.iamsmkr" %% "prime-protobuf" % protobufSourceVersion % "protobuf-src"
    

    You can check out this project I am working on to see this in action.

    Alternate Way

    An alternate way I figured is that you can keep your .proto files in a root directory and then refer them in client and server build.sbt like so:

    PB.protoSources.in(Compile) := Seq(sourceDirectory.value / ".." / ".." / "proto")
    

    Checkout this project to see it in action.