【问题标题】:Is it possible to transform .properties file in java like app.config in C#是否可以像 C# 中的 app.config 一样在 java 中转换 .properties 文件
【发布时间】:2018-02-16 01:57:45
【问题描述】:

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

我可以在构建之前根据一些debug.template.properties 文件修改这个文件吗?在 C# 中进行 xml 转换?

【问题讨论】:

    标签: java c# config-transformation


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2013-03-23
      • 1970-01-01
      • 2018-06-19
      • 1970-01-01
      • 1970-01-01
      • 2014-04-26
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      相关资源
      最近更新 更多