ScalaPBC: ScalaPB's standalone compiler
ScalaPBC is a tool that lets you generate Scala sources from the command line (or from a maven build).
#
InstallationYou can download the current release here: scalapbc-0.11.11.zip.
Older versions can be found in the releases page.
Unzip the file, and inside you will find two scripts: bin/scalapbc
(and
bin/scalapbc.bat
) that can be used on Unix/Linux/Mac OS X (and Windows,
respectively).
#
UsageScalaPBC is used exactly like protoc. In fact, scalapbc calls protoc.
On the first invocation, the protoc binary will be automatically
fetched via Coursier and stored for further invocations in a
local cache,
which can be customized by setting the PROTOC_CACHE
environment variable.
You can pass a custom protoc version as the first command line argument:
To generate Scala code, invoke ScalaPBC like this:
To generate both Scala code and Java code along with Java conversions:
The generated code depends on
scalapb-runtime
to compile. You will To get the code to work, add a dependency on [https://mvnrepository.com/artifact/com.thesamet.scalapb/scalapb-runtime] to your project. The version ofscalapb-runtime
needs to match or be newer than the version of the plugin.
#
Passing generator parametersIf you would like to pass additional options, like java_conversions
,
flat_package
, or single_line_to_proto_string
, it can be done like this:
where OPT1,OPT2 is a comma-separated list of options, followed by a colon
(:
) and then the output directory. For example:
The supported parameters are: flat_package
, java_conversions
, grpc
and single_line_to_proto_string
, no_lenses
, retain_source_code_info
.
Those parameters are described in SBT settings
#
Loading additional generators from MavenScalaPBC (starting version 0.10.1) can fetch generators from Maven using Coursier:
If you use zio-grpc, you can use the following command to generate services that use ZIO. This also generates ScalaPB case classes for messages and the GRPC descriptors that the generated ZIO code depends on.
bin/scalapbc --plugin-artifact=io.grpc:grpc-java:
#
Using ScalaPB as a proper protoc pluginYou may want to use ScalaPB code generator as a standard protoc plugin (rather than using scalapbc as a wrapper or through SBT).
For Linux and Mac OS X, you can download a native executable version of the plugin for Scala from our release page:
- For Linux: protoc-gen-scala-0.11.11-linux-x86_64.zip
- For OS X: protoc-gen-scala-0.11.11-osx-x86_64.zip
Those zip files contain native executables of the plugin for the respective operating system built using GraalVM. If you are using another operating system, or prefer to use a JVM based plugin implementation, you will find executable scripts for Windows and Unix-like operating systems in maven. These scripts require a JVM to run. The JVM needs to be available on the path, or through the JAVA_HOME
environment variable.
To generate code:
On Windows:
For passing parameters to the plugin, see the section above.
Note that the standalone plugin provided in
scalapbc
needs to be able to find a JVM in the path or throughJAVA_HOME
environment variable. If you encounter unexpected errors, try to execute the plugin directly from the command line, and the output printed may be useful for further debugging.
The generated code depends on
scalapb-runtime
to compile. To get the code to work, add a dependency on scalapb-runtime to your project. The version ofscalapb-runtime
needs to match or be newer than the version of the plugin.