grails-3.0grails-3.3

Use database property from external property file in grails 3


I want to use external property file for database configuration in production environment. i have tried some of solution from blogs and stack overflow but its work only for development environment.

grailsVersion=3.3.2

Solution

  • First create properties file in src/main/resources (if resources dir does not exist, create it).

    then remove configuration from application.yml (if won't then it will override). in Application.groovy load the file with this :

    def url = getClass().classLoader.getResource("myconfig.properties")
    def confFile = new File(url.toURI())
    Properties properties = new Properties()
    confFile.withInputStream {
      properties.load(it)
    }
    
    environment.propertySources.addFirst(new PropertiesPropertySource("local.config.location", properties))