如何在build.sbt中声明和使用对象?

时间:2015-12-01 23:06:11

标签: scala sbt

我正在尝试理解如何编写一个编写良好的build.sbt文件,并在youtube上学习了一个教程。在该教程中,创建一个类似于下面的对象,但是我写的内容给出了显示的错误。

我做错了什么?

我试图删除导入和对象声明之间的空行而不做任何更改。

import sbt._
import sbt.Keys._

object BuildScript extends Build {
  lazy val commonSettings = Seq(
  organization := "me",
  version := "0.1.0",
  scalaVersion := "2.11.4"
 )
 lazy val root = (project in file(".")).
  settings(commonSettings: _*).
  settings(
    name := "deepLearning",
    libraryDependencies += "org.deeplearning4j" % "deeplearning4j-core" % "0.4-rc3.4"
  )}

error message:
error: illegal start of simple expression
object BuildScript extends Build {
^
[error] Error parsing expression.  Ensure that there are no blank lines    within a setting.

我认为这个帖子实际上解释了它:What is the difference between build.sbt and build.scala?

我觉得克里斯·马丁在这篇文章上所做的编辑是不必要的,但不能拒绝它。

2 个答案:

答案 0 :(得分:4)

我认为你的教程已经过时了。使用最新版本的sbt(0.13+左右)你真的想这样做:

lazy val commonSettings = Seq(
  organization := "me",
  version := "0.1.0",
  scalaVersion := "2.11.4"
)

lazy val root = (project in file(".")).
  settings(commonSettings).
  settings(
    name := "deepLearning",
    libraryDependencies += "org.deeplearning4j" % "deeplearning4j-core" % "0.4-rc3.4"
  )

如果您的项目没有任何子项目,commonSettings val有点多余,您可以内联它:

lazy val root = (project in file(".")).
  settings(
    organization := "me",
    name := "deepLearning",
    version := "0.1.0",

    scalaVersion := "2.11.4",
    libraryDependencies += "org.deeplearning4j" % "deeplearning4j-core" % "0.4-rc3.4"
  )

如果你有子项目,并且有许多常见的设置,你可能想把它们拉到一个自动插件中,但这是一个更高级的概念。

答案 1 :(得分:0)

有两种方法可以解决这个问题:

  1. 执行object BuildScript extends Build { ... }并使用.scala扩展名作为构建文件。我认为这种方式是推荐的长期Sbt构建文件样式。
  2. 将构建定义更改为gregsymons的答案并保留.sbt扩展名。