Configuration depending on launch mode

后端 未结 3 577
忘掉有多难
忘掉有多难 2021-02-04 09:22

Play can be launched in dev mode (via run), in production mode (via start) or in test mode. Is there a way to provide a different config file (co

3条回答
  •  南方客
    南方客 (楼主)
    2021-02-04 10:10

    This thing can be done by having loading config files based on the environment which can be supplied via -Dmode=staging/dev/prod, and for loading the files I override onLoadConfig of GlobalSettings in Global.java.

    Java snippet-

     @Override
     public Configuration onLoadConfig(Configuration config, File file,ClassLoader classLoader) {
     Configuration updatedConfig = config;
     String mode = config.getString("mode");
     if (StringUtils.isNotEmpty(mode)) {
       try {
         File modeFolder = FileUtils.getFile(file, "conf/" + mode);
         if (modeFolder.exists()) {
           play.api.Configuration modeConfig = config.getWrappedConfiguration();
           IOFileFilter fileFilter = new WildcardFileFilter("*.conf");
           Collection fileList = FileUtils.listFiles(modeFolder, fileFilter, null);
           for (File confFile : fileList) {
             modeConfig = modeConfig
                .$plus$plus(new play.api.Configuration(ConfigFactory.parseFile(confFile)));
    
          }
           updatedConfig = new Configuration(modeConfig);
         }
       } catch (Exception e) {
         Logger.error("Exception while loading configuration for mode : " + mode, e);
       }
     } else {
       Logger.error("Please provide mode in which play application has to start (Ex. play -Dmode=) ");
     }
    

    For each mode, create a folder(name same as environment) and keep environment specific config in that folder.

提交回复
热议问题