如何使 sbt 控制台初始命令可以访问应用程序资源?

发布于 2024-12-07 15:00:46 字数 1338 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

所有深爱都是秘密 2024-12-14 15:00:46

发布此内容后不久就找到了答案。只需将此行添加到initialCommands 之前:

Thread.currentThread.setContextClassLoader(getClass.getClassLoader)

Discovered the answer soon after posting this. Just prepend this line to initialCommands:

Thread.currentThread.setContextClassLoader(getClass.getClassLoader)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文