【发布时间】:2018-02-16 01:57:45
【问题描述】:
我的项目中有带有调试设置的debug.properties 文件。此文件位于 repo 中,每个团队成员都应在调试之前对此文件进行更改。
我可以在构建之前根据一些debug.template.properties 文件修改这个文件吗?在 C# 中进行 xml 转换?
【问题讨论】:
标签: java c# config-transformation
我的项目中有带有调试设置的debug.properties 文件。此文件位于 repo 中,每个团队成员都应在调试之前对此文件进行更改。
我可以在构建之前根据一些debug.template.properties 文件修改这个文件吗?在 C# 中进行 xml 转换?
【问题讨论】:
标签: java c# config-transformation
您可以使用cfg4j 库和以下代码:
private ConfigurationProvider configureProvider() {
ClassLoader classLoader = getClass().getClassLoader();
String envPath = classLoader.getResource("properties").getPath();
Path localPropertiesFilePath = Paths.get(envPath, "local.properties");
Path basePropertiesFilePath = Paths.get(envPath, "base.properties");
if (!Files.exists(localPropertiesFilePath)) {
try {
Files.createFile(localPropertiesFilePath);
}
catch (IOException exception) {
exception.printStackTrace();
}
}
ConfigFilesProvider localConfigFilesProvider =
() -> Collections.singletonList(localPropertiesFilePath);
ConfigFilesProvider baseConfigFilesProvider =
() -> Collections.singletonList(basePropertiesFilePath);
ConfigurationSource local = new FilesConfigurationSource(localConfigFilesProvider);
ConfigurationSource base = new FilesConfigurationSource(baseConfigFilesProvider);
// If you have 'some' property in base and local files - it takes from local
ConfigurationSource mergedSource = new MergeConfigurationSource(base, local);
Environment environment = new ImmutableEnvironment(envPath);
return new ConfigurationProviderBuilder()
.withConfigurationSource(mergedSource)
.withEnvironment(environment)
.build();
}
【讨论】: