aka*_*ppi 2 protocol-buffers scalapb
我如何告诉ScalaPB它应该.proto从Internet上获取依赖项,例如
google/api/annotations.proto来自https://github.com/googleapis/googleapis/tree/master/google/api
背景:
目的是通过gRPC从Scala中读取etcd v3 API。
我.proto从他们的项目中选择了etcd的特定文件,并放置在我的文件下面。有用。但是,依赖关系开始深入运行,必须有更好的方法。
https://github.com/googleapis/googleapis/tree/master/google/api
更新: sbt-protoc可以从maven下载和提取带有protos的jar。也可以将其设置为为那些第三方原型生成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
Run Code Online (Sandbox Code Playgroud)
此处的完整示例:https : //github.com/thesamet/sbt-protoc/tree/master/examples/google-apis-external-jar
旧答案,已过时:
ScalaPB不处理第三方依赖项的下载,但是让SBT为您下载它们并告诉ScalaPB构建下载的原型很容易。
以下示例build.sbt定义了一个extractProtos任务,该任务将从github下载您链接到的回购的master分支作为zip文件并将其提取。在执行任何操作之前,它将检查目标目录是否不存在,以防止每次编译时都反复下载zip。
由于其中包含许多原型,因此我们将过滤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"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
986 次 |
| 最近记录: |