To automatically generate Scala case classes for your messages add ScalaPB’s
sbt plugin to your project. Create a file named
containing the following line:
addSbtPlugin("com.thesamet" % "sbt-protoc" % "0.99.23") libraryDependencies += "com.thesamet.scalapb" %% "compilerplugin" % "0.9.0"
Add the following line to your
PB.targets in Compile := Seq( scalapb.gen() -> (sourceManaged in Compile).value )
compile command in sbt will both generate Scala sources from
your protos and compile them. If you just want to generate Scala sources for
your protocol buffers without compiling them, run
Additionally, if you need customizations from
scalapb/scalapb.proto or anything from
google/protobuf/*.proto, add the
following to your
libraryDependencies += "com.thesamet.scalapb" %% "scalapb-runtime" % scalapb.compiler.Version.scalapbVersion % "protobuf"
The plugin assumes your
proto files are under
however this is configurable using the
PB.protoSources in Compile setting.
protoc 3.0.0 that is shipped with
If you would like to run a different version of
PB.protocVersion := "-v261"
See all available options in sbt-protoc documentation
Running on Windows
Before sbt-protoc 0.99.15, generating Scala code on Windows required Python 2.x to be installed on your system. If you are using sbt-protoc 0.99.15 or later, then things should just work.
If you are using an older version of sbt-protoc and unable to upgrade, then you must have Python 2.x installed on your system. If Python is not installed on your system, you can download it from here.
If Python.exe can be found in your PATH, then ScalaPB should just work. If not, you can set the location of the Python executable explicitly:
PB.pythonExe := "C:\\Python27\\Python.exe"
To enable Java conversions add the following to your build.sbt:
PB.targets in Compile := Seq( PB.gens.java -> (sourceManaged in Compile).value, scalapb.gen(javaConversions=true) -> (sourceManaged in Compile).value )
Generating gRPC stubs for services is enabled by default. To disable:
PB.targets in Compile := Seq( scalapb.gen(grpc=false) -> (sourceManaged in Compile).value )
Additional options to the generator
scalapb.gen( flatPackage: Boolean = false, javaConversions: Boolean = false, grpc: Boolean = true, singleLineToProtoString: Boolean = false, asciiFormatToString: Boolean = false, lenses: Boolean = true, retainSourceCodeInfo: Boolean = false )
Note that in ScalaPB 0.7,
singleLineToString has been renamed to
flatPackage: When true, ScalaPB will not append the protofile base name
to the package name.
singleLineToProtoString: By default, ScalaPB generates a
that renders the message as a multi-line format (using
Set to true If you would like ScalaPB to generate
toString() methods that use the single line
asciiFormatToString: Setting this to true, overrides
return a standard ASCII representation of the message by calling
lenses: By default, ScalaPB generates lenses for each message for easy
updating. If you are not using this feature and would like to reduce code size
or compilation time, you can set this to
false and lenses will not be
retainSourceCodeInfo: Retain source code information (locations,
comments) provided by protoc in the descriptors. Use the
to get that information from a descriptor.