使用 Typesafe Config 的 ConfigFactory 在 build.sbt 中设置关键设置? [英] Using Typesafe Config's ConfigFactory to set key setting in build.sbt?

查看:72
本文介绍了使用 Typesafe Config 的 ConfigFactory 在 build.sbt 中设置关键设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sbt.version=0.13.1

sbt.version=0.13.1

build.sbt 中,我通过调用我的项目依赖项的一段代码来分配设置键,该代码又通过 Typesafe Config 的 ConfigFactory 配置自身.我的依赖项在 jar 的根目录中有一个 reference.conf,而我的项目本身在 src/main/resources 中包含一个覆盖的 application.conf>.

In build.sbt I am assigning a setting key by calling a piece of my project dependency's code that in turn configures itself via Typesafe Config's ConfigFactory. My dependency has a reference.conf in the root of the jar, and my project itself contains an overriding application.conf in src/main/resources.

lib/dependency 也是我的代码,顺便说一句.

The lib/dependency is also my code, btw.

import com.mylib.Finders
import com.myproj.sbt.Keys._

projKeyColorSetting in Compile := Finders.findColor // this calls ConfigFactory.load

seq(projSettings:_*)

构建甚至没有加载,因为它找不到我试图在我的 lib 代码中引用的第一个 conf 键.

The build doesn't even load because it can't find the first conf key I attempt to reference in my lib code.

我在构建文件中尝试了多种范围界定和类路径操作的组合,但都无济于事.我假设 jar 的 reference.conf 应该在 Compile 范围的类路径上,但它没有按我预期的那样工作.

I've tried a number of combinations of scoping and Classpath manipulation in my build file, but to no avail. I assumed that the jar's reference.conf would have been on the Compile scope's classpath but it doesn't work as I expect.

昨天我花了大部分时间研究关于类路径、范围、键、任务和资源生成器的 SBT 文档 - 我的目的是执行一个依赖于 中的 projKeyColorSetting 设置的自定义插件build.sbt 如下:

I spent the majority of yesterday poring over SBT documentation on Classpath, Scopes, Keys, Tasks, and ResourceGenerators - my intention is to execute a custom plugin that relies on the projKeyColorSetting setting in build.sbt as follows:

lazy val projSettings = inConfig(Compile) {
    Seq(
        resourceGenerators in Compile <+= Def.task {
            val fileCreated = createColorFile(projKeyColorSetting.value)
            Seq(fileCreated)
        }
    )
 }

推荐答案

如果你从 foo.jar 获取一个类,那么 ConfigFactory.load() 应该在同一个罐子.如果不是,则说明有问题,但很难猜测是什么.可能是 reference.conf 中有一些无效的语法;可能是 reference.conf 不在 jar 中;可能是 reference.conf 位于子目录中而不是 jar 的根目录中;很难猜.我会尝试 -Dconfig.trace=loads 来查找那里的问题(例如,它应该告诉你配置是否尝试加载 reference.conf).你也可以做你自己的 classLoader.getResources 看看你是否可以在不涉及配置的情况下找到文件.您也可以尝试 ConfigFactory.parseResourcesAnySyntax("reference") 并查看您的参考设置是否在其中,然后尝试直接调用 ConfigFactory.load 并查看您的设置是否在那里.一般来说,请仔细检查所有假设,看看哪里出了问题.

If you are getting a class from foo.jar, then ConfigFactory.load() should get a reference.conf found in the same jar. If it doesn't, then something is wrong but it's hard to guess what. It could be that reference.conf has some invalid syntax in it possibly; it could be that reference.conf isn't in the jar; it could be that reference.conf is in a subdirectory instead of root of the jar; hard to guess. I'd try -Dconfig.trace=loads to look for problems in there (it should tell you whether config tries to load the reference.conf for example). You could also do your own classLoader.getResources and see if you can find the file without config involved. You could also try ConfigFactory.parseResourcesAnySyntax("reference") and see if your reference settings are in there, and try calling ConfigFactory.load directly and see if your settings are in there. Just in general, double-check all assumptions and see where it goes wrong.

至于如何添加 src/main/resources,两个基本策略是 1) 以某种方式将它放在类路径上(在这种情况下这可能很困难;您甚至在启动 sbt 之前就需要它,或者需要做一些自定义类加载器的乐趣)或者可能更实用 2)使用 ConfigFactory.parseFile() 手动加载它.

As for how to add src/main/resources, the two basic strategies would be 1) to get it on the classpath somehow (which is probably difficult in this case; you would need it before even launching sbt or would need to do some kind of custom ClassLoader fun) or probably more practical 2) load it manually with ConfigFactory.parseFile().

我可能会获取 resourceDirectory 键作为您任务的依赖项,然后执行类似(未经测试)的操作:

I would probably grab the resourceDirectory key as a dependency of your task and then do something like (untested):

myTask := {
   val resourceDir = (resourceDirectory in Compile).value
   val appConfig = ConfigFactory.parseFile(resourceDir / "application.conf")
   val config = ConfigFactory.load(appConfig) // puts reference.conf underneath
   Finders.findColor(config)
}

请注意,这涉及更改 findColor 以采用 Config 参数,或者您可能希望将 Finders 设为可以使用 Config 构造的非单例;请参阅 https://github.com/typesafehub/config/blob/master/examples/scala/simple-lib/src/main/scala/simplelib/SimpleLib.scala#L22 我试图说明的地方当使用 Config 时,通常一个库应该默认为 ConfigFactory.load 但也有一个构造函数允许自定义 Config 用于这种情况.

Note that this involves changing findColor to take a Config parameter, or maybe you would prefer to make Finders a non-singleton that can be constructed with a Config; see the example at https://github.com/typesafehub/config/blob/master/examples/scala/simple-lib/src/main/scala/simplelib/SimpleLib.scala#L22 where I was trying to illustrate that when using a Config usually a library should both default to ConfigFactory.load but also have a constructor that allows a custom Config for situations like this.

这篇关于使用 Typesafe Config 的 ConfigFactory 在 build.sbt 中设置关键设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆