ScalaPB: Scala Protocol Buffer Compiler

ScalaPB is a protocol buffer compiler (protoc) plugin for Scala. It will generate Scala case classes, parsers and serializers for your protocol buffers.

ScalaPB is hosted on Github.

Main features

  • Built on top of Google’s protocol buffer compiler to ensure perfect compatibility with the language specification.

  • Supports both proto2 and proto3.

  • Nested updates are easy by using lenses:
    val newOrder = order.update(_.creditCard.expirationYear := 2015)
  • Generated case classes can co-exist alongside the Java-generated code (the class names will not clash). This allows gradual transition from Java to Scala.

  • Can optionally generate conversion methods between the Java generated version of Protocol Buffers to the Scala generated version. This makes it possible to migrate your project gradually.

  • New: Supports for Oneof’s that were introduced in Protocol Buffers 2.6.0.

  • Newer: Supports Scala.js (in 0.5.x).

  • Newer Supports gRPC (in 0.5.x).

  • Newest: Supports SparkSQL (in 0.5.23).

  • Newest: Supports converting to and from JSON (in 0.5.x).

  • Newest: Supports User-defined options (in 0.5.29).

To automatically generate Scala case classes for your messages add ScalaPB’s sbt plugin to your project. Create a file named project/scalapb.sbt containing the following line:

addSbtPlugin("com.thesamet" % "sbt-protoc" % "0.99.28")

libraryDependencies += "com.thesamet.scalapb" %% "compilerplugin" % "0.10.1"

Add the following line to your build.sbt:

PB.targets in Compile := Seq(
  scalapb.gen() -> (sourceManaged in Compile).value / "scalapb"

// (optional) If you need scalapb/scalapb.proto or anything from
// google/protobuf/*.proto
libraryDependencies += "com.thesamet.scalapb" %% "scalapb-runtime" % scalapb.compiler.Version.scalapbVersion % "protobuf"

ScalaPB looks for protocol buffer files in src/main/protobuf, but this can be customized. Running the compile command in sbt will both generate Scala sources from your protos and compile them.

For additional configuration options, see ScalaPB SBT Settings.

Running Standalone Using scalapbc

If you would like to compile protocol buffers into Scala outside SBT, you can use scalapbc (ScalaPB compiler).

See ScalaPBC.

Running from Maven

Using ScalaPBC, you can get maven to generate the code for you. Check out the ScalaPB Maven example.

Questions? Comments?

Feel free to post to our mailing list. Found a bug? Missing a feature? Report on Github.


Read about the Generated Code.