I have a build.sbt for a couple of packages in the same project. When I do sbt compile, my protobuf files are under [packagename]/src/main/protobuf/*, so it won't scan these files since they have that additional package name in the path. How to change the default protobuf file path?
CodePudding user response:
I recommend using AkkaGrpcPlugin
and a multi-project SBT configuration.
Put the .proto
files in a separate project and make the others depend on it. This project just has a single directory <root>/grpc/src/main/protobuf
containing the .proto
files. When this project is compiled it will create all the stub files which can be picked up by other projects that depend on it.
Here is an outline build.sbt
file:
lazy val top_level =
(project in file("."))
.aggregate(grpc, main)
lazy val grpc =
project
.in(file("grpc"))
.settings(
???
)
.enablePlugins(AkkaGrpcPlugin)
lazy val main =
project
.in(file("main"))
.settings(
???
)
.dependsOn(grpc)
And add this to plugins.sbt
:
addSbtPlugin("com.lightbend.akka.grpc" % "sbt-akka-grpc" % "1.1.1")
CodePudding user response:
It is unclear from your question whether your sbt build is a single or multi-project build. You can tell that it is a multi-project build if you see lines like val someSubProject = project.in(file(...)
.
If your build.sbt
is a single project build, you can add a line to customize where protos are being scanned:
Compile / PB.protoSources = file("pkgname/src/main/protobuf")
If you have a multi-project build with a project proj1
, then it should already work in the way you expect:
val proj1 = project.in(file("proj1"))
.settings(
PB.targets in Compile := Seq(
scalapb.gen(javaConversions=true) -> (sourceManaged in Compile).value / "scalapb"
),
libraryDependencies = Seq(
"com.thesamet.scalapb" %% "scalapb-runtime" % scalapb.compiler.Version.scalapbVersion % "protobuf",
)
)
then sbt-protoc will generate sources for the protos under proj1/src/main/protobuf
. The sources will get compiled as poart of the proj1
project.
You can customize the path it looks for by setting Compile / PB.protoSources
within that project's setting. For example, if you want it to generate source for protos that are in another top-level directory, you can do:
val proj1 = project.in(file("proj1"))
.settings(
PB.targets in Compile := Seq(
scalapb.gen(javaConversions=true) -> (sourceManaged in Compile).value / "scalapb"
),
Compile / PB.protoSources :=
Seq((ThisBuild / baseDirectory).value / "somewhere" / "protos"),
libraryDependencies = Seq(
"com.thesamet.scalapb" %% "scalapb-runtime" % scalapb.compiler.Version.scalapbVersion % "protobuf",
)
)