Scala build script - no projects defined in build

2019-08-10 07:35发布

问题:

I just ried to set up an SBT-Project with a custom Scala-based build script, but I recieve the following error message:

[error] No projects defined in build unit /home/user/scala/myProject

the myProject directory is looking like this:

├── project
│   ├── myProjectBuild.scala
│   └── target
├── src
│   └── main
└── target
    ├── resolution-cache
    ├── scala-2.9.2
    └── streams

and here the content of myProjectBuild.scala:

import sbt._
import sbt.Keys._

object myProjectBuild extends Build {
    val myProjectDependencies = List(
        "org.scalatest" % "scalatest_2.11" % "2.2.1" % "test"
    )

    val myProjectSettings = List(
        name := "myProject",
        version := "1.0",
        scalaVersion := "2.11.5",
        libraryDependencies := myProjectDependencies
    )

    override lazy val settings = super.settings ++ myProjectSettings
}

Any ideas?

回答1:

There is a difference between Bare Build Definition, Multi-Project .sbt definition and Scala Build Definition. About the first one:

Unlike Multi-project .sbt build definition and .scala build definition that explicitly define a Project definition, bare build definition implicitly defines one based on the location of the .sbt file.

Instead of defining Projects, bare .sbt build definition consists of a list of Setting[_] expressions.

The current recommendation is to use Multi-project .sbt build definition.

So, it allows you to not specify any project explicitly, but it's pretty obsolete and not much recommended to use.

P.S. I suppose you're reading some outdated revision of "Learning Scala" (instead of this) or the author uses .sbt-build based example.



回答2:

I found a solution for the problem. I added the following lines to the scala build file:

lazy val root = Project(id = "HardyHeron",
   base = file("."),
   settings = Project.defaultSettings ++ hardyHeronSettings)

After that it nearly worked but I got an MissingRequirementError because of the line

libraryDependencies := myProjectDependencies

After changing it to

libraryDependencies ++= myProjectDependencies

as described here, it compiled properly. I'm not sury why it's working now. The book which I'm referring to (Learning Scala) offers a scala build file without defining the rool val. So should it run without this line anyway?



标签: scala sbt