包括来自$ {HOME} /.app / someconfig.conf的配置

时间:2013-09-29 03:58:23

标签: config typesafe-stack typesafe

我需要在我的typesafe配置中包含一些属性文件,比如

include ${user.HOME}"/.app/db-test.conf"

然而解析器抱怨:

com.typesafe.config.ConfigException$Parse: dev/application.conf: 47: include keyword is not followed by a quoted string, but by: '${'user.HOME'}'
com.typesafe.config.ConfigException$Parse: dev/application.conf: 47: include keyword is not followed by a quoted string, but by: '${'user.HOME'}'
    at com.typesafe.config.impl.Parser$ParseContext.parseError(Parser.java:329)
    at com.typesafe.config.impl.Parser$ParseContext.parseError(Parser.java:325)
    at com.typesafe.config.impl.Parser$ParseContext.parseInclude(Parser.java:574)
    at com.typesafe.config.impl.Parser$ParseContext.parseObject(Parser.java:624)
    at com.typesafe.config.impl.Parser$ParseContext.parseValue(Parser.java:408)
    at com.typesafe.config.impl.Parser$ParseContext.parseObject(Parser.java:657)
    at com.typesafe.config.impl.Parser$ParseContext.parse(Parser.java:832)
    at com.typesafe.config.impl.Parser.parse(Parser.java:34)

如何在包含语句中使用系统属性/环境变量?

2 个答案:

答案 0 :(得分:1)

您可以在加载配置的代码中手动执行此操作吗?

Config baseConfig = ConfigFactory.load();
// Probably want error checking here.
Config testConfig = ConfigFactory.parseFile(
    new File(System.getenv("HOME") + "/.app/db-test.conf"));
// You may need to change your resolution order, depending on what you're doing in your 
// default config.
testConfig.resolve();
Config finalConfig = baseConfig.withFallback(testConfig);

答案 1 :(得分:0)

目前无法开箱即用(请参阅https://github.com/typesafehub/config/issues/122)。

但是,如果您使用可以修改的代码配置应用程序,则可以编写自定义ConfigIncluder。见http://typesafehub.github.io/config/latest/api/com/typesafe/config/ConfigIncluder.html

您还可以使用标准Java机制向Java添加URL方案(请参阅Creating custom URI scheme using URI class) 然后使用include url("myscheme:whatever")