尝试编译SBT项目时,ScalaPB无法正常工作。编译失败,并显示“对象gen不是软件包scalapb的成员”

时间:2019-03-28 16:54:30

标签: scala sbt protocol-buffers scalapb

我想在我的SBT项目中使用ScalaPB插件。但是,当我尝试编译项目时,出现一个错误,指出“对象gen不是软件包scalapb的成员”。那么如何配置此插件以与我的项目一起使用?

我按照Github页面上的说明进行操作,但没有用。

我的项目具有以下结构,换句话说就是标准的Maven项目结构:

.
├── build.sbt
├── ci
│   └── checkstyle
├── LICENSE
├── project
│   ├── build.properties
│   ├── Dependencies.scala
│   ├── plugins.sbt
│   ├── project
│   └── target
├── src
│   ├── main
│   └── test
└── version.sbt

这是Dependencies.scala文件:

import sbt._

object Dependencies {

  lazy val scalatestVersion = "3.0.5"
  lazy val scalamockVersion = "4.1.0"
  lazy val scalaPbcVersion = "0.8.3"

  // Libraries for Protobuf
  val scalaPbc = "com.thesamet.scalapb" %% "compilerplugin" % scalaPbcVersion

  // Libraries for Testing
  val scalatest = "org.scalatest" %% "scalatest" % scalatestVersion % Test
  val scalamock = "org.scalamock" %% "scalamock" % scalamockVersion % Test

  // Projects
  val groupBackendDependencies = Seq(
    scalatest, scalamock, scalaPbc)
}

这是plugins.sbt文件:

// The Typesafe repository
resolvers += "Typesafe repository" at "https://repo.typesafe.com/typesafe/maven-releases/"

// for autoplugins
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.6" withSources())

dependencyOverrides += "com.puppycrawl.tools" % "checkstyle" % "8.12"

// Scala checkstyle
addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "1.0.0")

// Scala Protobuf
addSbtPlugin("com.thesamet" % "sbt-protoc" % "0.99.19")

这是build.sbt文件。这是发生错误的地方。


/** ****************************************************************************
  * <REDACTED>
  * ****************************************************************************
  */
enablePlugins(UniversalPlugin)

/** ****************************************************************************
  * Application related configurations
  * ****************************************************************************
  */
organization := "<REDACTED>"
name := "<REDACTED>"
packageName := "<REDACTED>"

/** ****************************************************************************
  * Compilation related
  * ****************************************************************************
  */

scalaVersion := "2.12.7"
scalacOptions ++= Seq("-target:jvm-1.8",
  "-unchecked",
  "-deprecation",
  "-encoding", "utf8",
  "-feature",
  "-Ywarn-adapted-args",
  "-Ywarn-dead-code")

javacOptions in(Compile, compile) ++= Seq("-source", "11",
  "-target", "11",
  "-g:lines")

logLevel := sbt.Level.Warn
exportJars := true
libraryDependencies ++= Dependencies.groupBackendDependencies

/** ****************************************************************************
  * Packaging related configurations
  * ****************************************************************************
  */
packageName in Universal := s"${packageName.value}-${version.value}"
exportJars := true

//By default, the dist task will include the API documentation in the generated package.
//Below instruction will exclude them/
sources in(Compile, doc) := Seq.empty
publishArtifact in(Compile, packageDoc) := false

/** ****************************************************************************
  * CI : Scala Checkstyle
  * Ref: http://www.scalastyle.org/sbt.html
  * Usage: sbt scalastyle
  * ****************************************************************************
  */
lazy val scalaCheckstyle = "ci/checkstyle/scala/scalastyle-config.xml"
scalastyleConfig := baseDirectory(_ / scalaCheckstyle).value
scalastyleFailOnWarning := true

/** ****************************************************************************
  * CI : Pipeline Simulation
  * Usage: sbt pipeline-ci
  * ****************************************************************************
  */
commands += Command.command("pipeline-ci") { state =>
  "clean" ::
    "compile" ::
    "test" ::
    state
}

/**
 * The error occurs here.
 */

PB.targets in Compile := Seq(
  scalapb.gen() -> (sourceManaged in Compile).value
)

我希望这个项目在运行时能够编译出所有错误

sbt clean compile

但是我得到了这个堆栈跟踪

/home/my-project/build.sbt:73: error: object gen is not a member of package scalapb
  scalapb.gen() -> (sourceManaged in Compile).value
          ^
[error] Type error in expression

1 个答案:

答案 0 :(得分:1)

您包括ScalaPB的编译器插件(“ com.thesamet.scalapb” %%“ compilerplugin”%scalaPbcVersion)作为您项目的库依赖项。编译器插件必须是您的构建项目的依赖项。为此,需要将其作为库依赖项添加到您的project/plugins.sbt中。