如何使 sbt 控制台初始命令可以访问应用程序资源?
我正在使用 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)
这会导致错误(REPL 似乎在 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.interpreter.IMain$$anon$2@3a8393ef
删除最后一个初始命令行并在 REPL 结果中运行它没有错误,因为到那时资源是可见的。
关于如何处理这个问题并使我的应用程序的资源可供初始命令访问的任何提示?
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.interpreter.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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发布此内容后不久就找到了答案。只需将此行添加到initialCommands 之前:
Discovered the answer soon after posting this. Just prepend this line to initialCommands: