I'm using sbt 0.11 and Scala 2.9.1 (which appears to evaluate REPL lines in the same thread). In my build.sbt I have:
initialCommands in console := """
println(Thread.currentThread)
println(Thread.currentThread.getContextClassLoader)
ru.circumflex.orm.Context.get() // reads my src/main/resources/cx.properties
"""
Context.get() loads resources with:
val bundle = ResourceBundle.getBundle(
"cx", Locale.getDefault, Thread.currentThread.getContextClassLoader)
This results in an error (the REPL appears to buffer up its own output after stdout/stderr):
> 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.interprete开发者_如何学Pythonr.IMain$$anon$2@3a8393ef
Removing the last initialCommand line and running it in the REPL results in no error, since by that time the resource is visible.
Any hints on how to deal with this and make my app's resources accessible to initialCommands?
Discovered the answer soon after posting this. Just prepend this line to initialCommands:
Thread.currentThread.setContextClassLoader(getClass.getClassLoader)
精彩评论