【问题标题】:how to declare and use object in this build.sbt?如何在这个 build.sbt 中声明和使用对象?
【发布时间】:2016-03-06 01:20:20
【问题描述】:

我正在尝试了解如何编写编写良好的 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?

我觉得 chris martin 对这篇文章的编辑是不必要的,但不能拒绝。

【问题讨论】:

    标签: scala sbt


    【解决方案1】:

    我认为你的教程已经过时了。使用最新版本的 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"
      )
    

    如果您确实有子项目,并且有很多常用设置,您可能希望将它们提取到自动插件中,但这是一个更高级的概念。

    【讨论】:

    • 我是sbt的维护者,我支持这个消息。
    • @eugene。到目前为止,sbt 似乎是一个了不起的工具。
    【解决方案2】:

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

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

    【讨论】:

    • 我相信完全有可能在 scala 构建工具的项目根目录处有一个 (build).sbt 结尾。我认为另一个堆栈溢出线程很好地解释了事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多