Andrey Yaskulsky
Andrey Yaskulsky

Reputation: 2526

grails external configuration values are not read

I want to use external configuration in my application. Along with it I use mail plugin and want to store its configuration in external .property file. I've chaged my Config.groovy so now it looks for external configuration under USER_HOME/.grails/APP_NAME-configuration.properties:

environments {
    development {
        grails.logging.jul.usebridge = true
        if (new File("${userHome}/.grails/${appName}-config.properties").exists()) {
            println "*** User defined config ***"
            grails.config.locations = ["file:${userHome}/.grails/${appName}-config.properties"]
        }
    }
       .....
}

As you can see there is if statement which tests is the configuration file exists. It exists so every time I see "* User defined config *" message when application starts.

I'm running application in development mode. After that configuration of my mail plugin goes:

grails {
    mail {
        host = grailsApplication.config['mail']['host']
        port = grailsApplication.config['mail']['host']
        username = grailsApplication.config['mail']['username']
        password = grailsApplication.config['mail']['password']
        props = ["mail.smtp.auth": "true",
                "mail.smtp.socketFactory.port": "465",
                "mail.smtp.socketFactory.class": "javax.net.ssl.SSLSocketFactory",
                "mail.smtp.socketFactory.fallback": "false"]
    }
} 

When I tried to send a message with email plugin it appeared that configuration of email plugin is not initialized - username, password = null and host, port have default values (localhost and -1). So it seemed to me that config.properties file is simply not loaded. I decided to check if config.properties had been loaded and found out that grailsApplication.config object contained all properties that I'd defined in config.properties: mail.host, mail.port, mail.username, mail.password. I checked if it's possible to get corresponding properties using grailsApplication.config['property']['name'] expression (in debug mode). This expression gave proper values for all mail.host, mail.port, mail.username and mail.password. I also tried to use just grailsApplication.config['property.name'] (with one pair of brackets and '.') but this variant didn't work. Can someone help me handle this situation? I've spent almost 3 trying different variants and still don't have a solution. Thank you.

Upvotes: 1

Views: 1223

Answers (1)

Sikander
Sikander

Reputation: 862

You cannot load maps from .properties file. You should use .groovy file instead of .properties. You can load configuration from external *.groovy file where you can have maps etc. create a file with contents below...

grails {
    mail {
        host = smtp.gmail.com
        port = 465
        username = username
        password = password
        props = ["mail.smtp.auth"                  : "true",
                 "mail.smtp.socketFactory.port"    : "465",
                 "mail.smtp.socketFactory.class"   : "javax.net.ssl.SSLSocketFactory",
                 "mail.smtp.socketFactory.fallback": "false"]
    }
}

And use this in your config.groovy,

grails.config.locations = ["classpath:mail-config.groovy"]

Upvotes: 1

Related Questions