scalaVersion
設定用於編譯的 Scala 版本。預設情況下,sbt 也會將此版本的 Scala 程式庫新增為依賴。請參閱下一節,瞭解如何停用此自動依賴。如果未指定 Scala 版本,則會使用 sbt 建置時所依據的版本。建議明確指定 Scala 版本。
例如,若要將 Scala 版本設定為「2.11.1」,請執行下列動作:
scalaVersion := "2.11.1"
sbt 預設會新增 Scala 標準程式庫的依賴。若要停用此行為,請將 autoScalaLibrary
設定設為 false。
autoScalaLibrary := false
若要將所有作用域中的 Scala 版本設定為特定值,請使用 ++
命令。例如,若要暫時使用 Scala 2.10.4,請執行
> ++ 2.10.4
使用 Scala 主目錄的路徑定義 scalaHome
設定,將使用該 Scala 安裝。使用本機 Scala 版本時,sbt 仍然需要設定 scalaVersion
。例如:
scalaVersion := "2.10.0-local"
scalaHome := Some(file("/path/to/scala/home/"))
請參閱跨建置。
consoleQuick
動作會擷取依賴,並將它們放在 Scala REPL 的類別路徑上。專案的來源不會編譯,但任何來源依賴的來源都會編譯。若要輸入類別路徑上具有測試依賴但未編譯測試來源的 REPL,請執行 Test/consoleQuick
。這將強制編譯主要來源。
console
動作會擷取依賴並編譯來源,並將它們放在 Scala REPL 的類別路徑上。若要輸入類別路徑上具有測試依賴和已編譯的測試來源的 REPL,請執行 Test/console
。
> consoleProject
如需詳細資訊,請參閱consoleProject 頁面。
設定 console / initialCommands
以設定執行 console
和 consoleQuick
時要評估的初始陳述式。若要個別設定 consoleQuick
,請使用 consoleQuick / initialCommands
。例如:
console / initialCommands := """println("Hello from console")"""
consoleQuick / initialCommands := """println("Hello from consoleQuick")"""
consoleProject
命令由 consoleProject / initialCommands
單獨設定。預設情況下,它不會使用 console / initialCommands
中的值。例如:
consoleProject / initialCommands := """println("Hello from consoleProject")"""
設定 console / cleanupCommands
以設定在結束由 console
和 consoleQuick
啟動的 Scala REPL 後要評估的陳述式。若要個別設定 consoleQuick
,請使用 consoleQuick / cleanupCommands
。例如:
console / cleanupCommands := """println("Bye from console")"""
consoleQuick / cleanupCommands := """println("Bye from consoleQuick")"""
consoleProject
命令由 consoleProject / cleanupCommands
單獨設定。預設情況下,它不會使用 console / cleanupCommands
中的值。例如:
consoleProject / cleanupCommands := """println("Bye from consoleProject")"""
sbt 在與 sbt 本身相同的 JVM 中執行測試,且 Scala 類別與應用程式類別不在相同的類別載入器中。這在 console
中也是如此,當 run
沒有分叉時也是如此。因此,當使用 Scala 直譯器時,請務必正確設定它,以避免出現如下的錯誤訊息:
Failed to initialize compiler: class scala.runtime.VolatileBooleanRef not found.
** Note that as of 2.8 scala does not assume use of the java classpath.
** For the old behavior pass -usejavacp to scala, or if using a Settings
** object programmatically, settings.usejavacp.value = true.
關鍵是使用 *embeddedDefaults* 初始化直譯器的設定。例如:
val settings = new Settings
settings.embeddedDefaults[MyType]
val interpreter = new Interpreter(settings, ...)
這裡,MyType
是一個代表性的類別,應包含在直譯器的類別路徑中及其應用程式類別載入器中。如需更多背景資訊,請參閱導致新增 *embeddedDefaults* 的原始提案。
同樣地,當使用 ILoop 的 break 和 breakIf 方法時,請使用代表性的類別作為型別引數,如下列範例所示:
def x(a: Int, b: Int) = {
import scala.tools.nsc.interpreter.ILoop
ILoop.breakIf[MyType](a != b, "a" -> a, "b" -> b )
}