3

我在理解 scalapb 的语法时遇到了一些困难,特别是我正在尝试为多项目 SBT 构建添加多个 .proto 源目录。

我的项目结构如下:

/build.sbt
/main/src/protobuf
/common/main/src/protobuf
/client/main/src/protobuf

我的 build.sbt 如下:

name := "myApp"

import Dependencies._

import com.trueaccord.scalapb.{ScalaPbPlugin => PB}

val protoDirectories = Seq(
  file("common/src/main/protobuf"),
  file("client/src/main/protobuf")
)

sourceDirectories in PB.protobufConfig ++= protoDirectories

PB.protobufSettings ++ Seq(
  flatPackage := false
)

lazy val common = (project in file("common")).
  settings(Commons.settings: _*).
  settings(libraryDependencies ++= commonDependencies)

lazy val client = (project in file("client")).
  settings(Commons.settings: _*).
  settings(libraryDependencies ++= clientDependencies).
  dependsOn(common)

当我运行时sbt compile,我收到以下错误消息:

[error] Reference to undefined setting: 
[error] 
[error]   sphere/*:sourceDirectories from myApp/*:sourceDirectories (<path_to_project_dir>\build.sbt:11)
[error]      Did you mean myApp/protobuf:sourceDirectories ?

有人可以指出我正确的方向吗?我在这里无法理解一些基本概念......

编辑

好的,所以我指向了 protoDirectories 的错误顺序。我已经修改了 build.sbt 以反映新的变化。我仍然有一个问题,我的 .proto 文件没有在子项目中编译。如果我将 .proto 文件移动到根目录 /main/src/protobuf,它们编译得很好。

4

1 回答 1

2

如果两个项目都包含src/main/protobuf. 此示例还显示了如何设置导入搜索路径。一个完整的例子在https://github.com/thesamet/scalapb-test/tree/multiproject

import com.trueaccord.scalapb.{ScalaPbPlugin => PB}

version in PB.protobufConfig := "3.0.0-beta-2"

lazy val common = (project in file("common")).
  settings(PB.protobufSettings)

lazy val client = (project in file("client")).
  settings(PB.protobufSettings ++ Seq(
    // If you want proto files in client to import proto files in common.
    PB.includePaths in PB.protobufConfig += file("common/src/main/protobuf")
  )).
  dependsOn(common)
于 2016-05-30T05:05:12.887 回答