不能在 sbt 构建定义代码中使用导入

Can't use imports inside sbt build definition code

我尝试在 sbt 中导入 JSON 库,以便我的自定义 sbt 任务可以使用 JSON api 编写 json 文件。然而,sbt 似乎无法导入这些库,而只能导入 "standard" 库,如 scala.io.Source、java.io.File 等...

下面注释掉的两行都会失败 sbt:

libraryDependencies += "com.typesafe.play" %% "play-json" % "2.3.7"

libraryDependencies += "io.argonaut" %% "argonaut" % "6.0.4"    

compile in Compile <<= (compile in Compile) map { c => 
  import scala.io.Source
  //import play.api.libs.json.Json
  //import argonaut._, Argonaut._

可能是什么?我必须写一个插件来规避这个吗?

$ about
[info] This is sbt 0.13.6
[info] The current project is built against Scala 2.11.6
[info] Available Plugins: ...
[info] sbt, sbt plugins, and build definitions are using Scala 2.10.4

当然,我可以徒手对 json 进行字符串插值,但我想知道它可能是什么...

谢谢!

根据 plugin documentation 中的这个片段,您只需要在 plugins.sbt 中包含依赖项而不是您的构建定义(即,不需要插件)。

// [within plugins.sbt]
// plain library (not an sbt plugin) for use in the build definition
libraryDependencies += "org.example" % "utilities" % "1.3"

所以你应该可以将这些添加到 plugins.sbt:

libraryDependencies += "com.typesafe.play" %% "play-json" % "2.3.7"

libraryDependencies += "io.argonaut" %% "argonaut" % "6.0.4"