Override property in .groovy app with external configuration in grails 3

Grails 3 no longer has the grails.config.locations property, now Grails 3 uses the Spring property source concept instead, but how can I achieve the same behavior in grails 3 as it was in previous versions? Suppose I want to override a property.to.be.overridden property.to.be.overridden in the application.grovy file with my external configuration file. How can i do this?

+5
source share
2 answers

The equivalent of grails.config.locations is spring.config.location

http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files

Here is an example indicating the configuration location when starting jar from the command line (the same arguments can be used inside your ide)

  java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties 

Also, since you mention that you want to override properties, it’s useful to find out how Spring Boot handles profile-specific property files (multiple profiles can also be specified)

http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-profile-specific-properties

+2
source

I solved it a little differently, so I could upload an external YAML file.

Application.groovy

 package com.mycompany.myapp import grails.boot.GrailsApp import grails.boot.config.GrailsAutoConfiguration import org.springframework.beans.factory.config.YamlPropertiesFactoryBean import org.springframework.context.EnvironmentAware import org.springframework.core.env.Environment import org.springframework.core.env.PropertiesPropertySource import org.springframework.core.io.FileSystemResource import org.springframework.core.io.Resource; class Application extends GrailsAutoConfiguration implements EnvironmentAware { static void main(String[] args) { GrailsApp.run(Application) } @Override void setEnvironment(Environment environment) { String configPath = System.properties["myapp.config.location"] if (configPath) { Resource resourceConfig = new FileSystemResource(configPath); YamlPropertiesFactoryBean propertyFactoryBean = new YamlPropertiesFactoryBean(); propertyFactoryBean.setResources(resourceConfig); propertyFactoryBean.afterPropertiesSet(); Properties properties = propertyFactoryBean.getObject(); environment.propertySources.addFirst(new PropertiesPropertySource("myapp.config.location", properties)) } } } 

Then I specify the YAML file when I run it

command line

 java -jar -Dmyapp.config.location=/etc/myapp/application.yml build/libs/myapp-0.1.war 
0
source

Source: https://habr.com/ru/post/1234813/


All Articles