如何使sbt控制台initialCommands可以访问应用程序资源?

时间:2011-09-30 21:36:08

标签: scala sbt read-eval-print-loop

我正在使用sbt 0.11和Scala 2.9.1(它似乎在同一个线程中评估REPL行)。在我的build.sbt中,我有:

initialCommands in console := """
println(Thread.currentThread)
println(Thread.currentThread.getContextClassLoader)
ru.circumflex.orm.Context.get() // reads my src/main/resources/cx.properties
"""

Context.get()使用以下命令加载资源:

val bundle = ResourceBundle.getBundle(
  "cx", Locale.getDefault, Thread.currentThread.getContextClassLoader)

这会导致错误(在stdout / stderr之后,REPL似乎缓冲了自己的输出):

> console
[info] No CoffeeScripts to compile
[info] Starting scala interpreter...
[info] 
Thread[run-main,5,trap.exit]
sun.misc.Launcher$AppClassLoader@12360be0
14:17:44.003 [run-main] ERROR ru.circumflex.core - Could not read configuration parameters from cx.properties.
res0: ru.circumflex.core.Context = ctx()
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_26).
Type in expressions to have them evaluated.
Type :help for more information.

scala> Thread.currentThread
res1: java.lang.Thread = Thread[run-main,5,trap.exit]

scala> Thread.currentThread.getContextClassLoader
res2: java.lang.ClassLoader = scala.tools.nsc.interpreter.IMain$$anon$2@3a8393ef

删除最后一个initialCommand行并在REPL中运行它会导致没有错误,因为到那时资源是可见的。

有关如何处理此问题以及使我的应用程序资源可供初始命令访问的任何提示吗?

1 个答案:

答案 0 :(得分:1)

发布后很快发现了答案。只需将此行添加到initialCommands:

Thread.currentThread.setContextClassLoader(getClass.getClassLoader)