java - 是否可以像 C# 中的 app.config 那样在 Java 中转换 .properties 文件

标签 java c# config-transformation

我的项目中有包含调试设置的 debug.properties 文件。这个文件位于 repo 中,每个团队成员在调试之前都应该在这个文件中进行更改。

我可以在构建之前基于某些debug.template.properties 文件修改此文件,例如在 C# 中进行 xml 转换吗?

最佳答案

您可以使用 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();
}

关于java - 是否可以像 C# 中的 app.config 那样在 Java 中转换 .properties 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39346280/

相关文章:

msbuild - 如何通过 Jenkins 中的批处理命令仅触发 web.config 转换

asp.net - 如何在没有源代码管理凭据的情况下转换 web.config 中的 mailSettings

java - Watson Conversation Java SDK 如何从 ExportEntity 到 CreateEntity?

c# - 用 div 值填充 @Html.Hidden

Guava 的 ImmutableListMultimap<String,T> 的 Java 8 收集器

c# - ASP.NET - 单元测试 MembershipProvider

c# - SQLite 的 Entity Framework MigrationSqlGenerator

deployment - Play Framework - 如何维护不同环境的配置文件?

java - volatile 关键字有什么用?

java - 如何检查数组的所有成员