更新: sbt-protoc 可以从 maven 下载和提取带有 protos 的 jars。它还可以设置为为那些第三方原型生成 Scala 代码。
这是如何做到的。在你的 build.sbt 中:
import scalapb.compiler.Version.scalapbVersion
val GrpcProtosArtifact = "com.google.api.grpc" % "grpc-google-common-protos" % "1.17.0"
scalaVersion := "2.12.10"
// This sub-project will hold the compiled Scala classes from the external
// jar.
lazy val googleCommonProtos = (project in file("google-common-protos"))
.settings(
name := "google-common-protos",
// Dependencies marked with "protobuf" get extracted to target / protobuf_external
libraryDependencies ++= Seq(
GrpcProtosArtifact % "protobuf"
),
// In addition to the JAR we care about, the protobuf_external directory
// is going to contain protos from Google's standard protos.
// In order to avoid compiling things we don't use, we restrict what's
// compiled to a subdirectory of protobuf_external
PB.protoSources in Compile += target.value / "protobuf_external" / "google" / "type",
PB.targets in Compile := Seq(
scalapb.gen() -> (sourceManaged in Compile).value
)
)
// This sub-project is where your code goes. It contains proto file that imports a proto
// from the external proto jar.
lazy val myProject = (project in file("my-project"))
.settings(
name := "my-project",
// The protos in this sub-project depend on the protobufs in
// GrpcProtosArtifact, so we need to have them extracted here too. This
// time we do not add them to `PB.protoSources` so they do not compile.
libraryDependencies ++= Seq(
GrpcProtosArtifact % "protobuf"
),
PB.targets in Compile := Seq(
scalapb.gen() -> (sourceManaged in Compile).value
),
)
.dependsOn(googleCommonProtos) // brings the compiled Scala classes from googleCommonProtos
完整示例:https ://github.com/thesamet/sbt-protoc/tree/master/examples/google-apis-external-jar
旧答案,已过时:
ScalaPB 不处理第三方依赖项的下载,但让 SBT 为您下载它们并告诉 ScalaPB 构建下载的原型相当容易。
以下示例build.sbt
定义了一个extractProtos
任务,该任务将您链接到的存储库的主分支下载为 github 的 zip 文件并将其解压缩。在做任何事情之前,它会检查目标目录是否不存在,以防止每次编译时一遍又一遍地下载 zip。
由于那里有很多 protos,我们过滤了 zip 文件。源根被提取到target/scala-2.12/resource_managed/googleapis-master
我们添加的位置,PB.protocSources in Compile
因此当调用 protoc 时,它会处理这些文件。
您可以添加更多来源src/main/protobuf
并拥有它们"import "google/rpc/..."
。
scalaVersion := "2.12.2"
libraryDependencies ++= Seq(
"io.grpc" % "grpc-netty" % com.trueaccord.scalapb.compiler.Version.grpcJavaVersion,
"com.trueaccord.scalapb" %% "scalapb-runtime-grpc" % com.trueaccord.scalapb.compiler.Version.scalapbVersion
)
PB.targets in Compile := Seq(
scalapb.gen() -> (sourceManaged in Compile).value
)
PB.generate in Compile := (PB.generate in Compile).dependsOn(extractProtos).value
PB.protoSources in Compile += resourceManaged.value / "googleapis-master"
lazy val extractProtos = Def.task {
if (!(resourceManaged.value / "googleapis-master").exists) {
val zipUrl = "https://github.com/googleapis/googleapis/archive/master.zip"
println(s"Unzipping $zipUrl.")
IO.unzipURL(
from=url(zipUrl),
filter=(
"googleapis-master/google/bigtable/admin/v2/*" |
"googleapis-master/google/api/*" |
"googleapis-master/google/logging/*" |
"googleapis-master/google/longrunning/*" |
"googleapis-master/google/rpc/*" |
"googleapis-master/google/type/*"
),
toDirectory=resourceManaged.value)
}
}
libraryDependencies += "com.trueaccord.scalapb" %% "scalapb-runtime" %
com.trueaccord.scalapb.compiler.Version.scalapbVersion % "protobuf"