简体   繁体   English

Java Standalone App中的Spring-boot application.yaml

[英]Spring-boot application.yaml in Java Standalone App

I was wondering if is there a way to use a file like spring-boot application.yaml from a Java Standalone App, I am searching for similar approach to pun on it environment properties (for example endpoints URLs dev, qa, production) 我想知道是否有办法使用Java Standalone App中的spring-boot application.yaml这样的文件,我正在寻找类似的方法来对其环境属性进行双打(例如,端点URLs dev,qa,production)。

I'm using gradle as build tool 我正在使用gradle作为构建工具

I've solved it by myself :) The solution was creating my own task on gradle. 我自己解决了问题:)解决方案是在gradle上创建我自己的任务。 Here the steps that I follow: 这里是我遵循的步骤:

  1. Inside src/main/resources I created as many files as I need: 在src / main / resources内部,我根据需要创建了多个文件:

    • config-dev.yaml config-dev.yaml
    • config-qa.yaml config-qa.yaml
    • config-prod.yaml config-prod.yaml
  2. I added a Gradle tasks to choose the corresponding file: 我添加了Gradle任务以选择相应的文件:

     task loadConfigFile(type: Copy) { def environments = ['dev', 'preprod', 'prod'] def environment = 'dev'; if (project.hasProperty('env')) { def validParam = environments.contains(env) if (validParam) { environment = env } } def configFile = "configuration-${environment}.yaml" from('src/main/resources/') into('src/main/resources/') include(configFile) rename(configFile, 'application.yaml') 

    } }

  3. Before to build the project I called the function using doFirst: 在构建项目之前,我使用doFirst调用了该函数:

     build.doFirst { loadConfigFile() } 
  4. For finish I should call the build task using a custom variable named env: 最后,我应该使用一个名为env的自定义变量来调用构建任务:

     ../gradlew build -Penv=preproda 

As a result in the src/main/resources I have a configuration file named application.yaml and using snakeyaml I read the config entries, here is demo about the use of snakeyaml http://enriquezrene.com/home/index.php/en/groovy-english/groovy-for-java-developers/57-reading-yaml-files-from-groovy 结果,在src / main / resources中,我有一个名为application.yaml的配置文件,并使用snakeyaml读取了配置条目,这是有关使用snakeyaml的演示http://enriquezrene.com/home/index.php/ zh / groovy-english / groovy-for-java-developers / 57-groovy读取Yaml文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在spring boot项目中,如何将application.yaml加载到Java Properties中 - In a spring boot project, how to load application.yaml into Java Properties 根据系统环境变量加载Spring-Boot application.yaml文件 - Load Spring-Boot application.yaml file based on system environment variables 如何在运行时使用 spring 引导应用程序中的 java 代码对 application.yaml 进行读写操作? - How can I write and read to and from a application.yaml at runtime using java code in a spring boot application? 如何在 application.yaml (spring boot) 中禁用 whitelabel 错误页面 - How to disable whitelabel error page in application.yaml (spring boot) application.yaml 文件,用于启动一些 Spring 引导应用程序 - application.yaml files for starting some Spring Boot applications Spring Boot,从 application.yaml 读取的 Path 类型的属性 - Spring Boot, property of type Path read from application.yaml Spring 从应用程序启动 Kafka StreamsConfig 或 ConsumerConfig。yaml 未应用 - Spring Boot Kafka StreamsConfig or ConsumerConfig from application.yaml not applying 无法将 application.yaml 装入 docker java 8 spring - Can't mount application.yaml into docker java 8 spring 如何在应用程序中包含 jwt 秘密。yaml 为 Java Spring - How to include jwt secret in application.yaml for Java Spring Spring Boot: Map a map from application.yaml to a map with @Value annotation - Spring Boot: Map a map from application.yaml to a map with @Value annotation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM