Scala Build失败了吗?

时间:2016-09-16 08:16:39

标签: scala apache-spark

当我尝试执行sbt更新时,我是scala的新手

[error] /home/workspace/Scala/SparkTwitterStreaming/project/project/build.scala:1: expected class or object definition
[error] name := "SparkTwitterStreaming"
[error] ^
[error] /home/workspace/Scala/SparkTwitterStreaming/project/project/build.scala:2: expected class or object definition
[error] version := "1.0"
[error] ^
[error] /home/workspace/Scala/SparkTwitterStreaming/project/project/build.scala:3: expected class or object definition
[error] scalaVersion := "2.11.6"
[error] ^
[error] /home/workspace/Scala/SparkTwitterStreaming/project/project/build.scala:5: expected class or object definition
[error] libraryDependencies += "org.apache.spark" % "spark-core_2.11" % "1.6.2"
[error] ^
[error] /home/workspace/Scala/SparkTwitterStreaming/project/project/build.scala:6: expected class or object definition
[error] libraryDependencies += "com.typesafe" % "config" % "1.3.0"
[error] ^
[error] 5 errors found

build.scala / build.sbt ??

name := "SparkTwitterStreaming"

version := "1.0"

scalaVersion := "2.11.6"

libraryDependencies += "org.apache.spark" % "spark-core_2.11" % "1.6.2"

libraryDependencies += "com.typesafe" % "config" % "1.3.0"

2 个答案:

答案 0 :(得分:2)

所以基本上...一个有效的最小sbt项目有一个像下面这样的结构,

YourProject
├── build.sbt
└── src
    └── main
        └── scala
            └── Main.scala

更详细的项目将是这样的,

SparkTwitterStreaming
├── build.sbt
├── project
│   ├── build.properties
│   └── plugins.sbt
└── src
    └── main
        └── scala
            └── YouCode.scala

最小build.sbt看起来像,

name := "SparkTwitterStreaming"

version := "1.0"

scalaVersion := "2.11.6"

libraryDependencies += "org.apache.spark" % "spark-core_2.11" % "1.6.2"

libraryDependencies += "com.typesafe" % "config" % "1.3.0"

现在有些人喜欢使用build.scala代替build.sbt,在这种情况下,此build.scala应位于SparkTwitterStreaming/project目录内。

SparkTwitterStreaming
├── project
│   ├── build.properties
│   ├── plugins.sbt
│   └── build.scala
└── src
    └── main
        └── scala
            └── YouCode.scala

此外,对于上述build.sbt,等效的build.scala将会是,

import sbt._
import Keys._

object MyBuild extends Build {   // <--- here MyBuild can be renamed to anything
  // if you needed extra resolvers
  val pResolvers = Seq(
    "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"
  )

  val pDependencies = Seq(
    "org.apache.spark" % "spark-core_2.11" % "1.6.2",
    "com.typesafe" % "config" % "1.3.0"
  )

  val pBuildSettings = Seq (
    name := "SparkTwitterStreaming",
    version := "1.0",
    scalaVersion := "2.11.6",
    resolvers ++= pResolvers,
    libraryDependencies ++= pDependencies
  )

  lazy val root = project.in(file(".")).settings(pBuildSettings)
}

答案 1 :(得分:1)

看起来您混淆了.scala.sbt个文件。 SBT允许以两种不同的方式定义构建。 .scala文件是正常的Scala文件,因此它们不能在顶层具有代码(并且在SBT中只有name := "SparkTwitterStreaming"等只是Scala代码),它必须在特征/类中/宾语。 SBT本身负责以这种方式包装.sbt文件。

SparkTwitterStreaming/project/project/build.scala也看起来不对:当您在project/project下使用文件时,SBT中存在 个案,但这不是其中之一。您可能希望将此文件移至SparkTwitterStreaming/build.sbt