简体   繁体   中英

Why does sbt console not see packages from subproject in multi-module project?

This is my 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
  )
}

This seems to be compiling my project just fine, but when I go into the console, I can't import any of the code that just got compiled?!

$ 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._

What am I doing wrong here? Trying to look at the usage, I don't see a way to say which subproject to load while in the console

$ 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

There's the solution from @Alexey-Romanov to start the console task in the project the classes to import are in.

sbt sutils/console

There's however another solution that makes the root sutils project depend on the other core . Use the following snippet to set up the project - note dependsOn core that will bring the classes from the core project to sutils 's namespace.

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

BTW, you should really use a simpler build.sbt for your use case as follows:

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
)

You could make it even easier when you'd split the build to two build.sbt s, each for the projects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM