[英]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: 这里是我遵循的步骤:
Inside src/main/resources I created as many files as I need: 在src / main / resources内部,我根据需要创建了多个文件:
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')
} }
Before to build the project I called the function using doFirst: 在构建项目之前,我使用doFirst调用了该函数:
build.doFirst { loadConfigFile() }
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.