ScalaPB
This module allows ScalaPB to be used in Mill builds. ScalaPB is a Protocol Buffers compiler plugin that generates Scala case classes, encoders and decoders for protobuf messages.
To declare a module that uses ScalaPB you can extend the mill.contrib.scalapblib.ScalaPBModule trait when defining your module.
This creates a Scala module which compiles .proto files in the protobuf folder of the module with ScalaPB and adds the resulting .scala sources to your module’s generatedSources.
build.mill//| mvnDeps: ["com.lihaoyi::mill-contrib-scalapblib:$MILL_VERSION"]
package build
import contrib.scalapblib._
object example extends ScalaPBModule {
def scalaVersion = "2.12.6"
def scalaPBVersion = "0.7.4"
}
This defines a project with the following layout:
build.mill
example/
src/
protobuf/
resources/
Configuration options
-
scalaPBVersion (mandatory) - The ScalaPB version
Stringe.g."0.7.4" -
scalaPBSources - paths to search for
.protofiles and generate scala case classes for. Defaults tomoduleDir / "protobuf" -
scalaPBFlatPackage - A
Booleanoption which determines whether the.protofile name should be appended as the final segment of the package name in the generated sources. -
scalaPBJavaConversions - A
Booleanoption which determines whether methods for converting between the generated Scala classes and the Protocol Buffers Java API classes should be generated. -
scalaPBGrpc - A
Booleanoption which determines whether grpc stubs should be generated. -
scalaPBSingleLineToProtoString - A
Booleanoption which determines whether the generated.toStringmethods should use a single line format. -
scalaPBProtocPath - A
Option[Path]option which determines the protoc compiler to use. IfNone, a java embedded protoc will be used, if set toSomepath, the given binary is used. -
scalaPBSearchDeps - A
Booleanoption which determines whether to search for.protofiles in dependencies and add them toscalaPBIncludePath. Defaults tofalse. -
scalaPBScala3Sources - Generate the sources with scala 3 syntax (i. e. use
?instead of_in types) -
scalaPBIncludePath - Additional paths to include
.protofiles when compiling.
If you’d like to configure the options that are passed to the ScalaPB compiler directly, you can override the scalaPBOptions task, for example:
build.mill//| mvnDeps: ["com.lihaoyi::mill-contrib-scalapblib:$MILL_VERSION"]
package build
import contrib.scalapblib._
object example extends ScalaPBModule {
def scalaVersion = "2.12.6"
def scalaPBVersion = "0.7.4"
override def scalaPBOptions = "flat_package,java_conversions"
}
If you’d like to pass additional arguments to the ScalaPB compiler directly, you can override the scalaPBAdditionalArgs task, for example:
build.mill//| mvnDeps: ["com.lihaoyi::mill-contrib-scalapblib:$MILL_VERSION"]
package build
import contrib.scalapblib._
object example extends ScalaPBModule {
def scalaVersion = "2.12.6"
def scalaPBVersion = "0.7.4"
override def scalaPBAdditionalArgs =
Seq(s"--zio_out=${Task.dest.toIO.getCanonicalPath}")
}
If you rely on any .proto files from your dependencies that requires case classes to be generated, override the scalaPBSources as follows:
build.mill//| mvnDeps: ["com.lihaoyi::mill-contrib-scalapblib:$MILL_VERSION"]
package build
import contrib.scalapblib._
object example extends ScalaPBModule {
def scalaVersion = "2.12.6"
def scalaPBVersion = "0.7.4"
override def scalaPBSources = {
defaultResolver().classpath(
Seq(ivy"..."),
sources = true
).foreach(jar => os.unzip(jar.path, Task.dest))
super.scalaPBSources() ++ Seq(PathRef(Task.dest))
}
}