为什么sbt控制台在多模块项目中看不到子项目的包?

时间:2014-06-19 04:56:25

标签: scala sbt

这是我的 project / Build.scala

package sutils

import sbt._
import Keys._

object SutilsBuild extends Build {

  scalaVersion in ThisBuild := "2.10.0"

  val scalazVersion = "7.0.6"

  lazy val sutils = Project(
    id = "sutils",
    base = file(".")
  ).settings(
    test := { },
    publish := { }, // skip publishing for this root project.
    publishLocal := { }
  ).aggregate(
    core
  )

  lazy val core = Project(
    id = "sutils-core",
    base = file("sutils-core")
  ).settings(
    libraryDependencies += "org.scalaz" % "scalaz-core_2.10" % scalazVersion
  )
}

这似乎正在编译我的项目,但是当我进入控制台时,我无法导入任何刚刚编译的代码?!

$ sbt console
scala> import com.github.dcapwell.sutils.validate.Validation._
<console>:7: error: object github is not a member of package com
       import com.github.dcapwell.sutils.validate.Validation._

我在这里做错了什么?试着看看用法,我没有办法说出在控制台中加载哪个子项目

$ sbt about
[info] Loading project definition from /src/sutils/project
[info] Set current project to sutils (in build file:/src/sutils/)
[info] This is sbt 0.13.1
[info] The current project is {file:/src/sutils/}sutils 0.1-SNAPSHOT
[info] The current project is built against Scala 2.10.3
[info] Available Plugins: org.sbtidea.SbtIdeaPlugin
[info] sbt, sbt plugins, and build definitions are using Scala 2.10.3

1 个答案:

答案 0 :(得分:15)

来自@Alexey-Romanov的解决方案是在项目中启动要导入的类的console任务。

sbt sutils/console

然而,另一个解决方案使根sutils依赖于另一个core。使用以下代码段设置项目 - 注意dependsOn core,它会将core项目中的类带到sutils的命名空间。

lazy val sutils = Project(
  id = "sutils",
  base = file(".")
).settings(
  test := { },
  publish := { }, // skip publishing for this root project.
  publishLocal := { }
).aggregate(
  core
).dependsOn core
顺便说一句,你应该在你的用例中使用更简单的build.sbt,如下所示:

scalaVersion in ThisBuild := "2.10.0"

val scalazVersion = "7.0.6"

lazy val sutils = project.in(file(".")).settings(
  test := {},
  publish := {}, // skip publishing for this root project.
  publishLocal := {}
).aggregate(core).dependsOn(core)

lazy val core = Project(
  id = "sutils-core",
  base = file("sutils-core")
).settings(
  libraryDependencies += "org.scalaz" %% "scalaz-core" % scalazVersion
)

当您将构建拆分为两个build.sbt时,您可以更轻松地实现项目。