有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

是否有可能转变。类java应用程序中的属性文件。在C中配置#

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

在C#中进行类似构建的xml转换之前,是否可以基于某个debug.template.properties文件修改此文件


共 (1) 个答案

  1. # 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();
    }